{"id":208118,"date":"2023-09-26T12:07:13","date_gmt":"2023-09-26T12:07:13","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/elencare-i-gruppi-di-cui-un-utente-fa-parte-powershell-2\/"},"modified":"2024-03-04T19:22:19","modified_gmt":"2024-03-04T19:22:19","slug":"elencare-i-gruppi-di-cui-un-utente-fa-parte-powershell-2","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/it\/script-hub\/elencare-i-gruppi-di-cui-un-utente-fa-parte-powershell-2\/","title":{"rendered":"Come elencare i gruppi di cui un utente fa parte, in modo efficiente, con PowerShell"},"content":{"rendered":"<p><span class=\"TextRun SCXW969672 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW969672 BCX0\">Nel panorama in rapida evoluzione dell&#8217;IT, la gestione efficace degli utenti e la comprensione dei loro ruoli <\/span><span class=\"NormalTextRun SCXW969672 BCX0\">rimangono<\/span><span class=\"NormalTextRun SCXW969672 BCX0\"> fondamentali per la sicurezza e l&#8217;efficienza di qualsiasi organizzazione. Tra gli strumenti a disposizione dei professionisti IT, gli script PowerShell si distinguono come potenti soluzioni per automatizzare le attivit\u00e0. Uno di questi script si occupa di <strong>elencare i gruppi di cui un utente fa parte<\/strong>.<\/span><\/span><\/p>\n<h2>Background<\/h2>\n<p><span class=\"TextRun SCXW228991937 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW228991937 BCX0\">I gruppi di cui un utente fa parte definiscono i diritti e le autorizzazioni di cui dispone un utente all&#8217;interno di una rete. Con la crescita dell&#8217;organizzazione, tenere traccia dei gruppi di cui un utente fa parte diventa sempre pi\u00f9 importante. Tuttavia, provare a farlo manualmente pu\u00f2 essere un&#8217;attivit\u00e0 complicata e soggetta a errori. \u00c8 qui che il nostro script PowerShell si fa notare. Progettato in modo specifico per individuare ed elencare i gruppi di cui un utente fa parte<\/span><span class=\"NormalTextRun ContextualSpellingAndGrammarErrorV2Themed SCXW228991937 BCX0\">,<\/span><span class=\"NormalTextRun SCXW228991937 BCX0\"> questo script \u00e8 prezioso per i professionisti IT e i Managed Service Provider (MSP). Garantire la corretta appartenenza ai gruppi aiuta a evitare l&#8217;assegnazione di privilegi eccessivi e rafforza la posizione di sicurezza di un&#8217;organizzazione.<\/span><\/span><\/p>\n<h2>Lo script per individuare i gruppi di cui un utente fa parte<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 4.0 -RunAsAdministrator\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    This will output the groups that the specified user belongs to.\r\n.DESCRIPTION\r\n    This will output the groups that the specified user belongs to.\r\n.EXAMPLE\r\n     -UserName \"Administrator\" -IsDomainUser\r\n    Will get the groups that the user Administrator belongs to in Active Directory.\r\n.EXAMPLE\r\n     -UserName \"Administrator\"\r\n    Will get the groups that the user Administrator belongs to on the machine it runs on.\r\n.EXAMPLE\r\n    PS C:&gt; Get-User-Membership.ps1 -UserName \"Administrator\"\r\n    Will get the groups that the user Administrator belongs to on the machine it runs on.\r\n.OUTPUTS\r\n    Output (PSCustomObject)\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 10, Windows Server 2012\r\n    If you wish to interact with AD you will need to install RSAT with at least the AD feature.\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    # Specify one user\r\n    [Parameter(Mandatory = $false)]\r\n    [String]\r\n    $UserName,\r\n    # Modify Local User Membership\r\n    [Parameter(Mandatory = $false)]\r\n    [Switch]\r\n    $IsDomainUser\r\n)\r\n\r\nbegin {}\r\n\r\nprocess {\r\n    if (-not $IsDomainUser) {\r\n        # Get local machine groups\r\n        Get-LocalGroup | ForEach-Object {\r\n            $Group = $_.Name\r\n            # Get users in current group\r\n            # Check that $UserName is a member of this current group and output it to StdOut\/Write-Output\r\n            Get-LocalGroupMember -Group $Group | Where-Object { $_.Name -like \"*$UserName\" } | ForEach-Object {\r\n                [PSCustomObject]@{\r\n                    Group = $Group\r\n                    User  = $_.Name\r\n                }\r\n            }\r\n        }\r\n    }\r\n    else {\r\n        if ((Get-Module -Name ActiveDirectory -ListAvailable -ErrorAction SilentlyContinue)) {\r\n            try {\r\n                Import-Module -Name ActiveDirectory\r\n                # Get most of our data needed for the logic, and to reduce the number of time we need to talk to AD\r\n                $ADUser = (Get-ADUser -Identity $UserName -Properties SamAccountName -ErrorAction SilentlyContinue).SamAccountName\r\n            }\r\n            catch {\r\n                Write-Error -Message \"Ninja Agent could not access AD, either RSAT was not installed or that the agent does not have permissions to view users and groups.\"\r\n                exit 5 # Access Denied exit code\r\n            }\r\n            # Get a list of groups that the user is in\r\n            # Loop through each group\r\n            Get-ADGroup -Filter * -ErrorAction SilentlyContinue | ForEach-Object {\r\n                $ADGroup = $_\r\n                # Get users from current group and filter out all other users\r\n                Get-ADGroupMember -Identity $ADGroup -ErrorAction SilentlyContinue | Where-Object {\r\n                    $_.SamAccountName -like $ADUser\r\n                } | ForEach-Object {\r\n                    # Write out to StandardOutput\r\n                    [PSCustomObject]@{\r\n                        Group = $ADGroup.Name\r\n                        User  = $_.SamAccountName\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        else {\r\n            # Throw error that RSAT: ActiveDirectory isn't installed\r\n            Write-Error -Message \"RSAT: ActiveDirectory is not installed or not found on this computer. The PowerShell Module called ActiveDirectory is needed to proceed.\" -RecommendedAction \"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/activedirectory\/\"\r\n            exit 2 # File Not Found exit code\r\n        }\r\n    }\r\n}\r\nend {}<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p>Accedi a oltre 300 script nel Dojo di NinjaOne<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/it\/prova-gratuita\/\">Ottieni l&#8217;accesso<\/a><\/p>\n<\/div>\n<h2>Analisi dettagliata<\/h2>\n<p><span data-contrast=\"none\">Lo script per individuare i gruppi di cui un utente fa parte pu\u00f2 essere suddiviso in due componenti principali in base alla sua funzione:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/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;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Controllo dell&#8217;appartenenza al gruppo locale:<\/span><\/b><span data-contrast=\"none\"> Se l&#8217;opzione <\/span><b><span data-contrast=\"none\">-IsDomainUser<\/span><\/b><span data-contrast=\"none\"> non \u00e8 specificata, lo script recupera ed elenca i gruppi di cui l&#8217;utente specificato fa parte sulla macchina locale.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/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;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Controllo dell&#8217;appartenenza al gruppo Active Directory:<\/span><\/b><span data-contrast=\"none\"> Se l&#8217;opzione <\/span><b><span data-contrast=\"none\">-IsDomainUser<\/span><\/b><span data-contrast=\"none\"> \u00e8 attiva, lo script comunica con Active Directory per recuperare ed elencare i gruppi di cui fa parte l&#8217;utente specificato.<\/span><\/li>\n<\/ul>\n<p><span class=\"TextRun SCXW70367704 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70367704 BCX0\">Durante il processo, lo script per individuare i gruppi di cui un utente fa parte visualizza l\u2019output dei risultati come un <\/span><\/span><span class=\"TextRun MacChromeBold SCXW70367704 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70367704 BCX0\">PSCustomObject<\/span><\/span><span class=\"TextRun SCXW70367704 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70367704 BCX0\"> assicurando che i dati siano strutturati e possano essere facilmente compresi o elaborati.<\/span><\/span><\/p>\n<h2>Situazione d&#8217;uso potenziale<\/h2>\n<p><span class=\"TextRun SCXW155228754 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW155228754 BCX0\">Immagina un professionista IT di una grande azienda che deve verificare l&#8217;appartenenza a un gruppo di un nuovo responsabile di reparto. Utilizzando lo script, il professionista IT pu\u00f2 facilmente elencare tutti i gruppi di cui un utente fa parte sia in ambiente locale che in Active Directory. In questo modo si garantisce che il responsabile del reparto abbia le autorizzazioni corrette, evitando potenziali violazioni della sicurezza o errori di accesso.<\/span><\/span><\/p>\n<p>Approccio alternativo<\/p>\n<p><span class=\"TextRun SCXW173330996 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW173330996 BCX0\">Anche se l\u2019interfaccia grafica di Active Directory Users and Computers (ADUC) consente di visualizzare i gruppi di cui un utente fa parte, richiede un intervento<\/span><span class=\"NormalTextRun SCXW173330996 BCX0\"> manuale e non \u00e8 adatta a controlli di massa. Il nostro script PowerShell automatizza questa attivit\u00e0, rendendola efficiente per il controllo di pi\u00f9 utenti o per verifiche frequenti. Inoltre, con PowerShell, i risultati possono essere facilmente esportati, filtrati o integrati nei report, e questo offre una flessibilit\u00e0 che gli strumenti della GUI non hanno.<\/span><\/span><\/p>\n<h2>Domande frequenti<\/h2>\n<ul>\n<li><strong>D: Lo script pu\u00f2 essere eseguito senza i privilegi di amministratore?<\/strong><br \/>\nR: Lo script per individuare i gruppi di cui un utente fa parte richiede diritti di amministrazione e garantisce risultati accurati grazie all&#8217;accesso alle risorse di sistema necessarie.<\/li>\n<li><strong>D: Cosa succede se il modulo Active Directory non viene trovato nel sistema?<\/strong><br \/>\nR: Lo script per individuare i gruppo di cui un utente fa parte visualizza un messaggio di errore in cui si consiglia l&#8217;installazione di RSAT con la funzionalit\u00e0 Active Directory.<\/li>\n<\/ul>\n<h2>Implicazioni<\/h2>\n<p><span class=\"TextRun SCXW38011074 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW38011074 BCX0\">L&#8217;appartenenza a gruppi non pertinenti pu\u00f2 portare all&#8217;accesso non autorizzato ai dati o all\u2019impossibilit\u00e0 di accedere a risorse che sono essenziali per alcuni utenti. Grazie all&#8217;accuratezza con cui lo script PowerShell <\/span><span class=\"NormalTextRun SCXW38011074 BCX0\">elenca<\/span><span class=\"NormalTextRun SCXW38011074 BCX0\"> i gruppi di cui un utente fa parte, i professionisti IT possono migliorare la sicurezza e garantire la conformit\u00e0 alle normative<\/span>.<\/span><\/p>\n<h2>Suggerimenti<\/h2>\n<ul>\n<li><span data-contrast=\"none\">Esegui sempre lo script per individuare i gruppi di cui un utente fa parte in un ambiente sicuro e controllato.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li><span data-contrast=\"none\">Verifica regolarmente i gruppi di cui un utente fa parte, soprattutto per i ruoli ad alto privilegio.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:false,&quot;134233118&quot;:false,&quot;201341983&quot;:0,&quot;335559738&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li><span data-contrast=\"none\">Integra i risultati dello script per verificare i gruppi di cui un utente fa parte in una strategia IAM (Identity and Access Management) pi\u00f9 ampia.<\/span><\/li>\n<\/ul>\n<h2>Considerazioni finali<\/h2>\n<p><span class=\"TextRun SCXW72627753 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW72627753 BCX0\">NinjaOne<\/span><span class=\"NormalTextRun SCXW72627753 BCX0\">, con la sua suite completa di soluzioni IT, pu\u00f2 essere un partner prezioso per <a href=\"https:\/\/www.ninjaone.com\/it\/\">automatizzare, gestire e proteggere gli ambienti IT<\/a>. Quando script come quello per individuare i gruppi di cui un utente fa parte, di cui abbiamo parlato, vengono combinati con le funzionalit\u00e0 di <\/span><span class=\"NormalTextRun SCXW72627753 BCX0\">NinjaOne<\/span><span class=\"NormalTextRun SCXW72627753 BCX0\">, <a href=\"https:\/\/www.ninjaone.com\/it\/efficienza-it\/\">i professionisti IT possono migliorare efficienza<\/a>, precisione e sicurezza. Questa <\/span><span class=\"NormalTextRun SCXW72627753 BCX0\">sinergia<\/span><span class=\"NormalTextRun SCXW72627753 BCX0\"> apre la strada a un approccio proattivo alla gestione e alla sicurezza IT.<\/span><\/span><span class=\"EOP SCXW72627753 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n","protected":false},"author":35,"featured_media":207228,"parent":0,"menu_order":0,"comment_status":"closed","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-208118","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\/208118","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=208118"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media\/207228"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/media?parent=208118"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/operating_system?post=208118"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/it\/wp-json\/wp\/v2\/use_cases?post=208118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}