0
$Output = 'C:\temp\Result.txt'
$Servers = Get-Content 'C:\temp\ServerNames.txt'
$ScriptBlock = {

$Groups = Get-WmiObject Win32_GroupUser -ComputerName $Servers 
$LocalAdmins = $Groups | Where GroupComponent –like '*"Administrators"'

$LocalAdmins |% {  
$_.partcomponent –match ".+Domain\=(.+)\,Name\=(.+)$" > $nul  
$matches[1].trim('"') + "\" + $matches[2].trim('"')
}
}
foreach ($ServerNames in $Servers) {
"Local Admin group members in $ServerNames" | Out-File $Output -Append
Invoke-command -ScriptBlock $ScriptBlock -ComputerName $ServerNames | Out-  File $Output -Append
}

I am using the above mentioned script to get local admins group members to run against multiple servers, I am getting error -

Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand + PSComputerName : Computer

Can you pls assist me to correct that...?

Server Names in the file ServerNames.txt are in this format mentioned below -

ServerNames.txt

V T
  • 3
  • 1
  • 4
  • Do you have a file named ServerNames.txt located in C:\temp with a list of server names in the file? – user5870571 Mar 23 '17 at 19:55
  • Yes,I do have that file name in C:\temp with server names – V T Mar 23 '17 at 20:01
  • Would you mind posting the contents of the file? – user5870571 Mar 23 '17 at 20:02
  • Applbtrn01 Applbtrn02 Applbtrn03 Applbtrn04 Applbtrn05 Applbtrn06 Applbtrn07 – V T Mar 23 '17 at 20:04
  • Its one below the other...I was trying that same format here, for some reasons its not letting me – V T Mar 23 '17 at 20:05
  • Would you please update the question and make sure the formatting is the same as the file? – user5870571 Mar 23 '17 at 20:05
  • attached the screenshot for those server names...FYI – V T Mar 23 '17 at 20:10
  • shouldn't the match line in the scriptblock be inside an if command? Thr following line will be executed even if there was no match. – LotPings Mar 23 '17 at 20:12
  • Hi LotPings...can you pls explain how I can change that...I am still in the learning process... – V T Mar 23 '17 at 20:27
  • If you want me to get a notification prepend the username with an `@`. See my answer. – LotPings Mar 23 '17 at 21:26
  • If you open this PowerShell script in PowerShell ISE, run the script, run `echo $Servers`, then what do you get? – user5870571 Mar 24 '17 at 18:10
  • I am getting the server names in the ServerNames.txt file... – V T Mar 24 '17 at 19:41
  • its working fine when using script edited by LotPings......Another thing is because its windows server 2008 R2...I am getting this message mentioned below - Connecting to remote server failed with the following error message : The client cannot connect to th e destination specified in the request. Verify that the service on the destination is running and is accepting requests . Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or Win RM. run the command and configure WinRM service: "winrm quickconfig". – V T Mar 24 '17 at 19:45

1 Answers1

1
$Output = 'C:\temp\Result.txt'
$Servers= Get-Content 'C:\temp\ServerNames.txt'
$ScriptBlock = {
    $Groups = Get-WmiObject Win32_GroupUser -ComputerName $Using:ServerName
    $LocalAdmins = $Groups | Where GroupComponent –like '*"Administrators"'
    $LocalAdmins | ForEach-Object {  
        If($_.partcomponent –match ".+Domain\=(.+)\,Name\=(.+)$"){  
            $matches[1].trim('"') + "\" + $matches[2].trim('"')
        }
    }
}
ForEach ($ServerName in $Servers) {
    "Local Admin group members in $ServerName" | Out-File $Output -Append
    Invoke-command -ScriptBlock $ScriptBlock -ComputerName $ServerName | Out-File $Output -Append
}

But IMO this could be a bit simplified without the unnecessary vars

$Output = 'C:\temp\Result.txt'
$Servers= Get-Content 'C:\temp\ServerNames.txt'
$ScriptBlock = {
    Get-WmiObject Win32_GroupUser -ComputerName $Using:ServerName |
    Where GroupComponent –like '*"Administrators"'|
    ForEach-Object {  
        If($_.partcomponent –match ".+Domain\=(.+)\,Name\=(.+)$"){  
            $matches[1].trim('"') + "\" + $matches[2].trim('"')
        }
    }
}
ForEach ($ServerName in $Servers) {
    "Local Admin group members in $ServerName" | Out-File $Output -Append
    Invoke-command -ScriptBlock $ScriptBlock -ComputerName $ServerName | Out-File $Output -Append
}
LotPings
  • 1,015
  • 7
  • 12
  • I am still getting the same error - Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand + PSComputerName : Applbtrn07 – V T Mar 24 '17 at 14:04
  • I changed the var inside the scriptblock to $Using:... to get actual values invoking. [See this blog](http://www.padisetty.com/2014/05/all-about-powershell-scriptblock.html) – LotPings Mar 24 '17 at 14:29
  • @LotPings...its working fine. my apologies there were some typos in the script.Another thing is because its windows server 2008 R2...I am getting this message mentioned below - Connecting to remote server failed with the following error message : The client cannot connect to th e destination specified in the request. Verify that the service on the destination is running and is accepting requests . Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or Win RM. run the command and configure WinRM service: "winrm quickconfig". – V T Mar 24 '17 at 19:44
  • So the question is answered and you should check my asnwer. For the new problem investigate and eventually post another question. Good luck with this. – LotPings Mar 24 '17 at 19:59