0
votes

When using this code:

$Prodservers = Get-ADComputer -Filter {OperatingSystem -like '*Server*'} -SearchScope Subtree -SearchBase $ProdSB -Server $DCprod -Credential $ProdCred -ErrorAction SilentlyContinue |
               select -Expand DnsHostname 
foreach ($P in $Prodservers) {
    [PSCustomObject]@{
        Hostname = $P
        'Support team' = (Invoke-Command -ComputerName $P -ScriptBlock {$env:supportteam} -Credential $ProdCred) 
        'Local Admins' = (Invoke-Command -ComputerName $P -ScriptBlock {$ADSIComputer = [ADSI]('WinNT://localhost,computer');$lgroup = $ADSIComputer.psbase.children.find('Administrators',  'Group');$lgroup.psbase.invoke('members') | % {$_.GetType().InvokeMember('Name',  'GetProperty',  $null,  $_, $null)}}  -Credential $ProdCred)
        'Host Reachable' = [bool](Invoke-Command -ComputerName $P -ScriptBlock {1} -Credential $ProdCred)
    }
}

This works, however an group membership of more than two members in the local administrators group returns similar to this:

{Administrator, Domain Admins, Prod Server Admin...

How would I expend the output to show the full membership? Also after pointers for selecting only certain groups that match group name x or y or return True is group x is present etc.

2

2 Answers

1
votes

You might be running into output display formatting issues, where the column data exceeds the displayable width in table format in PowerShell.

You can try use the Format-List cmdlet to display things in a list instead to see if your local administrators group with multiple members displays correctly. Check out the link above to see how it helps, but a basic example of using it would be:

Get-Service | Format-List

As for your filtering question, it looks like you're using reflection to invoke methods that collect that data, so it would be harder to use PS cmdlets to help there, so I would suggest getting that data as you do now, but do it separately, into a temporary variable, then filter the data there selecting your specific groups you want using something like this to match your group names, and in the if statement, put the relevant data into another variable, which you then use for your final output.

if ($item -match "groupNameX") { #Then... }
0
votes

Finally worked it out.

Came across this answer.

First, found a script block that outputted the memberships as a PSObject property:

                $SB = {
                $members = net localgroup administrators | 
                where {$_ -AND $_ -notmatch "command completed successfully"} | 
                select -skip 4
                New-Object PSObject -Property @{
                Members=$members
                }
            }

Then modified the local admins column:

'Local Admins' = $admins.Members -join ','

The output is still truncated, however now instead of export-CSV showing the column contents as System.Object[] it now shows the full output with the separator specified in -join.