locked
Elenco Utenti Dominio escludendo alcune OU RRS feed

  • Domanda

  • Buongiorno,

    per estrarre l'elenco degli utenti e di alcuni attributi utilizzo il seguente script powershell:

    $ExportPath = 'C:\Estrazioni\Elenco_Utenti.csv'

    Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, SAMAccountname, Mail,  Description | Select-Object -Property @{Name="Nome"; Expression = {$_.displayname}},@{Name="Ultimo Logon";Expression= {$_.lastLogondate}},@{Name="Username"; Expression ={$_.samaccountname}},@{Name="Email"; Expression ={$_.mail}},@{Name="Abilitato"; Expression = {$_.enabled}},@{Name="Descrizione"; Expression = {$_.description}}|Sort-Object Nome | Export-Csv -NoTypeInformation $ExportPath

    Lo script funziona e fa il suo lavoro ma vorrei inserire un "filtro" in cui escludo una o più OU da cui non voglio estratti gli utenti? Come posso farlo?

    Grazie.

    lunedì 22 febbraio 2021 12:25

Risposte

  • Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, SAMAccountname, Mail,  Description,distinguishedname | 
    	Where-Object -Property DistinguishedName -NotMatch -Value "OU=Amministrazione,OU=Users" |
    		Select-Object -Property @{Name="Nome"; Expression = {$_.displayname}}, `
    								@{Name="Ultimo Logon";Expression= {$_.lastLogondate}},`
    								@{Name="Username"; Expression ={$_.samaccountname}},`
    								@{Name="Email"; Expression ={$_.mail}},`
    								@{Name="Abilitato"; Expression = {$_.enabled}},`
    								@{Name="DN";Expression = {$_.distinguishedname}},`
    								@{Name="Descrizione"; Expression = {$_.description}}|
    									Sort-Object Nome #| Export-Csv -NoTypeInformation $ExportPath

    Giocando sulla riga : where-object, potrai selezionare od escludere ciò che vuoi

    Where-Object -Property DistinguishedName -NotMatch -Value "OU=Amministrazione,OU=Users"

    o

    Where-Object -Property DistinguishedName -Match -Value "OU=Marketing"


    Gastone Canali >


    Se alcuni post rispondono al tuo quesito, ricorda di contrassegnarli come risposta e non dimenticare anche i post utili. GRAZIE! Dai un occhio ai link Click Here and Here Leaderboard Here



    martedì 23 febbraio 2021 16:35
    Moderatore
  • $OU1 = 'OU=Amministrazione,OU=Users'
    $OU2 = 'OU=zabbixVM'
    $ou3 = 'OU=home'
    Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, SAMAccountname, Mail,  Description,distinguishedname | 
    	Where-Object -Property DistinguishedName -NotMatch -Value "$ou1|$ou2|$ou3"|
    		Select-Object -Property @{Name="Nome"; Expression = {$_.displayname}}, `
    								@{Name="Ultimo Logon";Expression= {$_.lastLogondate}},`
    								@{Name="Username"; Expression ={$_.samaccountname}},`
    								@{Name="Email"; Expression ={$_.mail}},`
    								@{Name="Abilitato"; Expression = {$_.enabled}},`
    								@{Name="DN";Expression = {$_.distinguishedname}},`
    								@{Name="Descrizione"; Expression = {$_.description}}|
    									Sort-Object Nome #| Export-Csv -NoTypeInformation $ExportPath

    Con il  NotMatch  puoi usare le regular expression 

    https://jdhitsolutions.com/blog/powershell/7817/distinguished-parsing-with-powershell-and-regex/

    https://www.ntk.si/urnik/arhiv/download/aac00162-188c-40f4-a9b7-c5f194ee2121

      

    Gastone Canali >


    Se alcuni post rispondono al tuo quesito, ricorda di contrassegnarli come risposta e non dimenticare anche i post utili. GRAZIE! Dai un occhio ai link Click Here and Here Leaderboard Here

    • Contrassegnato come risposta Oldsys71 martedì 2 marzo 2021 07:28
    lunedì 1 marzo 2021 21:20
    Moderatore

Tutte le risposte

  • Ciao OldSys,

    spero che entra Gastone Canali, lui e il guru dei script, io provo a ricercare qualcosa, pero non la mia specialita. 

    Saluti, Nikola

    martedì 23 febbraio 2021 12:10
    Moderatore
  • Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, SAMAccountname, Mail,  Description,distinguishedname | 
    	Where-Object -Property DistinguishedName -NotMatch -Value "OU=Amministrazione,OU=Users" |
    		Select-Object -Property @{Name="Nome"; Expression = {$_.displayname}}, `
    								@{Name="Ultimo Logon";Expression= {$_.lastLogondate}},`
    								@{Name="Username"; Expression ={$_.samaccountname}},`
    								@{Name="Email"; Expression ={$_.mail}},`
    								@{Name="Abilitato"; Expression = {$_.enabled}},`
    								@{Name="DN";Expression = {$_.distinguishedname}},`
    								@{Name="Descrizione"; Expression = {$_.description}}|
    									Sort-Object Nome #| Export-Csv -NoTypeInformation $ExportPath

    Giocando sulla riga : where-object, potrai selezionare od escludere ciò che vuoi

    Where-Object -Property DistinguishedName -NotMatch -Value "OU=Amministrazione,OU=Users"

    o

    Where-Object -Property DistinguishedName -Match -Value "OU=Marketing"


    Gastone Canali >


    Se alcuni post rispondono al tuo quesito, ricorda di contrassegnarli come risposta e non dimenticare anche i post utili. GRAZIE! Dai un occhio ai link Click Here and Here Leaderboard Here



    martedì 23 febbraio 2021 16:35
    Moderatore
  • Ciao Gastone,

    grazie infinite per la tua risposta e scusa il ritardo nel risponderti.

    Roberto.

    lunedì 1 marzo 2021 10:55
  • Buongiorno Gastone,

    ho provato ad inserire una seconda riga Where-Object per escludere una seconda OU in questo modo:

    Where-Object -Property DistinguishedName -NotMatch -Value "OU=Amministrazione,OU=Users"
    	Where-Object -Property DistinguishedName -NotMatch -Value "OU=Tecnico,OU=Users"

    fa il suo lavoro e restituisce gli utenti che mi aspettavo. Mi chiedo, è possibile ottenere lo stesso risultato in modo più elegante ad esempio un unica riga Where-Object in cui inserisco i valori separati da un "and" oppure una serie di variabili che inizializzo all'inizio del codice in modo da snellire il tutto? Qualcosa del tipo:

    $OU1 = 'OU=Amministrazione,OU=Users'

    $OU2 = 'OU=Produzione,OU=Users'

    Where-Object -Property DistinguishedName -Notmatch -Value $OU1 -and $OU2


    Naturalmente è solo a scopo di esempio.

    Grazie.

    lunedì 1 marzo 2021 12:48
  • $OU1 = 'OU=Amministrazione,OU=Users'
    $OU2 = 'OU=zabbixVM'
    $ou3 = 'OU=home'
    Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, SAMAccountname, Mail,  Description,distinguishedname | 
    	Where-Object -Property DistinguishedName -NotMatch -Value "$ou1|$ou2|$ou3"|
    		Select-Object -Property @{Name="Nome"; Expression = {$_.displayname}}, `
    								@{Name="Ultimo Logon";Expression= {$_.lastLogondate}},`
    								@{Name="Username"; Expression ={$_.samaccountname}},`
    								@{Name="Email"; Expression ={$_.mail}},`
    								@{Name="Abilitato"; Expression = {$_.enabled}},`
    								@{Name="DN";Expression = {$_.distinguishedname}},`
    								@{Name="Descrizione"; Expression = {$_.description}}|
    									Sort-Object Nome #| Export-Csv -NoTypeInformation $ExportPath

    Con il  NotMatch  puoi usare le regular expression 

    https://jdhitsolutions.com/blog/powershell/7817/distinguished-parsing-with-powershell-and-regex/

    https://www.ntk.si/urnik/arhiv/download/aac00162-188c-40f4-a9b7-c5f194ee2121

      

    Gastone Canali >


    Se alcuni post rispondono al tuo quesito, ricorda di contrassegnarli come risposta e non dimenticare anche i post utili. GRAZIE! Dai un occhio ai link Click Here and Here Leaderboard Here

    • Contrassegnato come risposta Oldsys71 martedì 2 marzo 2021 07:28
    lunedì 1 marzo 2021 21:20
    Moderatore
  • Ancora grazie Gastone,

    ho iniziato da qualche mese ad utilizzare Powershell, studierò con attenzione la documentazione che hai linkato. 

    martedì 2 marzo 2021 07:30