Skip to content

Instantly share code, notes, and snippets.

@Taloth
Created July 10, 2017 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taloth/e02e76bcb29942f791cc2e8595192846 to your computer and use it in GitHub Desktop.
Save Taloth/e02e76bcb29942f791cc2e8595192846 to your computer and use it in GitHub Desktop.
DownloadedEpisodesScan example PowerShell
param($FilePath, $InfoHash, $Label)
############
# Settings
# Url to Sonarr api, should include UrlBase is specified.
$SonarrApiRootUrl = "http://testsonarr:8989/api"
# ApiKey as specified in System->General->Api Key
$SonarrApiKey = "4e6e7bff2e2431907b15e26e6b7ee792"
# Whether to wait for completion of the command
$WaitForCompletion = $true
# Whether to log the Responses
$Verbose = $true
############
$ErrorActionPreference = 'Stop'
# TODO: Filter on Label?
$url = "$SonarrApiRootUrl/command";
$command = @{
'name' = "DownloadedEpisodesScan"
'path' = $FilePath
'importMode' = 'Move'
'downloadClientId' = $InfoHash
} | ConvertTo-Json
if ($SonarrApiRootUrl -match 'testsonarr' -or $SonarrApiKey -eq '4e6e7bff2e2431907b15e26e6b7ee792') {
Write-Error "Change the Settings at the top of the script..."
}
Write-Host "Sending Request to Sonarr: Url=$url Content=$command"
$response = Invoke-RestMethod -Uri $url -Method Post -Body $command -Headers @{"X-Api-Key"=$SonarrApiKey} -MaximumRedirection 0 -Verbose:$Verbose
if (-not $response) {
Write-Error "Failed to submit command"
}
$response | Format-List | Out-String | Write-Verbose -Verbose:$Verbose
# We need to the command ID to be able to query the status
$commandId = $response.id
# A bit of advanced logic to see if the command has already been completed, we're only looping if $WaitForCompletion is set to $true
while ($response -and $response.status)
{
Write-Host "Checking status..."
$response = Invoke-RestMethod -Uri "$url/$commandId" -Method Get -Headers @{"X-Api-Key"=$SonarrApiKey} -MaximumRedirection 0 -Verbose:$Verbose
if (-not $response) {
Write-Error "Failed to get status from command"
break
}
$response | Format-List | Out-String | Write-Verbose -Verbose:$Verbose
if (@('queued', 'started', 'completed') -notcontains $response.status) {
Write-Error "Command $($response.status): $($response.exception)"
break
}
if ($response.status -eq 'completed') {
Write-Host "Command finished"
break
}
if (-not $WaitForCompletion) {
break
}
Start-Sleep -Milliseconds 1000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment