Read txt line by line and query AD

后端 未结 2 468
太阳男子
太阳男子 2021-01-26 11:42

I have a text file containing samids:

XXXXXXX
YYYYYYY
ZZZZZZZ

For each one I need to read it quering AD with this:

dsquery user         


        
相关标签:
2条回答
  • 2021-01-26 12:20

    Use Get-ADUser from the ActiveDirectory PowerShell module instead of the ds tools:

    Import-Module ActiveDirectory
    
    Get-Content 'C:\path\to\input.txt' |
      Get-ADUser -SearchBase 'DC=example,DC=org' -Property mail |
      select -Expand mail | Out-File 'C:\path\to\output.txt'
    

    Where DC=example,DC=org is the distinguished name (DN) of your forest root domain.

    For programmatically determining the DN of the forest root domain you could use this:

    ([ADSI]"LDAP://RootDSE").RootDomainNamingContext
    
    0 讨论(0)
  • 2021-01-26 12:40

    Finally, I managed to solve it this way proposed on the other post:

    $a = Get-Content .\input.txt 
    for ($i=0 ; $i -lt $a.Length; $i++){
     dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line >> output.txt
    }
    

    Thank you

    0 讨论(0)
提交回复
热议问题