Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Gets the full queue of SABnzbd as a snapshot text file using the official API.
# Name: SABqueueSnapShot for PowerShell
# Decription: Gets the full queue of SABnzbd as a snapshot text file using the official API
# Version: 1.0B
# Required Edits: 1-4
# To troubleshoot issues ensure that all firewalls are configured properly :D
# Start Edits:
# Edit 1) Edit this to your API key in SABnzbd's General > Security settings section
# Edit 2) Edit this to the host IP of the device running SABnzbd
$hostSAB = ''
# Edit 3) Edit this to 9090 (secured connection or 8080 for insecured) or your custom set
$portSAB = 9090
# Edit 4) Edit this to the folder you want save text files to (must exist and must end in a backslash, \)
$outputPath = 'C:\Users\Dean\Documents\MEGAsync\SAB Queue Snapshots\'
$outputFilename = 'queue_snapshot_' + [DateTime]::Now.ToString("") + '.txt'
# Example filename output: ...\queue_snapshot_2017-12-08@19.02.42.txt
# Stop Edits.
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$outputTextFile = $outputPath + $outputFilename
$requestURL = "https://$hostSAB`:$portSAB/sabnzbd/api?output=xml&apikey=$apiKey&mode=queue"
$resultQueue = ''
# Makes the API request
try {
Write-Host 'Trying to connect to SABnzbd API...' -ForegroundColor Cyan
$resultQueue = Invoke-RestMethod -Uri $requestURL -Method Get
catch {
Write-Host 'Failed to connect to SAB, please check it is running.' `
-ForegroundColor Red
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Message:" $_.Exception.Message
# Get error code
$errorCode = $resultQueue.result.error
# Check error codes
if ($errorCode -eq 'API Key Required') {
Write-Host 'Please check your API key is entered.' `
-ForegroundColor Yellow
if ($errorCode -eq 'API Key Incorrect') {
Write-Host 'Please check your API key is correct.' `
-ForegroundColor Yellow
# No errors happened
# Loops through the XML result
Write-Host 'Getting files list from request...' -ForegroundColor Magenta
$output = ''
foreach ($queueItem in $resultQueue.queue.slots.ChildNodes) {
$output += $queueItem.filename + "`r`n"
if (Test-Path $outputPath) {
# Formats output and then saves to text file
$output = $output.Trim()
$output -creplace '(?m)^\s*\r?\n', '' | Out-File -FilePath $outputTextFile `
-Encoding utf8 -Force
Write-Host 'Saved queue to text file snapshot:'$outputTextFile `
-ForegroundColor Green
else {
Write-Host 'Failed to write output list.' -ForegroundColor Red
Write-Host 'Could not find the set output path:'$outputPath `
-ForegroundColor Red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment