Need help with output Formatting

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

Mirceyhun MusayevSystem AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason CrawfordTransport NinjaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Senior IT System EngineerIT ProfessionalCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.