I need to get local admins on remote machine but need to show only domain users (Users with domain\prefix)
$admins = Gwmi win32_groupuser –computer computer
$admins = $admins |? {$_.groupcomponent –like '*"Administrators"'}
$admins |% {
$_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul
$matches[1].trim('"') + “\” + $matches[2].trim('"')
}
$admins = $admins |? {$_.groupcomponent –like '*"Administrators"'}
GroupComponent : \\COMPUTER\root\cimv2:Win32_Group.Domain="COMPUTER",Name="Administrators"
PartComponent : \\COMPUTER\root\cimv2:Win32_Group.Domain="DOMAIN",Name="Domain Admins"
PSComputerName : COMPUTER
Current Output:
COMPUTER\Administrator
DOMAIN\Domain Admins
DOMAIN\User1
DOMAIN\user2
I need to show only domain users
Desired Output:
DOMAIN\Domain Admins
DOMAIN\User1
DOMAIN\user2
I tried this:
$admins |% {
$_.partcomponent –match “.+Domain\='DOMAIN\',Name\=(.+)$” > $nul
$matches[1].trim('"') + “\” + $matches[2].trim('"')
}
But single user is shown multiple times
DOMAIN\User1
DOMAIN\User1
DOMAIN\user1
-matchoperator:if($_.PartComponent -match '...'){ $matches... }else{ Write-Warning "Couldn't extract name from $($_.PartComponent)"}Get-LocalGroupMember?“and”in code. Instead use straight ones"