Steve Bona
asked on
Get SamAccountName for multiple users's name on multiple domains
I need to create a script to get SamAccountName from users's name csv file on several domains
Actually my script look like this
$PathFile = "C:\CSV\UsersINSPQ.csv"
Import-Csv -Path $PathFile | ForEach {
$User = $_.User
Get-ADuser -Filter {Name -eq $User} -Properties Name -Server "dc01.domainA.com" | Select Name, SamAccountName}
It works well, but i need to do it on 7 domains like domain A,B,C,... and G. So how to make a loop to have a single result for all domains with this script, and yes all domains trust each others.
Thanks
Actually my script look like this
$PathFile = "C:\CSV\UsersINSPQ.csv"
Import-Csv -Path $PathFile | ForEach {
$User = $_.User
Get-ADuser -Filter {Name -eq $User} -Properties Name -Server "dc01.domainA.com" | Select Name, SamAccountName}
It works well, but i need to do it on 7 domains like domain A,B,C,... and G. So how to make a loop to have a single result for all domains with this script, and yes all domains trust each others.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That should work just fine without @() if you use a comma to separate the list elements.
Just paste this into a PS console:
Just paste this into a PS console:
$DCs = "dc01.domainA.com", "dc01.domainB.com", "dc01.domainC.com"
$i = 1; $DCs | ForEach-Object {"Processing Loop $(($i++)): $($_)"}
PS C:\> $DCs = "dc01.domainA.com", "dc01.domainB.com", "dc01.domainC.com"
PS C:\> $i = 1; $DCs | ForEach-Object {"Processing Loop $(($i++)): $($_)"}
Processing Loop 1: dc01.domainA.com
Processing Loop 2: dc01.domainB.com
Processing Loop 3: dc01.domainC.com
ASKER
ASKER
I need to sort it by Name, SamAccountName and DC and export it on csv.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah Write-Host was the key in line 6 and now the result in my csv file look good.
Thanks for your help !!!
Thanks for your help !!!
ASKER
I just add this
$DCs = @("dc01.domainA.com", "dc01.domainB.com", "dc01.domainC.com")
And then the script works fine.
Thanks