Variable not being recognized in loop

Scratching my head trying to understand why a variable is failing when looping through a foreach loop.

I know the variable is stored correctly because it’s echoed successfully when called in a simple write-host. I know the command syntax is correct because when I manually run the line of code without the use of the variable, the command completes successfully. I’m pretty sure it’s something i’m just overlooking but have been staring at the code for awhile and can’t seem to identify the issue.

Below is the script simplified:

$svrlist = gc "c:\temp\serverlist"
ForEach ($svr in $svrlist)
             $Nadapter=gwmi Win32_NetworkAdapterConfiguration -filter "IPEnabled='True'" -computername $svr
             Write-Host "Server:" $svr

I get the following error:

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At C:\temp\get-serverdns.ps1:4 char:13 + $Nadapter = gwmi Win32_NetworkAdapterConfiguration -filter "IPEnabled = ' ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I’m pretty sure the RPC error is because the value being passed to the pipeline is getting dorked somehow. The server is online and accessible.

If I declare the $srv variable with computername and then type the command passing the $srv variable as the computername parameter, it completes successfully.


try to debug your $svr variable with Write-Host “Server: -$($svr)-” form to look for ‘edge effects’

You didnt show what the serverlist file looks like.
Maybe move the write-host up above the gwmi line, so you see the contents of the variable before you get the error.

My guess is that your server list contains additional whitespace characters like tab or space which aren’t valid in a computer name.

WMI methods require Windows Management Instrumentation to be ticked in Windows Firewall exceptions. Otherwise you get RPC errors. Do you have it set up on server? Or try turning off firewall temporarily.

It might be possible that the $svrlist variable ends up being cast as a string instead of an array for some reason. I always do this:

[Array] $svrlist = gc "c:\temp\serverlist"

Thanks all. Was able to debug using your suggestions. Max’s suggestion returned no artifacts and output was exactly as stated. Moving the write host did not change anything. It echoed correctly originally so this was expected. No firewalls enabled. I was able to manually type the gwmi statement with static parameters rather than a variable and the command succeeded so that told me there was no issue with lack of communication. enclosing in quotes did not make a difference, output was the same.

Daniels suggestion led me to the issue. I decided for kicks to create a txt file from scratch. The original was output from another powershell script.

Pointing to the new txt file with just three entries yielded success. So i decided to analyze the original txt file closer and doing a control-a on the content showed there was a crap load of spaces after the server name in the row. I did a quick replace to remove the whitespace and success.

Thanks all for jumping in to assist. I really appreciate the help.