Skip to main content

Create a list of local administrators with PowerShell



1.       Create folder :    C:\Temp
2.       Save a text file with the name servers.txt and add server names
3.       Create a .PS1 file with the name “ Get-LocalGroupMembers.ps1 ”with below PowerShell code:

**********************************************************   
 [CmdletBinding()]
Param(
    [Parameter(    ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true
                )]
    [string[]]
    $ComputerName = $env:ComputerName,

    [Parameter()]
    [string]
    $LocalGroupName = "Administrators",

    [Parameter()]
    [string]
    $OutputDir = "c:\temp"
)

Begin {

    $OutputFile = Join-Path $OutputDir "LocalGroupMembers.csv"
    Write-Verbose "Script will write the output to $OutputFile folder"
    Add-Content -Path $OutPutFile -Value "ComputerName, LocalGroupName, Status, MemberType, MemberDomain, MemberName"
}

Process {
    ForEach($Computer in $ComputerName) {
        Write-host "Working on $Computer"
        If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
            Write-Verbose "$Computer is offline. Proceeding with next computer"
            Add-Content -Path $OutputFile -Value "$Computer,$LocalGroupName,Offline"
            Continue
        } else {
            Write-Verbose "Working on $computer"
            try {
                $group = [ADSI]"WinNT://$Computer/$LocalGroupName"
                $members = @($group.Invoke("Members"))
                Write-Verbose "Successfully queries the members of $computer"
                if(!$members) {
                    Add-Content -Path $OutputFile -Value "$Computer,$LocalGroupName,NoMembersFound"
                    Write-Verbose "No members found in the group"
                    continue
                }
            }       
            catch {
                Write-Verbose "Failed to query the members of $computer"
                Add-Content -Path $OutputFile -Value "$Computer,,FailedToQuery"
                Continue
            }
            foreach($member in $members) {
                try {
                    $MemberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)
                    $MemberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)
                    $MemberPath = $member.GetType().Invokemember("ADSPath","GetProperty",$null,$member,$null)
                    $MemberDomain = $null
                    if($MemberPath -match "^Winnt\:\/\/(?\S+)\/(?\S+)\/") {
                        if($MemberType -eq "User") {
                            $MemberType = "LocalUser"
                        } elseif($MemberType -eq "Group"){
                            $MemberType = "LocalGroup"
                        }
                        $MemberDomain = $matches["CompName"]

                    } elseif($MemberPath -match "^WinNT\:\/\/(?\S+)/") {
                        if($MemberType -eq "User") {
                            $MemberType = "DomainUser"
                        } elseif($MemberType -eq "Group"){
                            $MemberType = "DomainGroup"
                        }
                        $MemberDomain = $matches["domainname"]

                    } else {
                        $MemberType = "Unknown"
                        $MemberDomain = "Unknown"
                    }
                Add-Content -Path $OutPutFile -Value "$Computer, $LocalGroupName, SUCCESS, $MemberType, $MemberDomain, $MemberName"
                } catch {
                    Write-Verbose "failed to query details of a member. Details $_"
                    Add-Content -Path $OutputFile -Value "$Computer,,FailedQueryMember"
                }

            }
        }

    }

}
End {}


***********************************************************
4.       Open PowerShell and point to Get-LocalGroupMembers.ps1 and use below commands to get the results.
.\Get-LocalGroupMembers.PS1 -ComputerName (Get-Content C:\temp\servers.txt)

Results will be saved as CSV file and at C:\Temp with the name “LocalGroupMembers.csv”



Source:  https://4sysops.com/archives/create-a-list-of-local-administrators-with-powershell/


Comments

Popular posts from this blog

How to configure.NET Framework 3.5 on Windows 8

If  an  utility  requires the .NET framework 3.5, however would not in finding that  version enabled  in your computer, it shows the following message box in Windows 8, both all the way through set up, or while you open the applying for the first time. In the message box, choose Install this feature to enable the .NET Framework 3.5. This option requires an Internet connection. If in case you have ever tried to update the . net   frame work  3.5 from windows features you will get 0x800F0906 or 0x800F081F error . In this article I am showing How to fix . net  framework 3.5 installation in windows 8 or 8.1 error code 0x800f0906  or 0x800F081F.      . NET Framework 4.5 is enabled by default in Windows 8 and Windows 8.1. Windows 8 & Windows 8.1 does not include .NET 3.5 by default, but the files for .NET 3.5 are available on the Wind...

test vpn bandwidth and speed with iperf

This article explains how to use a free utility called iPerf to test the speed of a VPN connection. In this example I am running iPerf on windows but there are other versions available (i.e. Linux). Download iPerf from  http://linhost.info/2010/02/iperf-on-windows/ Put a copy on 2 computers with 1 either side of the VPN. In this example I have put in the c:\triangle folder On the “server” PC open a cmd prompt and navigate to the folder containing iperf. Note on computers running Win7 or Win2008 I recommend running the cmd prompt in elevated mode. Run the command  iperf –s On the “client” PC open a cmd prompt and again navigate to the folder containing iperf. Run the command  iperf –c After a short while the estimated bandwidth is displayed.

Installing Nagios 4 on Ubuntu 14 04 LTS