Link to home
Start Free TrialLog in
Avatar of Steve Bona
Steve BonaFlag for Canada

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Bona

ASKER

But i make only one change because it does'nt make a loop
I just add this
$DCs = @("dc01.domainA.com", "dc01.domainB.com", "dc01.domainC.com")
And then the script works fine.
Thanks
Avatar of oBdA
oBdA

That should work just fine without @() if you use a comma to separate the list elements.
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++)): $($_)"}

Open in new window

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

Open in new window

The only problem i just get now it's Export-csv
On Powershell console the result look fine. But whent i add in the end   Export-Csv -NoTypeInformation -Path $OutFile, i have a file with no result.
So i change the export-csv for OGV in powershell ise you can take a look in a attach file.
User generated image
I need to sort it by Name, SamAccountName and DC and export it on csv.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah Write-Host was the key in line 6 and now the result in my csv file look good.
Thanks for your help !!!