{"id":208143,"date":"2023-11-29T13:49:16","date_gmt":"2023-11-29T13:49:16","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/monitorare-ultimo-accesso-di-un-utente-2\/"},"modified":"2024-03-04T19:04:04","modified_gmt":"2024-03-04T19:04:04","slug":"monitorare-ultimo-accesso-di-un-utente-2","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/it\/script-hub\/monitorare-ultimo-accesso-di-un-utente-2\/","title":{"rendered":"Come monitorare l&#8217;ultimo accesso di un utente con PowerShell"},"content":{"rendered":"<p>Nella moderna gestione IT, uno dei compiti pi\u00f9 importanti \u00e8 il monitoraggio degli account utente, in particolare l&#8217;identificazione di quelli inattivi. Questa attivit\u00e0 va oltre una semplice questione di ordine. \u00c8 direttamente collegata alla sicurezza, alla conformit\u00e0 e alla gestione efficiente delle risorse. Oggi analizzeremo uno script PowerShell che\u00a0consente di <strong>monitorare l&#8217;ultimo accesso di un utente<\/strong>.<\/p>\n<h2>Background<\/h2>\n<p>Lo script per monitorare l&#8217;ultimo accesso di un utente che andremo ad analizzare \u00e8 progettato per identificare e segnalare gli account utente in base al loro ultimo orario di accesso, in particolare quelli che non sono stati utilizzati per un periodo di tempo specificato. Garantire l&#8217;identificazione e la gestione degli account inutilizzati \u00e8 fondamentale per i professionisti IT e i <a href=\"https:\/\/www.ninjaone.com\/what-is-an-msp\/\">provider di servizi gestiti (MSP)<\/a>. Gli account inattivi comportano rischi per la sicurezza e spesso diventano facili bersagli per i criminali informatici. Inoltre, per le organizzazioni soggette a normative di conformit\u00e0, il monitoraggio dell&#8217;attivit\u00e0 degli utenti e la disabilitazione degli account non utilizzati possono essere uno dei requisiti a cui attenersi.<\/p>\n<h2>Lo script per monitorare l&#8217;ultimo accesso di un utente<\/h2>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 5.1\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    Returns exit code of 1 if any Enabled accounts that haven't been logged in over 90 days or a custom amount of days.\r\n.DESCRIPTION\r\n    Returns exit code of 1 if any Enabled accounts that haven't been logged in over 90 days or a custom amount of days.\r\n.EXAMPLE\r\n    No parameters needed.\r\n    Returns exit code of 1 if any Enabled accounts that haven't been logged in over 90 days.\r\n.EXAMPLE\r\n    -IncludeDisabled\r\n    Returns exit code of 1 if any Enabled or Disabled accounts that haven't been logged in over 90 days.\r\n.EXAMPLE\r\n    -Days 60\r\n    Returns exit code of 1 if any Enabled accounts that haven't been logged in over 60 days.\r\n.EXAMPLE\r\n    -Days 60 -IncludeDisabled\r\n    Returns exit code of 1 if any Enabled or Disabled accounts that haven't been logged in over 60 days.\r\n.OUTPUTS\r\n    None\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 7, Windows Server 2012\r\n    Exit code 1: Found users that haven't logged in over X days and are enabled.\r\n    Exit code 2: Calling \"net.exe user\" or \"Get-LocalUser\" failed.\r\n    Release Notes:\r\n    Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n.COMPONENT\r\n    ManageUsers\r\n#&gt;\r\n\r\n[CmdletBinding()]\r\nparam (\r\n    [Parameter()]\r\n    [int]\r\n    $Days = 90,\r\n    [Parameter()]\r\n    [switch]\r\n    $IncludeDisabled\r\n)\r\n\r\nbegin {}\r\nprocess {\r\n    if ($Days -lt 0) {\r\n        # Change negative days to the expected positive days\r\n        $Days = 0 - $Days\r\n    }\r\n    $Accounts = if ($(Get-Command \"Get-LocalUser\").Name -like \"Get-LocalUser\") {\r\n        try {\r\n            Get-LocalUser | Select-Object Name, Enabled, SID, LastLogon\r\n        }\r\n        catch {\r\n            exit 2\r\n        }\r\n    }\r\n    else {\r\n        # Get users from net.exe user\r\n        $Data = $(net.exe user) | Select-Object -Skip 4\r\n        # Check if the command ran the way we wanted and the exit code is 0\r\n        if ($($Data | Select-Object -Last 2 | Select-Object -First 1) -like \"*The command completed successfully.*\" -and $LASTEXITCODE -eq 0) {\r\n            # Process the output and get only the users\r\n            $Users = $Data[0..($Data.Count - 3)] -split 's+' | Where-Object { -not $([String]::IsNullOrEmpty($_)) }\r\n            # Loop through each user\r\n            $Users | ForEach-Object {\r\n                # Get the Account active property look for a Yes\r\n                $Enabled = $(net.exe user $_) | Where-Object {\r\n                    $_ -like \"Account active*\" -and\r\n                    $($_ -split 's+' | Select-Object -Last 1) -like \"Yes\"\r\n                }\r\n                # Get the Last logon property\r\n                $LastLogon = $(\r\n                    $(\r\n                        $(net.exe user $_) | Where-Object {\r\n                            $_ -like \"Last logon*\"\r\n                        }\r\n                    ) -split 's+' | Select-Object -Skip 2\r\n                ) -join ' '\r\n                # Get the Password last set property\r\n                $PasswordLastSet = $(\r\n                    $(\r\n                        $(net.exe user $_) | Where-Object {\r\n                            $_ -like \"Password last set*\"\r\n                        }\r\n                    ) -split 's+' | Select-Object -Skip 3\r\n                ) -join ' '\r\n                # Output Name and Enabled almost like how Get-LocalUser displays it's data\r\n                [PSCustomObject]@{\r\n                    Name      = $_\r\n                    Enabled   = if ($Enabled -like \"*Yes*\") { $true }else { $false }\r\n                    LastLogon = if ($LastLogon -like \"*Never*\") { [DateTime]::Parse($PasswordLastSet) } else { [DateTime]::Parse($LastLogon) }\r\n                }\r\n            }\r\n        }\r\n        else {\r\n            exit 2\r\n        }\r\n    }\r\n    $Output = $Accounts | Where-Object {\r\n        if ($IncludeDisabled) {\r\n            $_.LastLogon -lt $(Get-Date).AddDays(0 - $Days)\r\n        }\r\n        else {\r\n            $_.Enabled -and $_.LastLogon -lt $(Get-Date).AddDays(0 - $Days)\r\n        }\r\n    }\r\n    $Output | Out-String | Write-Host\r\n    if ($null -ne $Output) {\r\n        exit 1\r\n    }\r\n}\r\nend {}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"in-context-cta\"><p style=\"text-align: center;\">Accedi a oltre 700 script nel Dojo di NinjaOne<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.ninjaone.com\/it\/prova-gratuita\/\">Ottieni l&#8217;accesso<\/a><\/p>\n<\/div><\/p>\n<h2>Analisi dettagliata<\/h2>\n<p>Lo script per monitorare l&#8217;ultimo accesso di un utente inizia impostando una durata predefinita di 90 giorni per verificare l&#8217;inattivit\u00e0. Utilizza quindi il cmdlet &#8220;Get-LocalUser&#8221; o il comando &#8220;net.exe user&#8221;, a seconda di ci\u00f2 che \u00e8 disponibile.<\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\">Se il cmdlet &#8220;Get-LocalUser&#8221; \u00e8 presente, recupera direttamente i dettagli.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">In caso contrario, utilizza il comando &#8220;net.exe user&#8221;. Questo comando recupera i dati e poi elabora l&#8217;output per filtrare i dettagli richiesti.<\/li>\n<\/ul>\n<p>L\u2019operazione principale consiste nel verificare se la data dell&#8217;ultimo accesso di ciascun utente \u00e8 pi\u00f9 vecchia della soglia definita. Se viene trovata una corrispondenza, questa viene visualizzata e lo script per monitorare l&#8217;ultimo accesso di un utente termina con un codice di uscita 1. Il codice di uscita 2 viene utilizzato per le eccezioni durante il recupero dei dati utente.<\/p>\n<h2>Casi d&#8217;uso potenziali<\/h2>\n<p>Immagina Jane, una professionista IT di un&#8217;azienda di medie dimensioni. Recentemente, l&#8217;azienda ha subito una piccola violazione della sicurezza. Dopo l&#8217;incidente, Jane \u00e8 stata incaricata di rafforzare la sicurezza. Uno dei suggerimenti \u00e8 stato quello di controllare regolarmente gli account inattivi e di disabilitarli. Utilizzando questo script per monitorare l&#8217;ultimo accesso di un utente, Jane potr\u00e0 impostare una routine mensile per recuperare un elenco di tali account, assicurandosi che la directory degli utenti dell&#8217;azienda rimanga attiva e sicura.<\/p>\n<h2>Confronti<\/h2>\n<p>Questo script per monitorare l&#8217;ultimo accesso di un utente utilizza PowerShell, ma un altro approccio potrebbe consistere nell&#8217;interrogare direttamente Active Directory (se disponibile) per ottenere i dettagli dell&#8217;account utente. \u00c8 possibile utilizzare strumenti come ADManager Plus o anche Active Directory Users and Computers (ADUC) integrato. Tuttavia, il nostro script per monitorare l&#8217;ultimo accesso di un utente offre un&#8217;alternativa leggera, personalizzabile e gratuita.<\/p>\n<h2>Domande frequenti<\/h2>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\"><strong>E se volessi impostare un periodo di inattivit\u00e0 diverso?<br \/>\n<\/strong>Per farlo, puoi personalizzare il parametro $Days.<\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\"><strong>Lo script per monitorare l&#8217;ultimo accesso di un utente pu\u00f2 identificare sia gli account abilitati che quelli disabilitati?<br \/>\n<\/strong>S\u00ec, utilizzando l&#8217;opzione -IncludeDisabled, lo script per monitorare l&#8217;ultimo accesso di un utente pu\u00f2 recuperare sia gli account abilitati che quelli disabilitati.<\/li>\n<\/ul>\n<h2>Implicazioni<\/h2>\n<p>Oltre all&#8217;identificazione degli account inattivi, l&#8217;implicazione pi\u00f9 importante di questo script per monitorare l&#8217;ultimo accesso di un utente riguarda la sicurezza informatica. Gli account inattivi possono essere facilmente compromessi, fornendo un accesso non autorizzato. Controlli periodici dell&#8217;inattivit\u00e0 degli utenti e azioni appropriate possono prevenire potenziali violazioni.<\/p>\n<h2>Raccomandazioni<\/h2>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\">Testa sempre lo script per monitorare l&#8217;ultimo accesso di un utente in un ambiente sandbox prima di distribuirlo in una configurazione live.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">Pianifica l&#8217;esecuzione periodica di questo script per monitorare l&#8217;ultimo accesso di un utente, per garantire un monitoraggio continuo.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"3\" data-aria-level=\"1\">Quando vengono individuati account inattivi, intraprendi le azioni appropriate, come la disabilitazione dell&#8217;account o la notifica all&#8217;utente o al suo manager.<\/li>\n<\/ul>\n<h2>Considerazioni finali<\/h2>\n<p>Sebbene lo script fornisca una soluzione efficiente per monitorare l&#8217;ultimo accesso di un utente, l&#8217;integrazione di strumenti come <a href=\"https:\/\/www.ninjaone.com\/it\/\">NinjaOne<\/a> pu\u00f2 migliorare ulteriormente le capacit\u00e0 di gestione in senso pi\u00f9 ampio, rendendo le attivit\u00e0 pi\u00f9 automatizzate e fornendo una <a href=\"https:\/\/www.ninjaone.com\/it\/gestione-endpoint\/\">dashboard centralizzata<\/a>. Strumenti come NinjaOne sono progettati per gestire tali attivit\u00e0 in modo efficiente, assicurando che i professionisti IT come Jane possano concentrarsi su attivit\u00e0 pi\u00f9 strategiche, utilizzando script come quello sopra riportato come parte di un kit di strumenti pi\u00f9 ampio.<\/p>\n","protected":false},"author":35,"featured_media":144297,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4212],"use_cases":[4277],"class_list":["post-208143","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub\/208143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/comments?post=208143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media\/144297"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media?parent=208143"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/operating_system?post=208143"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/use_cases?post=208143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}