Use PowerShell scripts to monitor AF Health Check counter
- Last UpdatedJun 27, 2023
- 1 minute read
If you are unable to run Internet Information Services (IIS) on your system, you might use Windows PowerShell scripts to create a listener to monitor the AF Server Health tag, as demonstrated by the following example code fragments.
-
Create a listener.
For example:
$endpoint = new-object System.Net.IPEndPoint
([system.net.ipaddress]::any, $port)$listener = new-object System.Net.Sockets.TcpListener
$endpoint$listener.start()
-
Read the AF Server Health tag.
For example:
$counter_path = "\PI AF Server\Health"
$counters_value = get-counter -counter $counter_path |
Select-Object –ExpandProperty CounterSamples |
Select-Object CookedValue -
Parse the response.
For example:
$Pass = Select-String -inputObject $counters_value
-pattern "CookedValue=1" -quietIf ($Pass){$responseString=$up}else{$responseString=$down}
-
Send the response to the listener.
For example:
$sendBytes = [System.Text.Encoding]::ASCII.GetBytes($responseString) + $CR + $LF
$stream.Write($sendBytes,0,$sendBytes.Length)