PowerShell Component Monitor - returns Undefined

Hello, 

I am creating an Application Template with a PowerShell Component monitor. It's a verification that clients can upload to an sftp site. 

I have a test account called SOLARWINDS and it has secure cred file that are using in the session. The PS Component is set to run on the Remote Host.

The PowerShell code works on the test machine through various tests. 

When I add the PowerShell Component monitor to an Application Template and test it against the same Node, it returns undefined. 

I am hoping to know where the code is having the issue in the Component Monitor. 

#################################

# SolarWinds Application Monitor#

# Requires WinSCP (All Users)   #

#################################

# Load WinSCP .NET assembly

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# setting locations for sftp file transfers

$localDirectory = "C:\TestUpload\"

$remoteDirectory = "/"

# setting location of Secure creds

$KeyFile = "C:\scripts\sftp-upload-securecreds\Auxiliary_Files\sftp-transaction-connect.key"

$PasswordFile = "C:\scripts\sftp-upload-securecreds\Auxiliary_Files\sftp-transaction-connect-SOLARWINDS.password"

# getting secure creds for the SOLARWINDS user account

$ServiceAccountCredentials = Get-SecureCreds -AccountName SOLARWINDS -KeyFile $KeyFile -PasswordFile $PasswordFile

# Set up sftp session options

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{

    Protocol = [WinSCP.Protocol]::Sftp

    HostName = "mb.payments.ca"

    PortNumber = "22"

    UserName = $($ServiceAccountCredentials.UserName)

    Password = $($ServiceAccountCredentials.GetNetworkCredential().Password)

    SshHostKeyFingerprint = "ssh-rsa 2048zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

}

################

# SCRIPT START #

################

    # Create session

    $session = New-Object WinSCP.Session

    # Connect

    $session.Open($sessionOptions)

    # Wait for 5 seconds before getting $connectionStatus

    Start-Sleep -Seconds 5

if ( !$session.Opened)

  {

    write-host "Statistic: 1"

    Write-Host "Message: Can't sftp connect to $sessionOptions"

    exit 1

  }

  

  else

  {

    # Transfer files from local to remote

    $transfer = $session.PutFiles("$localDirectory\*", "$remoteDirectory/") 

  

    if ($transfer.IsSuccess -eq $true) {

        write-host "Statistic: 0"

        write-host "Message: sftp file transfer complete."

        exit 0

    }

    write-host "Statistic: 1"

    write-host "Message: sftp file transfer failed"

    exit 1

  }

##############################################################################

Parents
  • My guess without running your code is that not all paths are returning a value. Try putting an exit 1 at the very end and see what happens. You should probably wrap your script in try/catch block and handle errors. Also check the log files generated by your app monitor for errors. 

  • Maybe something like this...and if there is an issue connecting with credentials then your script will return critical and you can look at the contents of Message.Error in the components pane of the Application Details page of the Solarwinds web console or the log file in C:\ProgramData\SolarWinds\Logs_Agent\APM\ApplicationLogs.

    # Solarwinds Exit codes
    # 0                   Up
    # 1                   Down
    # 2                   Warning
    # 3	                  Critical
    # Any other value     Unknown, for example 4
    
    try {
        # Wait for 5 seconds before getting $connectionStatus
        Start-Sleep -Seconds 5
    
        if (!$session.Opened)
        {
            write-host "Statistic.Error: 1"
            Write-Host "Message.Error: Can't sftp connect to $sessionOptions"
            exit 3 # Critical
        }
        else
        {
            # Transfer files from local to remote
            $transfer = $session.PutFiles("$localDirectory\*", "$remoteDirectory/")
    
            if ($transfer.IsSuccess -ne $true) 
            {
                write-host "Statistic.Error: 1"
                write-host "Message.Error: transfer error."
                exit 3 # Critical
            }
        }
    }
    catch {
        Write-Host "Statistic.Error: 1"
        Write-Host "Message.Exception: $($PSItem.Exception.GetType()) $($PSItem.Exception.Message)`r`n$($PSItem.ScriptStackTrace)"
        exit 3 # Critical
    }
    
    # if we got here then success!
    # 0 = Up = script ran successfully
    exit 0
    

Reply
  • Maybe something like this...and if there is an issue connecting with credentials then your script will return critical and you can look at the contents of Message.Error in the components pane of the Application Details page of the Solarwinds web console or the log file in C:\ProgramData\SolarWinds\Logs_Agent\APM\ApplicationLogs.

    # Solarwinds Exit codes
    # 0                   Up
    # 1                   Down
    # 2                   Warning
    # 3	                  Critical
    # Any other value     Unknown, for example 4
    
    try {
        # Wait for 5 seconds before getting $connectionStatus
        Start-Sleep -Seconds 5
    
        if (!$session.Opened)
        {
            write-host "Statistic.Error: 1"
            Write-Host "Message.Error: Can't sftp connect to $sessionOptions"
            exit 3 # Critical
        }
        else
        {
            # Transfer files from local to remote
            $transfer = $session.PutFiles("$localDirectory\*", "$remoteDirectory/")
    
            if ($transfer.IsSuccess -ne $true) 
            {
                write-host "Statistic.Error: 1"
                write-host "Message.Error: transfer error."
                exit 3 # Critical
            }
        }
    }
    catch {
        Write-Host "Statistic.Error: 1"
        Write-Host "Message.Exception: $($PSItem.Exception.GetType()) $($PSItem.Exception.Message)`r`n$($PSItem.ScriptStackTrace)"
        exit 3 # Critical
    }
    
    # if we got here then success!
    # 0 = Up = script ran successfully
    exit 0
    

Children
No Data