Need data from different servers

I need data from 4 different servers, however, I am getting result only from the server I am running the script.

$domains = "us.jas.com","cm.jas.com","asia.jas.com","europe.jas.com"

foreach($domains in $domain){

Get-ADUser -filter * -Property EmployeeID, Givenname, sn,  EmailAddress, StreetAddress, City, State, Country, PostalCode |Select-object EmployeeID, Givenname, sn, EmailAddress, StreetAddress, City, State, Country, PostalCode |export-csv J:/eudcusersnew.csv
}

Also how can I put a filter that Employee id should not be a service account(employee id = "service") nor employee id should be like "234567a" or Givenname should not contain a '-"._
Aakash AggarwalAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You're only getting results for the server you're running this on because you're not using the $domain loop variable anywhere.
Export-Csv's "-Append" is only available in PS 3.0 or later, which means you're still running PS 2.0 from 2009. WMF 5 still supports Windows 7; time to upgrade.
But even if -Append were available, you shouldn't use it that way. The file would have to be opened and closed for each and every user written, which slows things down immensely.
Use the ForEach-Object cmdlet instead of the ForEach statement, and just pipe the results to Export-Csv.
Assuming you have permissions in all domains, try this:
$domains = "us.jas.com", "cm.jas.com", "asia.jas.com", "europe.jas.com"
$domains | ForEach-Object {
	$domain = $_
	Write-Host "Processing $($domain) ..."
	Get-ADUser -Server $domain -Filter {(employeeID -ne 'Service') -and (employeeID -notlike '*234567a*') -and (givenName -notlike '*-*')} -Property EmployeeID, Givenname, sn,  EmailAddress, StreetAddress, City, State, Country, PostalCode |
		Select-Object -Property @{n='Domain'; e={$domain}}, EmployeeID, Givenname, sn, EmailAddress, StreetAddress, City, State, Country, PostalCode
} | Export-Csv -NoTypeInformation -Path J:\eudcusersnew.csv

Open in new window

1
 
sirbountyCommented:
add the append parameter...
|export-csv J:/eudcusersnew.csv -append

Open in new window

0
 
sirbountyCommented:
Also how can I put a filter that Employee id should not be a service account(employee id = "service") nor employee id should be like "234567a" or Givenname should not contain a '-"._

-filter {EmployeeID -like '234567a*' -and GivenName -notlike '*.*'}
0
 
Aakash AggarwalAuthor Commented:
Now getting error : A parameter cannot be found that matched parameter name Append.

$domains = "us.jas.com","cm.jas.com","asia.jas.com","europe.jas.com"

foreach($domain in $domains){

Get-ADUser -filter * -Property EmployeeID, Givenname, sn,  EmailAddress, StreetAddress, City, State, Country, PostalCode |Select-object EmployeeID, Givenname, sn, EmailAddress, StreetAddress, City, State, Country, PostalCode |export-csv J:/eudcusersnew1.csv -Append
}

Where should the below filter be put in this query ?

-filter {EmployeeID -like '234567a*' -and GivenName -notlike '*-*'} -> Here I need to filter for all EmployeeID that end with 'a' or where Employee ID = service and GivenName -notlike '*-*.
0
 
oBdACommented:
Question answered.
0
All Courses

From novice to tech pro — start learning today.