The following function uses the ADSI provider and some tricks to determine the members of the local Administrators group on any of your Windows Servers. Check it out:

function Get-LocalAdmins
{
param([string]$serverName)
 $ErrorActionPreference = “Stop”
 If ($serverName -eq “”)
 {
  Write-Host “Usage: Get-LocalAdmins “`
  -ForegroundColor yellow
 }
 Else
 {
 $Group = [ADSI]“WinNT://$serverName/Administrators”
 $Memberlist = @($Group.PSBase.Invoke(“Members”))
 $Members = $Memberlist |
  %{$_.GetType().InvokeMember(“Name”,’GetProperty’,$null,$_,$null)}
 Return $Members
 }
}

You use it simply by typing Get-LocalAdmins MYSERVER. Ain’t that neat?

No related posts.

  4 Responses to “Helpful Function of the Day: Get-LocalAdmins”

  1. Hi,

    I am new to the scripting.Could you please let me know how to run this function?

    Thanks,
    Sumit

  2. 1. Save it as Get-LocalAdmins.ps1
    2. Start Powershell
    3. Dot-source the script by typing the full path to the script, preceeded by a dot and a space
    4. Run Get-LocalAdmins YOURSERVERNAME
    Hugo

  3. Thanks…it works…

  4. UPDATE: Be advised that this command might generate warning events in the System Event Log with Event-ID 2510: The server service was unable to map error code 1722. Other sources on the internet will tell you this error code means The RPC Server was unavailable. I believe this error message is benign. If anybody can give more insight on this, please add a comment.
    Hugo

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2012 PeetersOnline Suffusion theme by Sayontan Sinha