Solved

PowerShell Formatting

Posted on 2012-03-29
13
2,457 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:Brent Challis
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html) provided 218 attendees with a step-by-step guide for identifying Acti…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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