We help IT Professionals succeed at work.
Get Started

Need help with output Formatting

Last Modified: 2018-12-14
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

Watch Question
Transport Ninja
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE