Need help with output Formatting

Mirceyhun Musayev
Mirceyhun Musayev used Ask the Experts™
on
I have a script which scans all servers in AD and returns local group members. Can you help me to update script so that output format will be grouped by server with heading – Server name, Local Group Name, Members, Type?

 
# Define local groups to audit
$groups = "Administrators", "Remote Desktop Users";
 
# Add Active Directory powershell plug-in
import-module activedirectory;
 
# Get all servers from AD and ignore predefined list 
$adservers = get-adcomputer -filter {operatingsystem -like "*server*"} | where {$_.enabled -eq $true} | sort name;
 
# Loop through each server found in AD
foreach ($adserver in $adservers) {
 
    # Set server name from AD object
    $servername = $adserver.name;
 
    # Check if server is pingable
    if((test-connection -computername $servername -count 1 -quiet)) {
 
        # Loop through each group to audit
        foreach ($group in $groups) {
 
            # Define the localgroup in the correct format
            $localgroup = [ADSI]"WinNT://$servername/$group";
 
            # Get members of the local group
            $members = @($localgroup.Invoke("Members"));
 
            # Loop through each member found
            foreach ($member in $members) {
 
                # Define name and type of the member
                $memberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null);
                $memberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null);
 
                # Build CSV string
                $outstring = $servername + "," + $group + "," +$membername + "," +$membertype;
                # Output string to screen
                write-host $outstring;
 
                # Append CSV string to file
                $outstring >> c:\temp\localgroupaudit.csv;
            }
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try using a PSObject instead:

 # Define local groups to audit
$groups = "Administrators", "Remote Desktop Users"
 
# Add Active Directory powershell plug-in
import-module activedirectory
 
# Get all servers from AD and ignore predefined list 
$adservers = get-adcomputer -filter {operatingsystem -like "*server*"} | where {$_.enabled -eq $true}
 
# Loop through each server found in AD
foreach ($adserver in $adservers) {
 
    # Set server name from AD object
    $servername = $adserver.name
 
    # Check if server is pingable
    if((test-connection -computername $servername -count 1 -quiet)) {
 
        # Loop through each group to audit
        foreach ($group in $groups) {
 
            # Define the localgroup in the correct format
            $localgroup = [ADSI]"WinNT://$servername/$group"
 
            # Get members of the local group
            $members = @($localgroup.Invoke("Members"))
 
            # Loop through each member found
            foreach ($member in $members) {
 
                # Define name and type of the member
                $memberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)
                $memberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)
                
                [array]$results += New-Object -TypeName PSObject -Property @{
                    'Server name' = $servername
                    'Local Group Name' = $localgroup
                    'Member' = $memberName
                    'Type' = $memberType
                }
            }
        }
    }
}

$results | Export-Csv C:\Temp\localgroupaudit.csv -NoTypeInformation

Open in new window

Commented:
The script returns Error:

The following exception occurred while retrieving member "Invoke": "The group name could not be found.
"
At line:26 char:13
+             $members = @($localgroup.Invoke("Members"))
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember

The variable '$results' cannot be retrieved because it has not been set.
At line:35 char:24
+                 [array]$results += New-Object -TypeName PSObject -Pro ...
+                        ~~~~~~~~
    + CategoryInfo          : InvalidOperation: (results:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial