So I was task with writing a script that would enumerate all the groups in an OU along with any nested groups and output to a .csv with column header that read AD User, AD Group and Timestamp. The timestamp is simply a get-date that take a timestamp each time the script is run. These all needed to be written using the new R2 AD cmdlets, not Quest cmdlets. I wrote this which will give me all the data;
$OU = "OU=UserOU,OU=BaseOU,DC=fabrikam,DC=com"
$TimeStamp
= get-date
Get-ADGroup -ldapfilter
"(cn=*)" -SearchBase $OU -searchscope subtree -properties members | foreach {
$GroupName
= $_.name
Get-ADGroupMember
$_.DistinguishedName -recursive | foreach {
$_
.SamAccountName
}
}
| Export-csv c:\ScriptOutput\users.csv -NoTypeInformation
This will give tme all the users in the groups (and nested groups) contained in the target OU, but how can I organize the .csv file with the appropriate columns? I know that a custom object with a hash table is probably the best option, something like this:
|
foreach {
$UserInfo
= New-Object PSObject -Property @{
ADUserName
= $user.name
ADGroupName
= $user.memberof
TimeStamp
= $TimeStamp
}}|
Sort-Object ADUsername
But I don;t know how to tie it all together.