Array not working

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #6306

    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.

You must be logged in to reply to this topic.