Avatar of Mirceyhun Musayev
Mirceyhun Musayev

asked on 

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

PowershellScripting Languages

Avatar of undefined
Last Comment
Albert Widjaja
ASKER CERTIFIED SOLUTION
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Albert Widjaja
Albert Widjaja
Flag of Australia image

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

Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo