-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helpfuls.psm1
562 lines (448 loc) · 11.1 KB
/
Helpfuls.psm1
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#*************************************************************************************************
# Helper functions for enable/disable scripts
# Include with:
# Import-Module (Resolve-Path $PSScriptRoot\Helpfuls.psm1).Path -Scope Local -Force
#*************************************************************************************************
function CheckRequirements
{
param($name, $key)
if ($PSVersionTable.PSVersion.Major -lt 5)
{
Write-Host "$name requires PowerShell Windows Management Framework 5.0 or greater" -ForegroundColor Yellow
return $false
}
if (-not (Test-Path $key))
{
Write-Host "$name doesn't appear to be installed on this machine. You're good to go!"
return $false
}
return $true
}
function DisableFirewallRules
{
param($name, [bool] $shouldProcess)
Write-Host
Write-Host "Checking Inbound $name Firewall rules" -ForegroundColor Yellow
if ($shouldProcess)
{
$count = (Get-NetFirewallRule -DisplayName "$name`*" | ? { `
$_.Enabled -eq 'True' } | Disable-NetFirewallRule | measure).Count
}
else
{
$rules = Get-NetFirewallRule -DisplayName "$name`*" | Where Enabled -eq 'True'
$rules | % { Write-Host $_.Displayname -ForegroundColor DarkGray }
$count = $rules.Count
}
if ($rules -gt 0)
{
WriteWork "Disabled $count $name firewall rules"
return 1
}
WriteOK "All $name firewall rules already disabled"
return 0
}
function DisableScheduledTask
{
param($taskName, [bool] $shouldProcess)
Write-Host
Write-Host "Checking $taskName" -ForegroundColor Yellow
if ((Get-ScheduledTask -TaskName $taskName).State -ne 'Disabled')
{
if ($shouldProcess)
{
$t = Disable-ScheduledTask -TaskName $taskName | measure
}
WriteWork 'Disabled task'
return 1
}
WriteOK 'Task is already disabled'
return 0
}
# we could use "gwmi win32_service" command to find all services within the app pathname
# but we want to process the services in a specific order to disable the most intrusive first
<# Quick dump of all services and pathnames:
gwmi win32_service | % { `
new-object psobject -Property @{ `
Started = $_.Started `
DisplayName = $_.DisplayName `
Name = $_.Name; `
PathNm = $_.PathName } } | sort -Property Started,PathNm
#>
function DisableServices
{
param($names, [bool] $shouldProcess)
$changes = 0
Write-Host
Write-Host "Checking services" -ForegroundColor Yellow
$names | % `
{
$name = $_
$service = Get-Service -Name $name -ErrorAction:SilentlyContinue
if ($service)
{
Write-Host
Write-Host "Checking service `"$name`"" -ForegroundColor Yellow
# Disable the service
if ($service.StartType -ne 'Disabled')
{
WriteWork "sc config `"$name`" start= disabled"
if (ShouldProcess1 $shouldProcess "sc config `"$name`" start= disabled")
{
cmd /c sc config "$name" start= disabled
}
$changes++
}
else
{
WriteOK 'Service already set to Disabled'
}
# Set the failure action to "Take No Action" to prevent a restart
$nofail = $true
$failure = cmd /c sc qfailure "$name"
$failure | ? { $_ -like "*FAILURE_ACTIONS*" } | % `
{
WriteWork "sc failure `"$name`" reset= 0 actions= `"`""
if (ShouldProcess1 $shouldProcess "sc failure `"$name`" reset= 0 actions= `"`"")
{
cmd /c sc failure "$name" reset= 0 actions= `"`"
}
$nofail = $false
$changes++
}
if ($nofail)
{
WriteOK 'Service fail mode already set to No Action'
}
# Stop the service
if ($service.Status -ne 'Stopped')
{
if ($service.CanStop)
{
WriteWork "sc stop `"$name`""
if (ShouldProcess1 $shouldProcess "sc stop `"$name`"")
{
cmd /c sc stop "$name"
}
}
else
{
WriteWork "Killing `"$name`""
Get-Process -Name $name -ErrorAction Ignore | Stop-Process -Force
}
$changes++
}
else
{
WriteOK 'Service already Stopped'
}
}
else
{
Write-Host " Service not found `"$name`"" -ForegroundColor DarkGray
}
}
return $changes
}
function EnableFirewallRules
{
param($name, [bool] $shouldProcess)
Write-Host
Write-Host "Checking Inbound $name Firewall rules" -ForegroundColor Yellow
if ($shouldProcess)
{
$count = (Get-NetFirewallRule -DisplayName "$name`*" | ? { `
$_.Enabled -eq 'False' } | Enable-NetFirewallRule | measure).Count
}
else
{
$rules = Get-NetFirewallRule -DisplayName "$name`*" | Where Enabled -eq 'False'
$rules | % { Write-Host $_.Displayname -ForegroundColor DarkGray }
$count = $rules.Count
}
if ($rules -gt 0)
{
WriteWork "Enabled $count $name firewall rules"
return 1
}
WriteOK 'All LANDesk firewall rules already enabled'
return 0
}
function EnableScheduledTask
{
param($taskName, [bool] $shouldProcess)
Write-Host
Write-Host "Checking $taskName" -ForegroundColor Yellow
if ((Get-ScheduledTask -TaskName $taskName).State -eq 'Disabled')
{
if ($shouldProcess)
{
$t = Enable-ScheduledTask -TaskName $taskName | measure
}
WriteWork 'Enabled task'
return 1
}
WriteOK 'Task is already enabled'
return 0
}
function EnableServices
{
param($names, [bool] $shouldProcess)
Write-Host
Write-Host "Checking services" -ForegroundColor Yellow
$names | % `
{
$name = $_
$service = Get-Service -Name $name -ErrorAction:SilentlyContinue
if ($service)
{
Write-Host
Write-Host "Checking service `"$name`"" -ForegroundColor Yellow
# Enable the service
if ($service.StartType -eq 'Disabled')
{
if (ShouldProcess1 $shouldProcess "sc config `"$name`" start= auto")
{
WriteWork "sc config `"$name`" start= auto"
}
cmd /c sc config "$name" start= auto
}
else
{
WriteOK 'Service already set to Auto'
}
# FAILURE_ACTIONS will be restored automatically by LANDesk agents
# so no need to change them here
if ($service.Status -eq 'Stopped')
{
if ($service.CanStart)
{
WriteWork "sc start `"$name`""
if (ShouldProcess1 $shouldProcess "sc start `"$name`"")
{
cmd /c sc start "$name"
}
}
else
{
WriteOK "Service `"$name`" will auto-start as needed"
}
}
else
{
WriteOK 'Service already started'
}
}
else
{
Write-Host " Service not found `"$name`"" -ForegroundColor DarkGray
}
}
}
function HideInstallationFolder
{
param($installPath, [bool] $shouldProcess)
$changes = 0
if ($installPath.EndsWith('\'))
{
$installPath = $installPath.Substring(0, $installPath.Length - 1)
}
Write-Host
Write-Host 'Checking installation folder' -ForegroundColor Yellow
if (Test-Path $installPath)
{
WriteWork 'Hiding installation folder'
if (ShouldProcess1 $shouldProcess "Rename-Item `"$installPath`" `"$installPath`-HIDDEN`"")
{
Rename-Item $installPath "$installPath`-HIDDEN"
}
$changes++
}
else
{
WriteOK 'Folder already hidden'
}
return $changes
}
function ShowFirewallRules
{
param($name)
Write-Host
Write-Host "Finding Inbound $name Firewall rules" -ForegroundColor Yellow
Get-NetFirewallRule -DisplayName "$name`*" | % `
{
$rule = $_
if ($rule.Enabled -eq $true)
{
WriteWork "$($rule.DisplayName) - $($rule.Profile) ... Enabled"
}
else
{
WriteOK "$($rule.DisplayName) - $($rule.Profile) ... Disabled"
}
}
}
function ShowInstallationFolder
{
param($installPath)
if ($installPath.EndsWith('\'))
{
$installPath = $installPath.Substring(0, $installPath.Length - 1)
}
Write-Host
Write-Host "Checking installation folder: $installPath" -ForegroundColor Yellow
if (Test-Path $installPath)
{
WriteWork 'Installation folder is exposed'
}
else
{
WriteOK 'Installation folder is HIDDEN'
}
}
function ShowRogueProcesses
{
param($installPath)
Write-Host
Write-Host 'Finding rogue processes' -ForegroundColor Yellow
if ($installPath.EndsWith('\'))
{
$installPath = $installPath.Substring(0, $installPath.Length - 1)
}
$numproc = 0
Get-Process | ? { $_ -and $_.Path -and $_.Path.StartsWith($installPath) } | % `
{
$process = $_
$procName = $process.Name
$procPath = $process.Path
WriteWork "`"$procName`" ($procPath)"
$numproc++
}
if ($numproc -eq 0)
{
WriteOK 'None found'
}
}
function ShowScheduledTask
{
param($name)
Write-Host
Write-Host "Finding task $name" -ForegroundColor Yellow
$task = Get-ScheduledTask -TaskName $name -ErrorAction:SilentlyContinue
if ($task)
{
if ($task.State -ne 'Disabled')
{
WriteWork "$($task.TaskName) is $($task.State)"
}
else
{
WriteOK "$($task.TaskName) is $($task.State)"
}
}
else
{
Write-Host " $name scheduled task not found" -ForegroundColor
}
}
function ShowServices
{
param($names, [bool] $shouldProcess)
Write-Host
Write-Host "Finding services" -ForegroundColor Yellow
$names | % `
{
$name = $_
$service = Get-Service -Name $name -ErrorAction:SilentlyContinue
if ($service)
{
if (($service.Status -ne 'Stopped') -or ($service.StartType -ne 'Disabled'))
{
WriteWork "$($service.DisplayName) ($($service.Name)) $($service.StartType)... $($service.Status)"
}
else
{
WriteOK "$($service.DisplayName) ($($service.Name)) $($service.StartType)... $($service.Status)"
}
}
else
{
Write-Host " Service not found `"$name`"" -ForegroundColor DarkGray
}
}
}
function StopRogueProcesses
{
param($installPath, [bool] $shouldProcess)
$changes = 0
Write-Host
Write-Host 'Checking rogue processes' -ForegroundColor Yellow
$pchanges = $changes
Get-Process | ? { $_ -and $_.Path -and $_.Path.StartsWith($installPath) } | % `
{
$process = $_
$procName = $process.Name
$procPath = $process.Path
WriteWork "Killing `"$procName`" ($procPath)"
if (ShouldProcess1 $shouldProcess "Stop-Process -Name $procName -Force")
{
Stop-Process -Name $procName -Force
}
$changes++
}
if ($pchanges -eq $changes)
{
WriteOK 'No rogue processes found'
}
return $changes
}
function UnhideInstallationFolder
{
param($installPath, [bool] $shouldProcess)
$changes = 0
if ($installPath.EndsWith('\'))
{
$installPath = $installPath.Substring(0, $installPath.Length - 1)
}
Write-Host
Write-Host 'Checking installation folder' -ForegroundColor Yellow
if (Test-Path "$installPath`-HIDDEN")
{
WriteWork 'Unhiding installation folder'
if (ShouldProcess1 $shouldProcess "Rename-Item `"$installPath`-HIDDEN`" `"$installPath`"")
{
Rename-Item "$installPath-HIDDEN" $installPath
}
$changes++
}
else
{
WriteOK 'Folder already visible'
}
return $changes
}
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
function ShouldProcess1
{
param([bool] $shouldProcess, $text)
if (-not $shouldProcess)
{
Write-Host "TEST: $text" -ForegroundColor DarkGray
}
return $shouldProcess
}
function WriteOK ($string)
{
$greenCheck = @{ Object = [Char]8730; ForegroundColor = 'Green'; NoNewLine = $true }
Write-Host ' ' -NoNewline
Write-Host @greenCheck
Write-Host " $string" -ForegroundColor Gray
}
function WriteWork ($string)
{
$redTriangle = @{ Object = [Char]9658; Foreground = 'Red'; NoNewLine = $true }
Write-Host ' ' -NoNewline
Write-Host @redTriangle
Write-Host " $string" -ForegroundColor Gray
}
Export-ModuleMember -Function *