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

  • 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
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
}
  • 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 – 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.