I'm attempting to run a query against AD to give me a list of users. Since our organisation adds the tag "LastName, FirstName" to the description field of computer objects, I also want to list out the assigned machines per user. This is what I have so far:
$ADGroup = "SomeADGroup"
$UserList = Get-ADGroupMember -Identity $ADGroup -Recursive `
| Select SamAccountName `
| foreach-object {(Get-ADUser -Identity $_.SamAccountName `
| Select @{n='AssignedUser';e={$_.Surname + ", " + $_.GivenName}}).AssignedUser}
$Results = New-Object System.Collections.Generic.List[System.Object]
foreach ($User in $UserList)
{
$MachineList = @(((Get-ADComputer -Filter "Description -like `"*$User*`"" | Select Name).Name) -join $_ + ",").ToUpper()
foreach ($Machine in $MachineList)
{
$Results +=
@{
'User' = $User
'Machine' = $Machine
}
}
}
$Results | ForEach-Object { [pscustomobject] $_ } | Format-Table
The result is something like this:
User Machine
---- -------
White, Scott W107175
Jones, Henry W107195
Flinstone, Fred L109531,W108812
Since it's possible to have multiple machines assigned to each user, I'm trying to figure out a way to dynamically add "columns" to the array so that each machine name element resides in a column. This will eventually make it easier to export to CSV. I'm looking for output similar to this:
User Machine Machine2
---- ------- -------
White, Scott W107175
Jones, Henry W107195
Flinstone, Fred L109531 W108812