-
Notifications
You must be signed in to change notification settings - Fork 1
/
Launcher.ps1
63 lines (38 loc) · 1.35 KB
/
Launcher.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
Param (
[switch]$elevated,
[switch]$start,
[switch]$stop,
[string]$path
)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
# decide what to do (no parameters given)
If (($start) -eq $false -and ($stop) -eq $false) {
$firstService = Get-Content -Path $path -TotalCount 1 | Get-Service -Name {$_}
If (($firstService.status) -eq "stopped") {
$start = $true
}
Else {
$stop = $true
}
}
# check for privileges
If ((Test-Admin) -eq $false) {
If ($elevated) {
# tried to elevate, did not work, aborting
}
Else {
$action = $(If ($start) {"start"} Else {"stop"})
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated -{1} -path {2}' -f ($myinvocation.MyCommand.Definition, $action, $path))
}
exit
}
# to be, or not to be, that is the question
If ($start) {
Get-Content $path | Get-Service -Name {$_} | Where-Object {$_.status -eq 'stopped'} | Start-Service -pass
}
Else {
Get-Content $path | Get-Service -Name {$_} | Where-Object {$_.status -eq 'running'} | Stop-Service -pass
}