Skip to content

Instantly share code, notes, and snippets.

@taschmidt
Last active December 6, 2023 03:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save taschmidt/b2a86b9e5e1c41f05dd9fd4df7b3c5ce to your computer and use it in GitHub Desktop.
Save taschmidt/b2a86b9e5e1c41f05dd9fd4df7b3c5ce to your computer and use it in GitHub Desktop.
PIA set qBittorrent port forward
# adapted from https://www.reddit.com/r/PrivateInternetAccess/comments/njwip9/qbittorrent_and_private_internet_access_pia_vpn/
# requires running "Install-Module PsIni"
Import-Module PsIni
# get qBittorrent process
$qbProc = Get-Process qbittorrent -ErrorAction SilentlyContinue
# if qBit is running, it's closed
if ($qbProc) {
"qBittorrent is running. Stopping now..."
$null = Invoke-WebRequest -Uri http://localhost:8081/api/v2/app/shutdown -Method Post
"Waiting for qBittorrent to exit..."
$null = $qbProc.WaitForExit(60000)
$null = $qbProc.Refresh()
if (!$qbProc.HasExited) {
"qBittorrent didn't close. Terminating..."
Stop-Process -Id $qbProc.Id
$null = $qbProc.WaitForExit(15000)
$null = $qbProc.Refresh()
if (!$qbProc.HasExited) {
"ERROR: qBittorrent failed to close"
Exit 1
}
}
}
# Check the forwarded port by PIA
$piaPort = & "${env:ProgramFiles}\Private Internet Access\piactl.exe" get portforward
"PIA port forward is $piaPort"
# Replace line in the .ini file
"Modifying qBittorrent settings..."
$qbIniPath = "${env:AppData}\qBittorrent\qBittorrent.ini"
$qbIni = Get-IniContent $qbIniPath
$qbIni["BitTorrent"]["Session\Port"] = $piaPort
$qbIni | Out-IniFile -FilePath $qbIniPath -Force
"Pausing..."
Start-Sleep -Seconds 5
# start qBittorrent
"Starting qBittorrent..."
Start-Process -FilePath "${env:ProgramFiles}\qBittorrent\qbittorrent.exe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment