I want to gather not only attributes of a bunch of Users but also gather up their memberOf, expanded out in the same select statement, and output all to a csv
I have this
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"
$Properties =
@(
'samAccountName'
'DistinguishedName'
'Manager'
'MemberOf'
'Name = 'memberOf'; Expression = { $_.MemberOf -Property -expandProperty MemberOf}'
'Title'
foreach ($MacUser in $MacUsers) {
$MacUser = $MacUser
Get-ADUser -identity $MacUser -Properties $Properties |
select $Properties |
Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -Append -Force -NoTypeInformation
}
but get error: “Unexpected token ‘memberOf’”
How can I collect into a csv the whole kittenkaboodle?
thank you
Olaf
September 22, 2021, 7:45pm
#2
If I got it right something like this should work:
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"
$Properties = @(
'samAccountName'
'DistinguishedName'
'Manager'
@{Name = 'MemberOf'; Expression = { $_.MemberOf -join ';'}}
'Title'
)
$Result =
foreach ($MacUser in $MacUsers) {
Get-ADUser -identity $MacUser -Properties Manager,MemberOf,Title |
Select-Object -Property $Properties
}
$Result |
Export-Csv -Path 'D:\MacOS\AllMacUsers.csv' -NoTypeInformation
1 Like
Same script as Olaf’s just rewritten to use the pipeline. The main issue I wrote this to “solve” is the possible repeat opening/closing of the CSV.
$Properties = @(
'samAccountName'
'DistinguishedName'
'Manager'
'MemberOf'
@{Name = 'memberOf'; Expression = { $_.MemberOf -join ';'}}
'Title'
)
Import-Csv -Path "D:\MacOsUsers.csv" | ForEach-Object {
Get-ADUser -identity $_."Last User" -Properties * |
Select-Object -Property $Properties
} | Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -NoTypeInformation -Force
There are a number of ways to solve it without using the pipeline as well. Such as just collecting all the output first
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"
$Properties = @(
'samAccountName'
'DistinguishedName'
'Manager'
'MemberOf'
@{Name = 'memberOf'; Expression = { $_.MemberOf -join ';'}}
'Title'
)
$output = foreach ($MacUser in $MacUsers) {
Get-ADUser -identity $MacUser -Properties * |
Select-Object -Property $Properties
}
$output | Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -Force -NoTypeInformation
As always, great answer Olaf.
1 Like
Testing your code Olaf I get csv and good values in all but the MemberOf column
MemberOf
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Same for Doug’s…good data, save for the MemberOf Column
Olaf
September 22, 2021, 9:50pm
#6
jeff-taylor:
Testing your code Olaf
… hmmm … changed my code suggestion from above slightly … .try it now.
Olaf:
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"
$Properties = @(
'samAccountName'
'DistinguishedName'
'Manager'
@{Name = 'MemberOf'; Expression = { $_.MemberOf -join ';'}}
'Title'
)
$Result =
foreach ($MacUser in $MacUsers) {
Get-ADUser -identity $MacUser -Properties Manager,MemberOf,Title |
Select-Object -Property $Properties
}
$Result |
Export-Csv -Path 'D:\MacOS\AllMacUsers.csv' -NoTypeInformation
That’s working for the MemberOfs now thank you…but if I want to add more extensionAttributes (say, 15 more) to that $Properties variable, I was hoping to get those too (without adding them individually to $MacUser -Properties Manager,MemberOf, Title, etc etc)
Olaf
September 22, 2021, 11:40pm
#8
jeff-taylor:
but if I want to add more extensionAttributes (say, 15 more) to that $Properties variable, I was hoping to get those too (without adding them individually to $MacUser -Properties Manager,MemberOf, Title, etc etc)
Most of the time we try to minimize the stress you put on your AD by specifying the properties you’re after explicitly. But if you really need 15 more of them - what sounds a kind of really weird to me - you may use the asterisk ( * ) instead.