0

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

5
  • You should make the output conditional on the -match operator: if($_.PartComponent -match '...'){ $matches... }else{ Write-Warning "Couldn't extract name from $($_.PartComponent)"} Commented Oct 21, 2020 at 14:22
  • Any specific reason you're not using Get-LocalGroupMember ? Commented Oct 21, 2020 at 14:49
  • Need to search multiple machines at once Commented Oct 21, 2020 at 14:57
  • 1
    As aside, don't use those curly so-called 'smart-quotes' and in code. Instead use straight ones " Commented Oct 21, 2020 at 15:14
  • it happened during copy/paste Commented Oct 21, 2020 at 15:24

2 Answers 2

1

Please, please, please do not use that spaghetti code from TechNet gallery.

A good script is a well written (English) prose.

Get-WmiObject Win32_GroupUser –Computer PlaceYourComputerNameHere | <# Now, you have all users #>
Where-Object {$_.GroupComponent –like '*"Administrators"'} | <# Now, you have all Administrators #>
Where-Object { $_.PartComponent  –match ".+Domain\=""(.+)"",Name\=(.+)$"} | <# Now, you have all domain administrators and you have created two regex groups by (.+) with index 1 and 2 #>
ForEach-Object {"$($Matches[1])\$($Matches[2])"} <# Now, you concatenated the results from regex matches into a single string #>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for tips but it's not answer for my question
1

Figured it out

$admins |% {  
$_.partcomponent –match ".+Domain\=(.+)\,Name\=(.+)$" > $nul 
$matches[1].trim('"') + "\" + $matches[2].trim('"') | Where-Object {$_ -like 'DOMAIN*' }
}

Output:

DOMAIN\Domain Admins
DOMAIN\user1
DOMAIN\User2

1 Comment

BTW: If you look closely on my answer, you would see that you can apply a small change to your regex and you don't need to trim anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.