question

RoelKnippen-6675 avatar image
0 Votes"
RoelKnippen-6675 asked RichMatheisen-8856 edited

Show Vallue when Member is $Null

Hi,

Running in a small challange when running the following part of the script
$Groups = Get-ADGroup -Filter * -SearchBase $SearchbaseShare -Properties Name, members
$Results = foreach( $Group in $Groups ){
Get-ADGroupMember -Identity $Group |
ForEach-Object{ [pscustomobject]@{
GroupName = $Group.Name
Name = $_.Name}
}}
When running the script I get the data of group wich have a member. But the groups in the searchbase wich don't have a member aren't showwing up.

Hopefully someone van put me in a direction to solve this.

Thank's in advance

Roel Knippen

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

How about something like this?

 $Groups = Get-ADGroup -Filter * -SearchBase $SearchbaseShare -Properties Name, members
 $Results = foreach ( $Group in $Groups ) {
     [array]$m = Get-ADGroupMember -Identity $Group
     if ($m.count -gt 0){
         $m |
             ForEach-Object { [pscustomobject]@{
                     GroupName = $Group.Name
                     Name      = $_.Name
                 }
             }
     else{
         [pscustomobject]@{
             GroupName = $Group.Name
             Name      = "EMPTY"
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RoelKnippen-6675 avatar image
0 Votes"
RoelKnippen-6675 answered RichMatheisen-8856 edited

Hi,

Dis some testing and ran into the following error:

else : 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 if a path was included, verify that the path is correct and try again.

 $Groups = Get-ADGroup -Filter * -SearchBase $SearchbaseShare -Properties Name, members
 $Results = foreach( $Group in $Groups ){
     [array] $Member = Get-ADGroupMember -Identity $Group 
     if ($Member.Count -gt 0) {
     $Member |    
     ForEach-Object { [pscustomobject]@{
         GroupName = $Group.Name     
         Name = $_.Name} 
     } 
     Else {
         [pscustomobject]@{
             GroupName = $Group.Name     
             Name = "Empty" }
          }
     }
 }

Hope you can help me solve the issue

Thanks in advanced
Roel

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There's not much to "solve". You're missing the closing bracket ("}")on line 10 of the example I submitted.

If you use the customary indentation in your code it would make problems like this easier to find. Even better, use an IDE like VS Code or Visual Studio (there are others, too) that will point out the presence of mismatched brackets, parenthesis, and quotation marks (among many other common coding errors).

0 Votes 0 ·