Consul watch handler in PowerShell

324 views
Skip to first unread message

Rajinder Singh

unread,
May 22, 2017, 5:11:12 PM5/22/17
to Consul
I have a windows only consul setup.
We are monitoring the health of Window Services.
We have Powershell scripts that  determine if a service is running.
I want to restart the service if it has unexpected shutdown.
So i am trying to  use watches.

  "watches": [
{
"type": "checks",
"state": "critical",
"handler": "PowerShell -ExecutionPolicy Bypass -NoProfile -File C:\\scripts\\StartService.ps1"
}
  ]


I am trying to figure out how to access Payload in the handler script.
I have log level at debug and I do see handle script executes.
I need to payload to determine which service health check failed.

However I cannot figure out how to get the value of payload in a powershell script.
Automatic variable $input is empty. $args is empty as well

Let me know if somebody has a PowerShell watch handler example.

Rajinder Singh

unread,
May 23, 2017, 11:33:06 AM5/23/17
to Consul


We are in a catch 22 situation 
Current watcher implementation does not allow us to filter based on health check name.
So we have no option but to read the payload. The fact that I have not been able to find a way to read payload has stopped us in our tracks. 


I wanted to share a few different things but no success so far.

This is the script that is invoked by watcher.

PowerShell -ExecutionPolicy Bypass -NoProfile -File C:\\scripts\\StartService.ps1

In StartService.ps1

 param(
        [Parameter(ValueFromPipeline=$true, Position=1)]
        $payload
    )

This should allow payload to be either piped or passed in as an argument.
I am printing the value of payload and its blank.

I may have to start reading the code to see how payload is passed into the handler script.
I did not find any example of a bash script or powershell script in the documentation for watches.
If I can get this to work I will contribute PowerShell example to help others.


Eugene Bogatenkov

unread,
Dec 11, 2018, 10:01:28 AM12/11/18
to Consul
Here is a working example in case someone still needs it:
read-host | Out-File -FilePath C:\Consul\handler.log
Reply all
Reply to author
Forward
0 new messages