This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

PowerShell Component Monitors - Not Defined

I've been looking for a solid example of a working PowerShell component monitor. I've been struggling with 'Not Defined' and I've been looking for a solid current example. 

I've located quite a few examples but without success. 

I'm trying to compare to arrays of IP Addresses. 

My Current (not working):

# Query external DNS server and store output as string
# Querying dns.google
$ExtIPs = Resolve-DnsName payments.ca -DnsOnly -server 8.8.8.8 | Select-Object -ExpandProperty IPAddress | Sort-Object -Descending

# Setting the IntIP to match External IPs
# SR -set IPs 2023-03-18
$IntIPs = ('7.77.77.207','7.23.98.149')

# Check to see if IPs if are the same
$ListsEqual = $true

IF (diff $IntIPs $ExtIPs)
{
$ListsEqual = $false
Write-Host "Statistic: 1"
Write-Host "Message:" $ListsEqual
exit 1
}
ELSE
{
$ListsEqual = $true
Write-Host "Statistic: 0"
Write-Host "Message:" $ListsEqual
exit 0
}

  • Are you using the "test" button in the "edit script" dialogue? If not you tend to get errors like this regardless, it formats the thing receiving the output

    Try moving the endquote on the Message line to  "  Write-Host "Message: $ListsEqual"   "

    Script seems like it works in general, though I've no idea why the if doesn't error

  • So on the example you gave above, it looks like on both of your Write-Host for message, you have the quote too early.  In theory it should be:

    Write-Host "Message: $($ListsEqual)" where you have the end quote after the semicolon.

    I normally like to start with my code in Visual Studio Code or another PS script editor, to validate the logic itself works.  I then run the script against the host first to validate my results.  Normally commenting out the exit blocks.

    So are you running into not defined on just this script, or any powershell script?

    I normally start my process with a simple:

    Write-Host "Message: $($ENV:ComputerName)"

    Then I pick my destination node and credentials and run the test.  This way I can verify I'm either A. hitting the right host (remote execution or polling method are normally the likely culprits) or if it just fails outright.  Which if it just fails, then I try a different node (which I know remote PS calls work on), and if that is a problem then I know i have something bigger going on.  If I see that systems name, then I know its a local host issue.  Once I validated that, then I remove it and start to build out the script chunk by chunk.  Outputting each call to a message, and then once I know my variables/counts are working correctly, then I branch into the final If/then logic.