I wanted to share a little technique I use to "fake" rearming of agent procedures similar to what we can do with monitoring today :-)

In a normal monitor there is the field this that allows us to set the frequency, duration and re-arm time on event  log alerts.

This isn't perfect and it certainly isn't pretty but i did devise a technique to simulate this for agent procedures.  Attached is the agent procedure.  Attached and pasted below is the example of this technique in powershell.

Enjoy...

(Gists link in case the forum messes up the formatting - https://gist.github.com/zpeters/7915604)


### Variables
$basedir = "c:\MDaemon\Queues"
$warningLevel = $args[0]
$rearmSeconds = $args[1]
$sentinelFile = "c:\appliedtech\mdaemon_sentinel.txt"


### Functions
function alertText([string]$message){
Write-Host "$message"
}


### get msg count in all queue paths
#Bad messages
$badPath = $basedir + "\Bad\*.*"
$badMsg = @(Get-ChildItem $badPath -include "*.msg" -ErrorAction silentlycontinue)
$badCount = $badMsg.length

#Holding messages
$holdingPath = $basedir + "\Holding\*.*"
$holdingMsg = @(Get-ChildItem $holdingPath -include "*.msg" -ErrorAction silentlycontinue)
$holdingCount = $holdingMsg.length

#Inbound messages
$inboundPath = $basedir + "\Inbound\*.*"
$inboundMsg = @(Get-ChildItem $inboundPath -include "*.msg" -ErrorAction silentlycontinue)
$inboudCount = $inboundMsg.length

#Lan messages
$lanPath = $basedir + "\Lan\*.*"
$lanMsg = @(Get-ChildItem $lanPath -include "*.msg" -ErrorAction silentlycontinue)
$lanCount = $lanMsg.length

#Local messages
$localPath = $basedir + "\Local\*.*"
$localMsg = @(Get-ChildItem $localPath -include "*.msg" -ErrorAction silentlycontinue)
$localCount = $localMsg.length

#Raw messages
$rawPath = $basedir + "\Raw\*.*"
$rawMsg = @(Get-ChildItem $rawPath -include "*.msg" -ErrorAction silentlycontinue)
$rawCount = $rawMsg.length

#Remote messages
$remotePath = $basedir + "\Remote\*.*"
$remoteMsg = @(Get-ChildItem $remotePath -include "*.msg" -ErrorAction silentlycontinue)
$remoteCount = $remoteMsg.length

#Retry messages
$retryPath = $basedir + "\Remote\Retry\*.*"
$retryMsg = @(Get-ChildItem $retryPath -include "*.msg" -ErrorAction silentlycontinue)
$retryCount = $retryMsg.length

### test if any queues are over the warningLevel
if ($badCount -ge $warningLevel -or $holdingCount -ge $warningLevel -or $inboundCount -ge $warningLevel -or $lanCount -ge $warningLevel -or $localCount -ge $warningLevel -or $rawCount -ge $warningLevel -or $remoteCount -ge $warningLevel -or $retryCount -ge $warningLevel) {
# creating the alert string
$message = "Warning Level: $warningLevel message(s)`nRearm: $rearmSeconds seconds`n`nQueue`n----------------------`nBad: $badCount`nHolding: $holdingCount`nInbound: $inboundCount`nLan: $lanCount`nLocal: $localCount`nRaw: $rawCount`nRemote: $remoteCount`nRetry: $retryCount"


if (Test-Path $sentinelFile) {
$lastWriteDate = (Get-ItemProperty -Path $sentinelFile).LastWriteTime
$secondsDiff = (New-TimeSpan -Start $lastWriteDate -End (Get-Date)).TotalSeconds
if ($secondsDiff -gt $rearmSeconds) {
# Limit hit and we are not disarmed - alerting
# create a new file with the message in it - "resets" the timer and helps with troubleshooting
$message | Out-File $sentinelFile
alertText $message
} else {
# Limit hit, but we are still disarmed, skipping for now...
}
} else {
# Limit has been hit but no sentinel - alerting and creating a sentinel for next run
# create a new empty file
#New-Item $sentinelFile -type File | out-null
# create a new file with the message in it - helps with troubleshooting
$message | Out-File $sentinelFile
alertText $message
}

} else {
# limit not hit, moving on...
}