I have a text file containing samids:
XXXXXXX
YYYYYYY
ZZZZZZZ
For each one I need to read it quering AD with this:
dsquery user
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
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