Can't figure out why it won't pass parameter?

1..254 |% {if (test-connection -computername 10.120.150.$_ -count 1 -quiet) {write-output 10.120.150.$_} }

It works perfectly

1..254 |% {if (test-connection -computername 10.120.150.$_ -count 1 -quiet) { get-wmiobject -computername 10.120.150.$_ -class win32_computersystem} }

I got error on {get-wmiobject -computer 10.120.150.$_ -class win32…

GetWMICOMException,Microsoft .Powershell.commands.getwmiobjectcommand..

Can someone help?

Are you trying to remote in 2? If so, you need to open a remote session. It might help if you format the code a bit.

Do you get the error for every instance or just specific IP addresses? Have you tried to just run gwmi win32_computersystem against one of the valid addresses to see if you get the same error? Can you provide more of the error details?

I suspect that your full error output looks like this:

get-wmiobject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:1
+ get-wmiobject -ComputerName -Class win32_computersystem
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

In this case, the RPC server is the important bit.

Test-Connection relies on ICMP traffic, which will be permitted on most networks, and doesn’t require any special permissions.

In contrast, Get-WMIObject relies on the RPC server, which uses TCP port 135 and may be blocked by your network’s firewall. Additionally, in order to use Get-WMIObject on a remote computer, your user account must be a member of the Administrators group for that computer.

So in this usage Test-Connection will pass normally, but Get-WMIObject may fail due to:

  1. RPC server not running
  2. firewall blocking TCP port 135
  3. user not having admin privileges on remote computer
If you're doing this at home, you may have to adjust your firewall, but #3 may still cause a problem because your user account on one computer won't necessarily be the same on another. If you're doing this at work... why are you trying to gather data on all the computers on a /24 subnet all at once? and why are you including the gateway in the address range? If #2 is the issue you'll need the help of your local network admin. If #3 is the issue then you probably shouldn't be doing this at all, because it kind of looks like you're trying to probe the network to find out which systems are available on which IP addresses.

We have Pre-imaged desktops ship to the branches from time to time. Sometimes, those desktops failed to register to our DNS. We try to remote in each subnet to scan. Using GWMI to get hostname directly from desktop if it is pingable. All TCP ports open by default. And I have admin right.

Are you running PowerShell with your domain admin credentials? When you use something like Get-WmiObject to remote into a machine it is using the credentials you logged into the host machine with. For me, most the time I am using a local account but in order to use RPC-type commands like this remotely I have to start PowerShell with runas to specify the appropriate domain admin credentials for it to work. Another work around to this is use WinRM instead of RPC. Here’s How I would rewrite your code to do so.

$alives = 1..254 |% {if (test-connection -computername "10.120.150.$_" -count 1 -quiet) {"10.120.150.$_"}}
$creds = Get-Credential -UserName "YourUserName" -Message "Enter Credentials Here"
Invoke-Command -ComputerName $alives -Credential $creds -ScriptBlock {
     Get-WmiObject -Class win32_computersystem


Instead of Get-WMIObject use Get-CIMInstance, it uses WinRM service via WSMan protocol, and doesn’t require to open 135, 445 and additionally 1024 & 1034, it only needs 5985/5986. Please refer to the link below for better understanding…

Get-CimInstance -ClassName CIM_ComputerSystem -ComputerName ComputerName

Thank you.


Thanks, All. It works