Solved

PowerShell Formatting

Posted on 2012-03-29
13
2,462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

730 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