by maindave at 2013-01-11 11:44:49
Hi, I have written a script that output certian AD objects for a AD user account, next i’m retrieving groups AD user is a member of. The problem I’m having is getting the group membership to export as CSV.by RichardSiddaway at 2013-01-12 01:56:04
function get-userprop {
param (
$user = " "
)
$userprop=Get-ADUser $user -properties * | sort name
$usermem=(Get-ADUser $user -properties * | select memberof).memberof
$obj = New-Object -typename psobject
$obj | Add-Member -membertype noteproperty<br> -name "First Name" -value ($userprop.givenname)<br> <br> $obj | Add-Member -membertype noteproperty
-name "Last Name" -value ($userprop.surname)
$obj | Add-Member -membertype noteproperty<br> -name "Description" -value ($userprop.description)<br> <br> $obj | Add-Member -membertype noteproperty
-name "Employee ID" -value ($userprop.EmployeeID)
$obj | Add-Member -membertype noteproperty<br> -name "Account Status" -value ($userprop.enabled) <br> <br> $obj | Add-Member -membertype noteproperty
-name "B1 ID" -value ($userprop.name)
$obj | Add-Member -membertype noteproperty<br> -name "OU" -value ($userprop.CanonicalName) <br> <br> $obj | Add-Member -membertype noteproperty
-name "Expiration Date" -value ($userprop.AccountExpirationDate)
$obj | Add-Member -membertype noteproperty `
-name "Group Membership" -value ($usermem)
Write-Output $obj
}
get-userprop
OUTPUT******** misssing group membership**************
First Name Last Name Description Employee ID Account Status B1 ID OU Expiration Date
XXX xxxxxx xxxxxx xxxxxx xxxxx xxx xx xxxx
Try something like thisby maindave at 2013-01-15 13:10:29$user = "mgreen"
Get-ADUser $user -properties * |
select @{N="FirstName"; E={$.givenname}},
@{N="LastName"; E={$.surname}},
Description, EmployeeID,
@{N="AccountStatus"; E={$.enabled}},
@{N="B1 ID"; E={$.name}},
@{N="OU"; E={$.CanonicalName}},
@{N="Expiration Date"; E={$.AccountExpirationDate}},
@{N="Group Membership"; E={$($_.memberof)}} |
Export-Csv -Path userdata.csv -NoTypeInformation
I'm creating an object using Select-Object. N = Name and E=Expression
Its a simpler way and a lot less typing. I get the group membership as a list in the last field of the CSV
Richard
thx for your asisstance