header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Using new 2008 R2 AD cmdlets to eumerate group membership and create custom object to organize
Last Post 08 Jun 2010 09:07 AM by ebrown35. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
ebrown35User is Offline
New Member
New Member
Posts:30
Avatar

--
29 Apr 2010 01:01 PM

    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.

    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    08 Jun 2010 08:09 AM
    Nice script...Use the select statement to tie it all together.  I've tested this in my domain and worked very nicely.  Here is how I used the select statement.

    $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 |
    Select SamAccountName,@{n="GroupName";e={$GroupName}},@{n='TimeStamp';e={$TimeStamp}}
    }|
    Export-csv gpusers.csv -NoTypeInformation

    ebrown35User is Offline
    New Member
    New Member
    Posts:30
    Avatar

    --
    08 Jun 2010 09:07 AM
    tha works too, but i ended up doing with the hash table and it works great:



    $OU = "OU=UserOU,OU=BaseOU,DC=fabrikam,DC=com"
    $timeStamp = get-date
    $dataArray = @()

    Get-ADGroup -ldapfilter "(cn=*)" -SearchBase $OU -searchscope subtree -properties members | foreach {
    $groupName = $_.name
    Get-ADGroupMember $_.DistinguishedName -recursive | foreach {

    $UserInfo = New-Object PSObject -Property @{
    ADUserName = $_.SamAccountName
    ADGroupName = $groupName
    TimeStamp = $timeStamp
    }
    $dataArray += $userinfo
    }
    }

    $dataArray| Export-csv c:\ScriptOutput\msusers.csv -NoTypeInformation
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer