Solved

PowerShell Formatting

Posted on 2012-03-29
13
2,467 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
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 …

688 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