Author Posts

January 1, 2012 at 12:00 am

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.

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 `
-name "First Name" -value ($userprop.givenname)

$obj | Add-Member -membertype noteproperty `
-name "Last Name" -value ($userprop.surname)

$obj | Add-Member -membertype noteproperty `
-name "Description" -value ($userprop.description)

$obj | Add-Member -membertype noteproperty `
-name "Employee ID" -value ($userprop.EmployeeID)

$obj | Add-Member -membertype noteproperty `
-name "Account Status" -value ($userprop.enabled)

$obj | Add-Member -membertype noteproperty `
-name "B1 ID" -value ($userprop.name)

$obj | Add-Member -membertype noteproperty `
-name "OU" -value ($userprop.CanonicalName)

$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

by RichardSiddaway at 2013-01-12 01:56:04

Try something like this

$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

by maindave at 2013-01-15 13:10:29

Richard
thx for your asisstance