Gracefully shutdown Qbit via Powershell?
Posted: Sat Mar 29, 2025 11:39 pm
I've searched far and wide and haven't had any luck with this yet.
Right now, PIA is randomly assigning me a new port 5 -10 times per day. It's a pain to have to go in, get the new port, open connection settings, and enter it over, and over, and over.
i found a script that will check if the ports match, kill the qbit task if not, rewrite the port and relauch qbit but that results in torrents showing as missing files since it's a task kill and not a graceful shutdown. If i shut down and relauch qbit manually, I don't have that issue. I see there's a shutdown in the Qbit WebUI API but i keep getting a 503 Forbidden error.
Does anyone know how to actually get Qbit to shut down gracefully via an API call?
Here's what i've got so far:
# Create a session to store cookies
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# Authenticate and capture cookies
$loginResponse = Invoke-WebRequest -Uri "$QB_URL/api/v2/auth/login" `
-Method Post `
-Body @{username=$USERNAME; password=$PASSWORD} `
-UseBasicParsing `
-WebSession $session
# Check if login was successful
if ($loginResponse.Content -match "Ok") {
Write-Output "Login successful. Shutting down qBittorrent..."
# Send shutdown command using POST instead of GET
try {
$shutdownResponse = Invoke-WebRequest -Uri "$QB_URL/api/v2/app/shutdown" `
-Method Post ` # <-- Changed to POST
-WebSession $session `
-UseBasicParsing
Write-Output "Shutdown command sent successfully."
} catch {
Write-Output "Failed to send shutdown command. Status Code: $($_.Exception.Response.StatusCode)"
}
} else {
Write-Output "Login failed. Please check your credentials and WebUI settings."
}
Right now, PIA is randomly assigning me a new port 5 -10 times per day. It's a pain to have to go in, get the new port, open connection settings, and enter it over, and over, and over.
i found a script that will check if the ports match, kill the qbit task if not, rewrite the port and relauch qbit but that results in torrents showing as missing files since it's a task kill and not a graceful shutdown. If i shut down and relauch qbit manually, I don't have that issue. I see there's a shutdown in the Qbit WebUI API but i keep getting a 503 Forbidden error.
Does anyone know how to actually get Qbit to shut down gracefully via an API call?
Here's what i've got so far:
# Create a session to store cookies
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# Authenticate and capture cookies
$loginResponse = Invoke-WebRequest -Uri "$QB_URL/api/v2/auth/login" `
-Method Post `
-Body @{username=$USERNAME; password=$PASSWORD} `
-UseBasicParsing `
-WebSession $session
# Check if login was successful
if ($loginResponse.Content -match "Ok") {
Write-Output "Login successful. Shutting down qBittorrent..."
# Send shutdown command using POST instead of GET
try {
$shutdownResponse = Invoke-WebRequest -Uri "$QB_URL/api/v2/app/shutdown" `
-Method Post ` # <-- Changed to POST
-WebSession $session `
-UseBasicParsing
Write-Output "Shutdown command sent successfully."
} catch {
Write-Output "Failed to send shutdown command. Status Code: $($_.Exception.Response.StatusCode)"
}
} else {
Write-Output "Login failed. Please check your credentials and WebUI settings."
}