-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToggleAndLaunch.ps1
66 lines (57 loc) · 1.95 KB
/
ToggleAndLaunch.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
<#
.DESCRIPTION
Toggle a service on or off and run an application
.PARAMETER Service
What service you want to toggle ?
You can get the service name by using windows built-in Services app or use the Get-Service command
You can only toggle 1 service!
.PARAMETER App
What app you want to run after a service is toggled ?
You can get the app name by running the app normally and then using Get-Process command.
This can alse close the application, if the app is already running.
You can only run 1 app!
.EXAMPLE
.\ToggleAndLaunch.ps1 -Service 'Cloudflare WARP' -App 'Cloudflare WARP'
.\ToggleAndLaunch.ps1 -Service 'Cloudflare WARP'
.\ToggleAndLaunch.ps1 -App 'Cloudflare WARP'
.\ToggleAndLaunch.ps1
#>
param (
[ValidateNotNullOrEmpty()]
[String]$Service,
[String]$App
)
if (!$Service -and !$App) {
Write-Host "At least one of the Service or App must be provided." -ForegroundColor Red
Write-Host "Use this command to get help and examples : Get-Help .\ToggleAndLaunch -Full"
return
}
########################## Toggle Service ##########################
if ($Service) {
$ServiceName = Get-Service -Name $Service
$ServiceStatus = (Get-Service -Name $Service).Status
$ServiceDisplayName = (Get-Service -Name $Service).DisplayName
if ($ServiceStatus -eq "Running") {
Write-Host "Stop ${ServiceDisplayName} Service."
Stop-Service $ServiceName
}
elseif ($ServiceStatus -eq "Stopped") {
Write-Host "Start ${ServiceDisplayName} Service."
Start-Service $ServiceName
}
else {
Write-Host "${ServiceDisplayName} is not running or stopped."
}
}
########################## Launch or Close App ##########################
if ($App) {
$AppProcess = Get-Process $App -ErrorAction SilentlyContinue
if (!$AppProcess) {
Write-Host "Start $App Process."
Start-Process $App
}
elseif ($AppProcess) {
Write-Host "Stop $App Process."
Stop-Process -Name $App
}
}