Author Posts

January 1, 2012 at 12:00 am

by southtx07 at 2013-03-13 10:04:13

Hello, Hoping I can get some assistance with my script below. When I run the script, it returns the data from the last record from the array and not all and I cannot figure out why this is so. if I scroll over $users I can see all the users.

$users = Get-QADUser -email '*@xyz*', '*@zzz*', '*@abc*' -IncludeAllProperties | ? {$_.accountisdisabled -eq $false}
foreach($user in $users)
{
$Company = $null
$email = $null
$displayname = $null
$jobtitle = $null
$IsDisabled = $null
$employeeid = $user.employeeid
$displayname = $user.displayname
$Company = $user.company
$logonname = $user.logonname
$manager = $user.manager
$costcenter = $user.department
$managername = Get-QADUser -identity $manager
$mgr = $managername.displayname
$jobtitle = $user.description
$email = $user.email
$IsDisabled = $user.accountisdisabled
}
Write-Host "$EmployeeID | $DisplayName | $CostCenter | $JobTitle | $Mgr | $Email | $Company"

by ArtB0514 at 2013-03-13 10:51:58

You need to put the Write-Host command (although you don't really want to use that command) inside the foreach loop in order to see the data from each user. Also, you don't need to set a variable to $null before you set it to a real value. Try this:

$users = Get-QADUser -email '*@xyz*', '*@zzz*', '*@abc*' -IncludeAllProperties | Where {$_.accountisdisabled -eq $false}
foreach($user in $users) {
$employeeid = $user.employeeid
$displayname = $user.displayname
$Company = $user.company
$logonname = $user.logonname
$costcenter = $user.department
$mgr = (Get-QADUser -identity $user.manager).displayname
$jobtitle = $user.description
$email = $user.email
$IsDisabled = $user.accountisdisabled
"$EmployeeID | $DisplayName | $CostCenter | $JobTitle | $Mgr | $Email | $Company"
}

by southtx07 at 2013-03-13 11:58:45

Many thanks ArtB0514. Worked perfectly.