Created
January 16, 2018 16:55
-
-
Save tvories/d8cf87b7e0eef2caa2d4143f6721a135 to your computer and use it in GitHub Desktop.
Get-LocalAdministrators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-LocalAdministrators { | |
param ([string]$computername=$env:computername, | |
[System.Management.Automation.CredentialAttribute()]$Credential | |
) | |
$computername = $computername.toupper() | |
$ADMINS = get-wmiobject -computername $computername -Credential $Credential -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent} | |
foreach ($ADMIN in $ADMINS) { | |
$admin = $admin.replace("\\$computername\root\cimv2:Win32_UserAccount.Domain=","") # trims the results for a user | |
$admin = $admin.replace("\\$computername\root\cimv2:Win32_Group.Domain=","") # trims the results for a group | |
$admin = $admin.replace('Name="',"") | |
$admin = $admin.Replace("`"","") | |
$admin = $admin.split(",") | |
$objOutput = New-Object PSObject -Property @{ | |
Machinename = $computername | |
Fullname = ("$($admin[0])\$($admin[1])") | |
DomainName =$admin[0] | |
UserName = $admin[1] | |
}#end object | |
$objreport+=@($objoutput) | |
}#end for | |
return $objreport | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment