Solved

PowerShell Formatting

Posted on 2012-03-29
13
2,459 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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 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.

856 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