|
# Run piactl.exe and get the current open port |
|
function Get-PiaPortForwarded { |
|
# $output = cmd /c "C:\Program Files\Private Internet Access\piactl.exe" $args 2`>`&1 |
|
# $output = Get-ProcessOutput -FileName "cmd.exe" -Args "/c ping localhost" |
|
# $output = [string] (cmd /c "C:\Program Files\Private Internet Access\piactl.exe" $args 2>&1) |
|
$output = (cmd /c 'C:\Program Files\Private Internet Access\piactl.exe' get portforward) | Out-String |
|
$output.trim() |
|
} |
|
|
|
# Check if the process qbittorrent.exe is running |
|
$process = Get-Process -Name "qbittorrent" -ErrorAction SilentlyContinue |
|
if ($process) { |
|
# We could just kill it and restart it with the correct port |
|
# Instead we'll utilize the the Web API to set the port |
|
# if it is running, kill it |
|
# $process | Stop-Process -Force |
|
# & 'C:\Program Files\qBittorrent\qbittorrent.exe' --torrenting-port=3456 |
|
} |
|
else { |
|
# Quit if it's not running |
|
Write-Host "qBittorrent is not running" |
|
exit |
|
} |
|
|
|
|
|
# if $output is "Inactive" or "Attempting" then wait 2 seconds and try again, stop trying after 10 times and exit |
|
$output = Get-PiaPortForwarded |
|
if ($output -notmatch "^\d+$") { |
|
$i = 0 |
|
do { |
|
Write-Host "Port forwarding is '$output' [Trying Again $i/10]" |
|
Start-Sleep -Seconds 2 |
|
$output = Get-PiaPortForwarded |
|
$i++ |
|
} while (($output -notmatch "^\d+$") -and $i -lt 10) |
|
} |
|
|
|
# if $output is not a number then exit |
|
if ($output -notmatch "^\d+$") { |
|
Write-Host "Did not find port forwarding for: '$output'" |
|
exit |
|
} |
|
|
|
|
|
# if $output is a number then run qBittorrent's API to set the listening port |
|
|
|
Write-Host "Found Forwarded Port: $output" |
|
|
|
Invoke-WebRequest -Uri "http://localhost:9001/api/v2/app/setPreferences" ` |
|
-Method "POST" ` |
|
-Body "json=%7B%22listen_port%22%3A%20$output%7D" |
|
|
|
Write-Host "Port Forwarding Finished" |
|
|
|
# Write-Host "Press any key to continue..." |
|
# $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |