Hello,
I wonder if anyone can please assist me, I am trying to create a 2 State Monitor to alert if a FTP connection fails. I am using the PowerShell script was written by Peter Zerger (see http://www.systemcentercentral.com/monitoring-ftp-site-availability-in-opsmgr-using-powershell/)
The script works perfectly without any issues when running this from PowerShell, But the 2 State monitor never changes, it always remains in a healthly state, not sure what to try next also is there away of checking what values are being sent back in the Property bag?. I am new to PowerShell so can anyone assist please.
Â
$ScomAPI = New-Object -comObject âMOM.ScriptAPIâ
$PropertyBag = $ScomAPI.CreatePropertyBag()
Â
Get the object used to communicate with the server.
$Request = [System.Net.WebRequest]::Create(âftp:ftp URL goes in here/â)
$Request.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
This example assumes the FTP site uses anonymous logon.
Username/password not real
$Request.Credentials = New-Object System.Net.NetworkCredential âusernameâ,password
$Response = $Request.GetResponse()
$ResponseStream = $Response.GetResponseStream()
Read and display the text in the file
$Reader = new-object System.Io.StreamReader $Responsestream
[System.Console]::Writeline($Reader.ReadToEnd())
Display Status
âDownload Complete, status:â
$response.StatusDescription
Â
if ($response.StatusDescription -match â226â){
#{âResponse code is 226. Health is GOODâ} #testing only. Comment out this line for production use.
#Write-Host âWe hit a TRUE matchâ
$PropertyBag.AddValue(âstateâ,âGOODâ)
#Submit Property Bag
#$API.Return($Bag) #for testing only. Comment out this line for production use.
$PropertyBag #For production use. Un-comment this line for production use.
}
else {
#{âResponse code not 226. Health is BADâ} #testing only. Comment out this line for production use.
#If not exists STATE=BAD
#Write-Host âWe hit a False matchâ
$PropertyBag.AddValue(âstateâ,âBADâ)
#Submit Property Bag
#$API.Return($Bag) #for testing only. Comment out this line for production use.
$PropertyBag #For production use. Un-comment this line for production use.
}
Close Reader and Response objects
$Reader.Close()
$Response.Close()
Â
The Monitor Unhealthily Expression reads:
Property[@Name=âstateâ] Equals GOOD (Please note I have set this intentionally to generate an alert)
so in theory it should generate an alert
Â
The Monitor Healthy Expression reads:
Property[@Name=âstateâ] Equals BAD