0

So I received a list of users from a co-worker who needed to confirm who in the list was still employed and who wasn't. I chose to filter out all users that either didn't exist in AD or were disabled and assign them to $TerminatedUser. I took all active users that assigned them to $EmployeedUser. (I know I spelled "Employed" wrong) I then tried to use the data from $EmployeedUser and $TerminatedUser and create a report within $EmployementStatus.

enter image description here

What I end up with is two columns which is awesome but I also only get 1 cell for each column. All the data for each column is bunched into one cell which makes it hard to read. At first when outputting $EmployementStatus to a csv file was only getting the headers and [system.object] for each cell. I was able to get around that.

enter image description here

So my question here now is: Is it possible to export $EmployementStatus to a csv where the data is listed out and each "Employed"/"Terminated" user receives their own cell as opposed to them all being bunched in cells A2 and B2?

Teach me something!

enter image description here

2
  • 1
    Please read stackoverflow.com/help/how-to-ask Commented Jan 18, 2023 at 19:25
  • I have an answer for this, but screenshots are terrible when you want people to review your code. Please format AS code. Commented Jan 19, 2023 at 5:49

1 Answer 1

1

This is sample code, since I'm not going to type out all that stuff again. And it isn't tested.

What you want, apparently, is to check there's an enabled AD user account that matches your userlist. For Powershell versions greater than 3.0, you can output [pscustomobject] directly into an array from a Foreach.

You just need ONE query to AD to determine if a user exists and whether the account is enabled ("Enabled" is one of the default properties returned in Get-AdUser).

It's probably more convenient for output if you simply have a "Verified" column and set that to TRUE or FALSE. Or you can have a "Status" column and output text to that like "Disabled" or "NotPresent" or "Verified". Whatever, really, I'm going with the easiest.

The try/catch is so you don't get a load of errors when the user doesn't exist. If you want to set different statuses for each "state", then you can place strings in there rather than $true/$false.

$employmentStatus = Foreach ($GID in $MyList) {
    $ID = $GID.SamAccountname
    try { 
        # if the user isn't found, it'll go to the Catch block after the next line
        $u = get-aduser $ID -erroraction stop
        if ($u.enabled) {
            $verified = $true
        }
        else {
            $verified = $false
        }
    }
    catch {
        # if the user doesn't exist, they're not verified
        $verified = $false
    }
    # output the per-user status as a pscustomobject in $employmentStatus
    [pscustomobject]@{
           ADUser = $ID
           Verified = $verified
    }
}

You should find that if you process your userlist with that, you can check the result with $employmentStatus | out-gridview.

That should show the "AdUser" and "Verified" columns, with TRUE or FALSE for each user.

If that looks OK, so will your CSV export: $employmentStatus | export-csv [path].

If you're using an old PS version, then you may need to predefine your output array as you did originally. Then you'd just fix up the line with the [pscustomobject] to append it to the array. Everything else works the same.

$employmentStatus = @()
Foreach ($GID in $MyList) {
...
    # output the per-user status as a pscustomobject - append to $employmentStatus
    $employmentStatus += [pscustomobject]@{
           ADUser = $ID
           Verified = $verified
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.