powershell - New-object, property "disappears" -
i'm trying make script returning ad users id, display name , group memberships based on input file , export result file.
however, group membership information seems lost in process.
any ideas?
my current script:
$result = @() foreach ($_ in gc userlist.csv) { $user = get-aduser $_ -properties displayname, samaccountname, lastlogondate | select displayname, samaccountname, lastlogondate $groups = get-adprincipalgroupmembership $_ | select name # far seems work $properties = @{ userid = (@($user.samaccountname) -join ',') name = (@($user.displayname) -join ',') lastlogondate = (@($user.lastlogondate) -join ',') groups = (@($groups.name) -join ',') } # time, groups doesn't return information $result += new-object psobject -property $properties } $result | select-object name, userid, groups, lastlogondate | export-csv -notypeinformation -path output.csv
here's think might work:
$result = @() foreach ($user in gc userlist.csv) { $userdetails = get-aduser $user -properties displayname, samaccountname, lastlogondate | select displayname, samaccountname, lastlogondate $groups = get-adprincipalgroupmembership $user | select name $properties = @{ userid = $userdetails.samaccountname name = $userdetails.displayname lastlogondate = $userdetails.lastlogondate groups = ($groups.name -join '; ') } $result += new-object psobject -property $properties } $result | select-object name, userid, groups, lastlogondate | export-csv -notypeinformation -path output.csv
- per comment, although using
$_
work not particularly practice manually set variable i've changed $user. - i've therefore changed $user $userdetails.
- i removed casting array
@()
each of properties joining them,
. i'm not sure why necessary, except perhapsgroups
property, in order not confuse csv result, i've joined these;
instead.
Comments
Post a Comment