Solved

PowerShell Formatting

Posted on 2012-03-29
13
2,446 Views
Last Modified: 2012-03-29
I have written the below powershell script which does the job I want it to but the user who requires the report wants it in a different format. This is what I am having trouble with.

The code outputs like below:

TestGroups 1
  User1
  User2
  User3

TestGroups 2
  User2
  User4

But the format required is:

TestGroups 1 , User1
TestGroups 1 , User2
TestGroups 1 , User3
TestGroups 2 , User2
TestGroups 2 , User4

Can someone please point me in the right direction..

Thanks Rod




$ReportName = "c:\admin\powershell\membership.csv"
del $ReportName
$GroupSearchString = "TestGroups*"

Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString } | ForEach-Object {
  ($_.Name)| out-file -append $ReportName
  ""
  Get-ADGroupMember $_.Name -Recursive | ForEach-Object { "  $($_.Name)"| out-file -append $ReportName }
  ""
}

Open in new window

0
Comment
Question by:Rodders1000
  • 7
  • 5
13 Comments
 
LVL 8

Expert Comment

by:bchallis
ID: 37781277
Try
$ReportName = "c:\admin\powershell\membership.csv"
del $ReportName
$GroupSearchString = "TestGroups*"

$groups = Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString }

foreach ($group in $groups)
{
    $members = Get-ADGroupMember $group.Name -Recursive
    foreach ($member in $members)
    {
        ($group.Name + "," + $member.Name) |  out-file -append $ReportName
    }
}
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37781289
Try to change this line

Get-ADGroupMember $_.Name -Recursive | ForEach-Object { "  $($_.Name)"| out-file -append $ReportName }
 

To this


(Get-ADGroupMember $group -Recursive | ForEach-Object { "  $($_.Name)"}) -join ","| out-file -append $ReportName }
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37781294
typo

should be

(Get-ADGroupMember $($_.Name) -Recursive | ForEach-Object { "  $($_.Name)"}) -join ","| out-file -append $ReportName }
0
 

Author Comment

by:Rodders1000
ID: 37781626
Thanks but the Join only joins the members names.
What I am really after is Group Name, Members Name on the same line.
One line for each member.

I did have to remove the brackets and $ from the first name to get it to work. (Not sure if that is why it doesnt do what I asked though.


(Get-ADGroupMember $_.Name -Recursive | ForEach-Object { "  $($_.Name)"}) -join ","| out-file -append $ReportName }
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37781803
ok, try this

Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString } | ForEach-Object {
$GroupName = $_.Name
$Members = Get-ADGroupMember $_.name -Recursive | foreach{$_.Name}
$Out = $GroupName + "," + [String]::Join(",", $Members)
$out | Out-File -append $ReportName
}
0
 

Author Comment

by:Rodders1000
ID: 37781889
That's a lot closer- Maybe better than what was required.
Only thing it does not handle empty groups, it just errors for them. Is there a way around this ?


Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\admin\powershell\grouplist.ps1:10 char:41
+ $Out = $GroupName + "," + [String]::Join <<<< (",", $Members)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37781906
ok, lets try to add a IF statement and count the members

Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString } | ForEach-Object {
$GroupName = $_.Name
$Members = Get-ADGroupMember $_.name -Recursive | foreach{$_.Name}
If ($Members.count -ge 1){
    $Out = $GroupName + "," + [String]::Join(",", $Members)
    $out | Out-File -append $ReportName
}
}
0
 

Author Comment

by:Rodders1000
ID: 37781955
That is fab. Currently as instructed it wont process a blank group.
How do we put in an else statement so that if the group is blank then only the groupname is written.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37781968
If ($Members.count -ge 1){
    $Out = $GroupName + "," + [String]::Join(",", $Members)
    $out | Out-File -append $ReportName
}
Else{
$Out = $GroupName
$out | Out-File -append $ReportName
}
}
0
 

Author Comment

by:Rodders1000
ID: 37782078
I think the Else is bombing out :(

Get-ADGroup : The server has returned the following error: invalid enumeration context.
At C:\admin\powershell\grouplist.ps1:7 char:12
+ Get-ADGroup <<<<  -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString } | ForEach
-Object {
    + CategoryInfo          : NotSpecified: (:) [Get-ADGroup], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Manageme
   nt.Commands.GetADGroup

The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or i
f a path was included, verify that the path is correct and try again.
At C:\admin\powershell\grouplist.ps1:15 char:5
+ Else <<<< {
    + CategoryInfo          : ObjectNotFound: (Else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 37782181
i have tested and it seems to work. Here is all the code, maybe there was a typo



Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -ne "DomainLocal" -and Name -Like $GroupSearchString } | ForEach-Object {
$GroupName = $_.Name 
$Members = Get-ADGroupMember $_.samaccountname -Recursive | foreach{$_.Name} 
    If ($Members.count -ge 1){
        $Out = $GroupName + "," + [String]::Join(",", $Members)
        $out | Out-File -append $ReportName
    }
    Else{
        $Out = $GroupName
        $out | Out-File -append $ReportName  
    }
}

Open in new window

0
 

Author Comment

by:Rodders1000
ID: 37782208
I had an extra squiggly bracket. This works magic thanks for all your help. I also learnt alot about piping today aswell.

Thanks again Rod
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 37782216
your welcome, glad it worked.
0

Join & Write a Comment

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now