Basit Amjad (Customer) asked a question.

Assigning a variable to another variable breaks the powershell script

I have the following script

 

$ipPattern = '10\.6\.4\.\d+'

$ipAddress = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -match $ipPattern }).IPAddress

 

$webServer = "10.6.4.20"

 

When I change the line above to

 

$webServer = $ipAddress

 

it breaks the script note that the $ipAddress returns the same ip address when I test this via powershell as a standaone script

 

Any thoughts on this?


  • Mike Rockwell (Progress Software)

    Why not just do:

    1. $webServer = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -match $ipPattern }).IPAddress

     

    • Basit Amjad (Customer)

      so I made the change above and tested, no difference. I then took the entire script and put it under settings -> Actions & Alerts -> Actions and Policies and ran it against one device and it worked fine.

       

      I do not understand why adding this script as a monitor to the device does not work if I have the $webServer variable defined as above. If I hard code the variable it works

      • Mike Rockwell (Progress Software)

        First thing, this line $Context.SetResult(0, "Running Update Monitor Polling Interval Script");" should be the last line or next to the last line that is run. That tells WUG your Monitor was successful and is finished. The Zero (0) means successfully ran, One (1) means it was unsuccessful.

         

        You run your script, then test it if returned what you want, then set the $Context.SetResult(0, "Some text) it is successful and UP, or $Context.SetResult(1, "some text) it failed and is down. At the first link it will always show up and not run anything else.

      • Mike Rockwell (Progress Software)

        Also your functions should be at the top of the script. PowerShell reads from the Top down and it has to load functions first for them to work, then at the bottom, run your code that calls the functions ....

         

      • Basit Amjad (Customer)

        ok but how is all this related to the issue I am facing?

      • Mike Rockwell (Progress Software)

        You wanted to know why it didn't work as a monitor. Also, are you coding for the 32-Bit PowerShell and not the 64-Bit? WUG runs the 32-Bit PowerShell. There are some things in PS that work in 64-Bit but not the 32-Bit.