-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-WebhookOnLock.ps1
More file actions
138 lines (101 loc) · 3.77 KB
/
Invoke-WebhookOnLock.ps1
File metadata and controls
138 lines (101 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.SYNOPSIS
Calls Home Assistant webhook endpoint when machine is locked/unlocked
.DESCRIPTION
Script can install itself as a sheduled task
.PARAMETER HomeAssistantHost
Home Assistant address (e.g. "192.168.1.2:8123")
.PARAMETER HookName
Hook name
.PARAMETER Action
Type of the action ("Locked" || "Unlocked")
.PARAMETER Exec
When used script issues HA request streight away
.INPUTS
None. You cannot pipe objects.
.OUTPUTS
None | ScheduledTask
.EXAMPLE
Script will ask whether to issue a HA request or setup scheduled task for 'OnWorkstationLocked' event
PS> .\Invoke-WebhookOnLock.ps1 "192.168.1.2:8123" "device_lock" Locked
.EXAMPLE
Script will ask whether to issue a HA request or setup scheduled task for 'OnWorkstationUnlocked' event
PS> .\Invoke-WebhookOnLock.ps1 "http://192.168.1.2:8123" "device_lock" Unlocked
.LINK
Github: https://github.com/maxwroc/Invoke-WebhookOnLock
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[String]$HomeAssistantHost,
[Parameter(Mandatory = $true, Position = 1)]
[String]$HookName,
[Parameter(Mandatory = $true, Position = 2)]
[ValidateSet('Locked','Unlocked')]
[String]$Action,
[Parameter(Mandatory = $false)]
[Switch]$Exec = $false
)
Function TriggerWebhook ($haHost, $haHookName, $action) {
$body = @{
device = $env:computername
action = $action.ToLower()
}
if (-not $haHost.StartsWith("http")) {
$haHost = "http://$($haHost)"
}
Invoke-WebRequest "$($haHost)/api/webhook/$($haHookName)" -Body ($body | ConvertTo-Json) -Method 'POST' -ContentType 'application/json'
}
Function RegisterTask($haHost, $haHookName, $action) {
$taskSettings = New-ScheduledTaskSettingsSet
$answer = Read-Host "Allow start if device is on batteries [y/n]"
if ($answer -ieq "y") {
$taskSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
}
$argumentString = "-NoProfile -WindowStyle Hidden $($PSCommandPath) $($haHost) $($haHookName) $($action) -Exec"
$taskAction = New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument $argumentString
$TASK_SESSION_LOCK = 7
$TASK_SESSION_UNLOCK = 8
$state = $TASK_SESSION_LOCK
if ($action -eq 'Unlocked') {
$state = $TASK_SESSION_UNLOCK
}
$stateChangeTrigger = Get-CimClass `
-Namespace ROOT\Microsoft\Windows\TaskScheduler `
-ClassName MSFT_TaskSessionStateChangeTrigger
$taskTrigger = New-CimInstance `
-CimClass $stateChangeTrigger `
-Property @{
StateChange = $state # TASK_SESSION_STATE_CHANGE_TYPE (taskschd.h)
} `
-ClientOnly
$principal = New-ScheduledTaskPrincipal -UserId "$($env:USERDOMAIN)\$($env:USERNAME)" -RunLevel Highest
$task = New-ScheduledTask `
-Trigger $taskTrigger `
-Action $taskAction `
-Description "Notify Home Assistant on device $($action.ToLower())" `
-Settings $taskSettings `
-Principal $principal
Register-ScheduledTask -InputObject $task -TaskName "HomeAssistant notify (device $($action.ToLower()))"
}
if ($Exec) {
$answer = '1'
} else {
Write-Host 'What do you want to do?'
Write-Host
Write-Host '1. Send event'
Write-Host '2. Install as event triggered task'
Write-Host 'q. Quit'
$answer = Read-Host 'Please choose the action'
Write-Host
}
switch ($answer) {
'1' {
TriggerWebhook $HomeAssistantHost $HookName $Action
} '2' {
RegisterTask $HomeAssistantHost $HookName $Action
} default {
exit
}
}