Variable not being recognized in loop

This topic contains 6 replies, has 6 voices, and was last updated by Profile photo of HeavyWater HeavyWater 5 months, 1 week ago.

  • Author
    Posts
  • #64209
    Profile photo of HeavyWater
    HeavyWater
    Participant

    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.

    ugh..

  • #64221
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    try to debug your $svr variable with Write-Host "Server: -$($svr)-" form to look for 'edge effects'

    • #64224
      Profile photo of SkullyD
      SkullyD
      Participant

      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.

  • #64231
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

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

  • #64243
    Profile photo of Mark
    Mark
    Participant

    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.

  • #64246
    Profile photo of Dudebro
    Dudebro
    Participant

    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"
  • #64314
    Profile photo of HeavyWater
    HeavyWater
    Participant

    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.

    Cheers.

You must be logged in to reply to this topic.