I having a tough time getting my script to correctly report the exit code to SAM 6.1.1. I have read The Basics of PowerShell (part 3) and I am using the required Message, Statistic and Exit codes. My script Telnets to a server (using a built telnet function, grabs a payload and verifies that its valid. I want 3 exit codes, 0 - Payload is confirmed (Up), 3 - Payload is invalid (Critical), 1 - Any error & failed to connect (Down). Exit codes 0 and 3 report correctly but 1 will not.
$error.clear()
$ErrorActionPreference= 'silentlycontinue'
#This builds a custom telnet function from http://community.spiceworks.com/scripts/show/1887-get-telnet-telnet-to-a-device-and-issue-commands
Function Get-Telnet
{ Param (
[Parameter(ValueFromPipeline=$true)]
[String[]]$Commands = @(""),
[string]$RemoteHost = "",
[string]$Port = "",
[int]$WaitTime = 1000,
[string]$OutputPath = ""
)
#Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
If ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
#Now start issuing the commands
ForEach ($Command in $Commands)
{ $Writer.WriteLine($Command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
#All commands issued, but since the last command is usually going to be
#the longest let's wait a little longer for it to finish
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
#Save all the results
While($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
}
Else
{ $Result = "Unable to connect to host: $($RemoteHost):$Port"
}
#Done, now save the results to a file
$Result | Out-File $OutputPath
}
#This clears the content of the output file so its not mistakenly read from previous telnet test
Clear-Content "F:\scripts\EDITelnetOutput.txt"
#This will telnet to the remote server and issue the command to check the payload. The output must be written to a file because get-telnet cmdlet sucks.
Get-Telnet -RemoteHost ${IP} -Port "3575" -Commands "Blah" -OutputPath "F:\scripts\TelnetOutput.txt"
$TelnetOutput = get-content "F:\scripts\TelnetOutput.txt"
#This looks for "Keyword" in the payload. If found then status is 1 and application is considered functional.
IF ($error)
{
Write-Host "Statistic: 1"
Write-Host "Message: $($error[0])"
Exit 1
}
IF ($TelnetOutput -match "Keyword") {
Write-Host "Statistic: 0"
Write-Host "Message: Valid Payload"
Exit 0
}
ELSE {
Write-Host "Statistic: 3"
Write-Host "Message: Invalid Payload"
Exit 3
}
Tests for Exit 0 and 3 are successful with proper output. Test for exit 1 is done by turning off the application. The port is closed and the telnet connection will fail. Below is the error that I get for Exit 1 "Get Output Failed". It should result in Exit 1 - Component is Down.
Image may be NSFW.
Clik here to view.
I found that if I changed "Exit 1" any other number value it will run successfully. Something is wrong with Exit 1.