diff --git a/Invoke-QBTCleanarr.ps1 b/Invoke-QBTCleanarr.ps1 index b38a0e3..9ef17b1 100644 --- a/Invoke-QBTCleanarr.ps1 +++ b/Invoke-QBTCleanarr.ps1 @@ -22,6 +22,19 @@ function Get-QBittorrentSession { return $qbtSession } +# Function to check if the session is valid +function Test-QBittorrentSession { + param ( + [Microsoft.PowerShell.Commands.WebRequestSession]$session + ) + try { + $qbtResponse = Invoke-WebRequest -Uri "https://$($qbtHost):$($qbtPort)/api/v2/app/version" -WebSession $session -Method GET -ErrorAction Stop + return $true + } catch { + return $false + } +} + # Function to get all torrents function Get-Torrents { param ( @@ -64,6 +77,9 @@ function Remove-Torrent { $session = Get-QBittorrentSession while ($true) { + if (-not (Test-QBittorrentSession -session $session)) { + $session = Get-QBittorrentSession + } $torrents = Get-Torrents -session $session foreach ($torrent in $torrents) { @@ -76,7 +92,7 @@ while ($true) } if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) { # Delete the torrent - Write-Host "Delete: $($torrent.name) $($toreent.hash)" + Write-Host "Delete: $($torrent.name) $($torrent.hash)" Remove-Torrent -session $session -hash $torrent.hash -name $torrent.name } }