forked from makcuber/LuxaforPowershellCLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluxps.ps1
321 lines (284 loc) · 9.46 KB
/
luxps.ps1
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#Dev: Jonathan Brunath
#Contact: [email protected]
#Desc: CLI tool for setting colour of Luxafor LED Status Flag, with option for running as background process that sets colour on screen lock
param([boolean]$verbose)
#The WebHook ID will load from the configPath file defined below, make sure you update this file if you re-generate the ID
$global:luxID = ""
#Duration inbetween Lock status scans
$scanPeriod = 5
#Toggle Console output
$global:echoOn = $verbose
#Config Path defaults to same directory as the script
$configPath = "$PSScriptRoot/luxid.conf"
#Count of how many times an error has looped
$global:errorLoopCount=0
#API info: https://luxafor.com/webhook-api/
$APIurl = @{
solid = "https://api.luxafor.com/webhook/v1/actions/solid_color" #accepts any colour in colours array
blink = "https://api.luxafor.com/webhook/v1/actions/blink" #accepts any colour in colours array
pattern = "https://api.luxafor.com/webhook/v1/actions/pattern" #accepts any pattern in pattern array
}
#Colour Definitions
$lockColour = "red"
$onlineColour = "green"
$busyColour = "blue"
$colours = "red", "green", "yellow", "blue", "white", "cyan", "magenta"
$patterns = "police", "traffic lights", "random 1", "random 2", "random 3", "random 4", "random 5"
$extraWindowsOnlyPatterns = "rainbow", "sea", "white wave", "synthetic"
#Functions
function echoVerbose {
#Write-Host $global:echoOn
if ($global:echoOn -eq $true) {
Write-Host $args[0]
#return $true
} else {
#return $false
}
}
function verifyLuxID {
Param([Boolean]$writeID=$false,[string]$testLuxID,[int]$errorLoop)
if ($testLuxID -eq "") {
if ($writeID) {
Write-Host "Error: No Webhook ID provided"
$global:errorLoopCount++
echoVerbose "ErrorLoop: $errorLoop"
genConfig -error $true
} else {
Write-Host "Error: Config file is empty"
genConfig
}
} else {
$global:luxID = $testLuxID
}
#the errorLoop check prevents the config from being writen multiple times if there was an error getting input from the user
if ($writeID -and ($errorLoop -eq $errorLoopCount) ) {
Out-File -FilePath $configPath -InputObject $testLuxID
Write-Host "Wrote webhook ID to config file"
$global:luxID = $testLuxID
}
echoVerbose "ErrorLoop: $errorLoop"
}
function genConfig {
#get LuxID from user via CLI, write to config file
Write-Host ""
Write-Host "Generate a Luxafor webhook ID from the Luxafor app"
Write-Host "In v2 of the app, this is located under General>Webhook"
Write-Host ""
$testLuxID = Read-Host "Enter your Luxafor Webhook ID"
verifyLuxID -writeID $true -testLuxID $testLuxID -errorLoop $global:errorLoopCount
}
function config {
Write-Host ""
if ((Test-Path -Path $configPath) -eq $true) {
Write-Host "Config file found ($configPath)"
$testLuxID = $(Get-Content -Path $configPath)
echoVerbose "Imported LuxID: $testLuxID"
#add luxID valiation here
verifyLuxID -testLuxID $testLuxID
Write-Host "LuxID Set: $($global:luxID)"
Write-Host ""
} else {
Write-Host "Error: No config file found"
genConfig
}
}
function validateItem {
Param($item,$array)
$isValid = $false
echoVerbose $item
foreach ($testItem in $array) {
if ($testItem -eq $item) {
echoVerbose "Test Item: $testItem"
$isValid = $true
break
}
}
echoVerbose $isValid
return $isValid
}
function validateColour {
[alias("validateColor")]
Param([Alias("color")][string]$colour)
return $(validateItem -item $colour -array $colours)
}
function validatePattern {
Param([string]$pattern)
return $(validateItem -item $pattern -array $patterns)
}
function listArray {
Param($array)
foreach ($item in $array) {
Write-Host " - $item"
}
Write-Host ""
}
function listColours {
[alias("listColors")]
Param()
listArray -array $colours
}
function listPatterns {
listArray -array $patterns
}
function setColour {
[alias("setColor")]
Param([Alias("color")][string]$colour)
if ((validateColour -colour $colour) -eq $true) {
$jsonData = @{
userId = "$global:luxID" #must be in quotes
actionFields = @{ color = "$colour" }
} | ConvertTo-Json
$params = @{
Uri = $APIurl['solid']
Method = 'POST'
Body = $jsonData #already in json format
ContentType = 'application/json'
}
$result=Invoke-RestMethod @params -UseDefaultCredentials
} else {
Write-Host "Error: Invalid Colour ($colour)"
Write-Host "Valid colours are: "
listColours
}
}
function setCustomColour {
[alias("setCustomColor")]
Param([Alias("color")][string]$colour)
$regex = $colour -Match '^[\d|A-Z|a-z]{6}$'
if (($regex) -and $colour) {
$jsonData = @{
userId = "$global:luxID" #must be in qoutes
actionFields = @{
color = "custom"
custom_color = $colour
}
} | ConvertTo-Json
$params = @{
Uri = $APIurl['solid']
Method = 'POST'
Body = $jsonData #already in json format
ContentType = 'application/json'
}
$result=Invoke-RestMethod @params -UseDefaultCredentials
} else {
Write-Host "Error: Invalid Custom Colour ($colour)"
Write-Host "Valid colours are 6 symbol hex color codes."
}
}
function setOff {
$jsonData = @{
userId = "$global:luxID" #must be in qoutes
actionFields = @{
color = "custom"
custom_color = "000000"
}
} | ConvertTo-Json
$params = @{
Uri = $APIurl['solid']
Method = 'POST'
Body = $jsonData #already in json format
ContentType = 'application/json'
}
$result=Invoke-RestMethod @params -UseDefaultCredentials
}
function setBlink {
Param([Alias("color")][string]$colour)
if ((validateColour -colour $colour) -eq $true) {
$jsonData = @{
userId = "$global:luxID" #must be in qoutes
actionFields = @{ color = $colour }
} | ConvertTo-Json
$params = @{
Uri = $APIurl['blink']
Method = 'POST'
Body = $jsonData #already in json format
ContentType = 'application/json'
}
$result=Invoke-RestMethod @params -UseDefaultCredentials
} else {
Write-Host "Error: Invalid Colour ($colour)"
Write-Host ""
Write-Host "Valid colours are: "
listColours
}
}
function setPattern {
Param([string]$pattern)
if ((validatePattern -pattern $pattern) -eq $true) {
$jsonData = @{
userId = "$global:luxID" #must be in qoutes
actionFields = @{ pattern = $pattern }
} | ConvertTo-Json
$params = @{
Uri = $APIurl['pattern']
Method = 'POST'
Body = $jsonData #already in json format
ContentType = 'application/json'
}
$result=Invoke-RestMethod @params -UseDefaultCredentials
} else {
Write-Host "Error: Invalid pattern $pattern"
Write-Host ""
Write-Host "Valid paterns are: "
listPatterns
}
}
function serviceMode {
$onlineLock = $false
echoVerbose "scanPeriod: $scanPeriod"
while ($true)
{
start-sleep $scanPeriod
$currentuser = gwmi -Class win32_computersystem | select -ExpandProperty username
$process = get-process logonui -ea silentlycontinue
$lockState = ($currentuser -and $process)
Write-Host "LockState: $lockState"
if ($lockState -eq $true) {
$onlineLock = $false
setColour -colour $lockColour
} else {
if ($onlineLock -eq $false) {
setColour -colour $onlineColour
$onlineLock = $true
}
}
echoVerbose "OnlineLock: $onlineLock"
echoVerbose ""
}
}
function showHelp {
Write-Host ""
Write-Host "Help Menu"
Write-Host "---------"
Write-Host ""
Write-Host "NOTE: You must manually set the correct WebHook LuxID in this script before running"
Write-Host ""
Write-Host "Parameters:"
Write-Host " -off : Turn off flag colour"
Write-Host " -colour <colour> : Set a specific colour"
Write-Host " -customcolour <colour> : Set a custom colour by hex value"
Write-Host " -blink <colour> : Trigger a blink event to a specific colour"
Write-Host " -pattern <pattern>: Trigger a pattern event"
Write-Host " -service : Enable service mode that changes the colour based on the machines Screen Lock state"
Write-Host ""
}
#Main
#if ($verbose.GetType().FullName -eq "System.Boolean") {
# $global:echoOn = $verbose
# Write-Host $verbose
# echoVerbose "Verbose: $global:echoOn"
#}
echoVerbose "echoOn: $global:echoOn"
echoVerbose "Args: $args"
echoVerbose ""
config
switch ($args[0]) {
-off { setOff; break }
-colour { setColour -colour $args[1]; break }
-customcolour { setCustomcolour -colour $args[1]; break }
-blink { setBlink -colour $args[1]; break }
-pattern { setPattern -pattern $args[1]; break }
-service { serviceMode; break }
-help { showHelp; break}
default { showHelp; break}
}