1

I have a function:

function getlocaladmin {  
param ($strcomputer)  
  
$admins = Gwmi win32_groupuser –computer $strcomputer   
$admins = $admins |? {$_.groupcomponent –like '*"Administrators"'}  
  
$admins |% {  
$_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul  
$matches[1].trim('"') + “\” + $matches[2].trim('"')  
}  
}

Doing 'getlocaladmin computer name' is going to take way too long.

I tried:

Get-ADComputer -filter * |

Foreach-Object {

 $function:getlocaladmin = $using:funcDef 
 getlocaladmin $_.name
 } |
 Export-Csv -Path .\localadmins.Csv

It... did not like that. What can I do to pass it a list of all the computers we have?

  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even _have_ them anymore. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Aug 05 '20 at 17:23

1 Answers1

0

You need to change the function first:

function getlocaladmin {  
param ([string[]]$strcomputer)  
  
$admins = Gwmi win32_groupuser –computer $strcomputer   
$admins = $admins |? {$_.groupcomponent –like '*"Administrators"'}  
  
$admins |% {  
$_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul  
$matches[1].trim('"') + “\” + $matches[2].trim('"')  
}  
}

Then you can call it like:

$array = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name
getlocaladmin $array

Get-WMIObject's ComputerName parameter gets a string[] so that means you can pass an array to it.

Wasif
  • 14,755
  • 3
  • 14
  • 34