Last active
June 20, 2023 21:57
-
-
Save araujofrancisco/d601d0781e0ef4f90b67ec378293302c to your computer and use it in GitHub Desktop.
Start/Shutdown a set of Tasks/Processes Using Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the start parameter from the CLI | |
param( | |
[switch]$Start, | |
[switch]$Shutdown | |
) | |
# stop a list of processes | |
function Stop-ProcessList([String[]] $processes) { | |
foreach ($process in $processes) { | |
if (Get-Process -Name $process -ErrorAction SilentlyContinue) { | |
# process is running, so we stop it | |
Write-Host "$process is running. Stopping it now..." | |
Stop-Process -Name $process | |
} | |
} | |
} | |
# include the processes you want to start/shutdown here | |
function Toggle-Process([bool]$Start) { | |
if ($Start) { | |
Write-Output "Starting T-Rex Miner" | |
Start-ScheduledTask -TaskName "\User\T-Rex" | |
# Start WSL | |
Write-Output "Starting WSL" | |
Start-Process -FilePath "wsl.exe" -ArgumentList "ls" -WindowStyle Hidden | |
} | |
else { | |
# Stop T-Rex Miner | |
Write-Output "Stopping T-Rex Miner" | |
Stop-ScheduledTask -TaskName "\User\T-Rex" | |
# Shutting down wsl | |
Write-Output "Shutting down WSL" | |
wsl --shutdown | |
# Close selected applications | |
$processes = @("Spotify", "firefox", "OUTLOOK") | |
Stop-ProcessList $processes | |
} | |
} | |
# Check if the user wants to start or shutdown processes | |
if ($Start -and !$Shutdown) { | |
Write-Host "Starting processes" | |
Toggle-Process $true | |
} | |
elseif ($Shutdown -and !$Start) { | |
# Get confirmation from the user before shutting down processes | |
Add-Type -AssemblyName PresentationFramework | |
$msgBody = "Shutting down everything, are you sure?" | |
$msgTitle = "Process shutdown" | |
$msgButton = "YesNo" | |
$msgImage = "Question" | |
$result = [System.Windows.MessageBox]::Show($msgBody, $msgTitle, $msgButton, $msgImage) | |
# Proceed with shutdown if the user confirms | |
if ($result -eq "Yes") { | |
Write-Host "Shutting down processes" | |
Toggle-Process $false | |
} | |
else { | |
Write-Host "Shutdown cancelled, bye." | |
} | |
} | |
else { | |
Write-Host "Please specify a valid command" | |
} | |
# Display help information if no command is specified | |
if (!$Start -and !$Shutdown) { | |
Write-Host "Usage: toggle [-start] [-shutdown]" | |
Write-Host "" | |
Write-Host "-start: Start the specified processes" | |
Write-Host "-shutdown: Shutdown the specified processes" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment