troubleshooting Question

Need help with output Formatting

Avatar of Mirceyhun Musayev
Mirceyhun Musayev asked on
PowershellScripting Languages
2 Comments1 Solution104 ViewsLast Modified:
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 = $;
    # 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;
Jason Crawford
Transport Ninja

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros