header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Cutom objects, hashtables and such
Last Post 07 Apr 2012 12:04 AM by RiffyRiot. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
LukeUser is Offline
New Member
New Member
Posts:1
Avatar

--
06 Apr 2012 09:54 AM

    Brief explanation:

    I manage an AD, and from time to time I need to pull the users from specific groups for audit compliance. I'd like to be able to run a script that dumps out a CSV that I can then email to those who need it. I'd like the table to look a little like this:

    Group1                           Group2                                    Group3
    ---------                           ---------                                    ----------
    James                             Jon                                          Ralph
    Don                                Daniel                                      Carroll
    Carroll                            Paul
    Paul                                Donald
                                          Mark
                                          Luke

    I've managed to pull this off pretty effectively by directly writing it to an Excel file:

    $Excel = New-Object -ComObject Excel.Application $Excel.visible = $True $Excel = $Excel.Workbooks.Add() $Sheet = $Excel.WorkSheets.Item(1) $Sheet.Cells.Item(1,1) = "User Revalidation" $WorkBook = $Sheet.UsedRange $WorkBook.Interior.ColorIndex = 8 $WorkBook.Font.ColorIndex = 11 $WorkBook.Font.Bold = $True $List = Get-Content groups.txt $Groups = $List | Get-QADGroup $intRow = 2 $intCol = 1 ForEach ($item in $Groups) { $Sheet.Cells.Item($intRow,$intCol) = $item.name $member = Get-QADGroupMember $item $intRow = $intRow +1     foreach ($item2 in $member)     {        $Sheet.Cells.Item($intRow,$intCol) = $item2.name        $intRow = $intRow +1     } $intRow = 2 $intCol = $intCol +1 } $WorkBook = $Sheet.UsedRange $WorkBook.EntireColumn.AutoFit() Clear

    One way to solve a problem never satisfies me. Is there a way to pull this off using custom objects so I can just export directly to the CSV?

    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:369
    Avatar

    --
    06 Apr 2012 03:11 PM

    CSV is not a good way to store the data the way you want.

    You could create objects with MemberName and GroupName properties and then create a CSV file that looks like this:

    MemberName,GroupName
    James,Group1
    Don,Group1
    Carroll,Group1
    Paul,Group1
    Jon,Group2
    Daniel,Group2
    Paul,Group2
    Donald,Group2
    Mark,Group2
    Luke,Group2
    Ralph,Group3
    Carroll,Group3
    

    Or using pretty formatting

    MemberName   GroupName
    ----------   ---------
    James        Group1
    Don          Group1
    Carroll      Group1
    Paul         Group1
    Jon          Group2
    Daniel       Group2
    Paul         Group2
    Donald       Group2
    Mark         Group2
    Luke         Group2
    Ralph        Group3
    Carroll      Group3
    

    To get column names to be the group names you would need to

    1. create objects that have the all the group names as properties
    2. each object would contain 3 unrelated people (one per group)
    3. when you run out of people for a given group you need to stop putting people in that group's property but still need to create a new object because at least one of the remaining groups will have a member

    Ugh! I'm having a hard time explaining what it would take to format it the way you want to show it.

    In any case, most of your code is excel management code or formatting and a CSV has neither of those. Perhaps it is best to keep your excel code.

    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    07 Apr 2012 12:04 AM
    This should get you started... I just used the csv from the Optikghost post to get the raw data
    You'll need to modify it to get your AD data

    $myresult=@()
    $mygroups=import-csv d:\groups.csv

    $groups=($mygroups | Select GroupName -unique)
    [int]$maxcount=($mygroups | Group GroupName | measure count -max).maximum
    for($i=0;$i -lt $maxcount;$i++){
    $expr='$myresult+=new-object PSObject -Property @{'
    for($j=0;$j -lt $groups.count;$j++){
    $thegroup=($groups[$j] | Select -expand GroupName)
    $themember=($mygroups | ?{$_.GroupName -eq $thegroup})[$i] | Select -expand MemberName
    $expr+="'$($thegroup)'='$($themember)';"
    }
    $expr+='}'
    invoke-expression $expr
    }

    $myresult | ft -auto

    Result:

    Group2 Group1 Group3
    ------ ------ ------
    Jon James Ralph
    Daniel Don Carroll
    Paul Carroll
    Donald Paul
    Mark
    Luke
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer