PowerShell Local Admin Group Membership

                
<# 
.SYNOPSIS     
    Script to get accounts in a local group (Administrators)
.DESCRIPTION 
    This can be used to query some computers and get local group membership
.EXAMPLE  
    Run the script against C:\Temp\Accounts.txt list of computers 
    Enter the name of the local admin group to check
    Results are exported to file and can be cleaned up in Excel 
    Use Text to Colums to Split the data into rows 
#>

Remove-Job *

$MyCommand={
  # Paramater for inbound Data
  Param($PassedArray) 
  # Array
  $MyArray=@()
  # Get Data 
  $group=get-wmiobject win32_group -filter "LocalAccount='True' and Name='$PassedArray'"
  $MyArray=$group.GetRelated().Name 
  # Return Data
  Return "$Env:Computername `n $MyArray"
}

#-------------------------------
# Get Static List of Computers 
#-------------------------------
$MyComputers=Get-Content C:\Temp\Accounts.txt

#Declare a Array
$MyComputersPassWinRM=@()

#---------------------------------
# Test Access to Systems
#---------------------------------
Foreach ($Comp in $MyComputers)
{
    if ((Test-Connection -ErrorAction SilentlyContinue –ComputerName $Comp –Quiet –Count 1) –and ((Invoke-Command -ErrorAction SilentlyContinue –ComputerName $comp –ScriptBlock { 1 }) –eq 1))
    {
        $MyComputersPassWinRM+=$Comp
    }
}

#-------------------------
# Input of data from user
#-------------------------
$PassedArray=Read-Host "Enter Local Group Name"

#--------------------------
# Invoke Command on Systems 
#--------------------------
$JobID=Invoke-Command $MyComputersPassWinRM -ScriptBlock $MyCommand -ArgumentList @(,$PassedArray) -AsJob

#---------------------
# Get Data From Job 
#---------------------
Receive-Job -id $JobID.ID -Wait

2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator