Retriewing list of local admins from remote system

I am trying to retrieve a list of members of the local administrators group on a given system and have the output of the function be an object such that and I can use it for later consumption and decision making in the script.

I have spent a significant amount of time researching my options and have come up with the following code.  The problem that I am having with the code is that if I run it locally on a system I get the list of the members of the local administrator group.  However, if I run it against a remote system I only get SystemName\Administrator back.

I have used this article as a guide:http://blogs.technet.com/b/heyscriptingguy/archive/2013/12/08/weekend-scripter-who-are-the-administrators.aspx

I must be missing something somewhere.  Can anybody tell me what I need to change in order to get my result set from a remote system to be like I am getting from the local system?

I should add that I am using Windows Server 2012 R2 across the board.

function Get-LocalAdministrators
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory=$true)]
        [String] $computerName
    )
    begin
    {
        $wmiEnumOpts = New-Object System.Management.EnumerationOptions
        $wmiEnumOpts.BlockSize = 20
        $wmiObject = Get-WmiObject -Class Win32_Group -Filter "LocalAccount=True and SID='S-1-5-32-544'" -ComputerName $computerName
        $groupName = $wmiObject.Name
    }
    process
    {
        #$wmiObject
        $wmiObject.GetRelated("Win32_Account","Win32_GroupUser","","","PartComponent","GroupComponent",$false,$wmiEnumOpts) | `
            Select-Object @{Name="ComputerName"; Expression={$_.__SERVER}}, @{Name="Name"; Expression={$groupName}}, @{Name="Member"; Expression={$_.Caption}}
    }
    end
    {
    }
}


April 27th, 2015 3:13pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics