#Looking for help with my Powershell script

1 messages · Page 1 of 1 (latest)

void heron
#

Hi i have this PowerShell script that I had been using for Quite a while, this takes what i have watched via emby and makes it unmonitored in sonarr
I'm trying to bring it up to date, and i have no contact with the original author, i have done my best with the limited knowledge i have and now I'm stuck and looking for help

here is the snippet from the PowerShell script for the changing from true to false, I'm sure I'm missing something really simple but i can't see it so asking here if any one knows,

Using PowerShell 7.3

If (($null -ne $Sonarr_EpisodeId) -and ($Sonarr_Monitored -eq $true))
                    {
                        $time = Get-Date -Format F
                        Log -logString $("[$time] - Sonarr Episode ID Found: "+$Sonarr_EpisodeId)
                        $data = @{
                                    SeriesId = $Sonarr_SeriesId
                                    id = $Sonarr_EpisodeId
                                    monitored = 'false'
                                }
            
                        # Update Sonarr monitored status's
                        Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId"&apikey="$Sonarr_Api_Key -Method Put -Body (ConvertTo-Json -InputObject $data) | Out-Null
                        Log -logString $("[$time] - Sonarr Episode ID: "+$Sonarr_EpisodeId.ToString("#####0").PadRight(6)+" Monitored Status Updated to False.")
                    }```

this is the line in the script 
```ps
Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId"&apikey="$Sonarr_Api_Key -Method Put -Body (ConvertTo-Json -InputObject $data) | Out-Null

that errors out on with a media not supported error

Edit: I have managed to rewrite the above line but with a new error

Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId"&apikey="$Sonarr_Api_Key -Method Put -Body ConvertTo-Json (ConvertTo-Json $data) | Out-Null

BUt now gives me an error stating

Line | 672 | … Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeI … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | A positional parameter cannot be found that accepts argument '{ "seriesId": 585, "id": 54357, "monitored": "false" }'.

It's just this one line that i am now having issues with, i just can't get it to write the change for the monitored tag

any help would be great?

dense bobcat
#

this snippet is not valid: ```ps
Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId"&apikey="$Sonarr_Api_Key -Method Put -Body ConvertTo-Json (ConvertTo-Json $data) | Out-Null

You need to drop the duplicate `ConvertTo-Json`. 

```ps
Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId&apikey=$Sonarr_Api_Key -Method Put -Body (ConvertTo-Json $data) | Out-Null
#

I would guess that you need ```ps
Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId&apikey=$Sonarr_Api_Key -Method Put -Body (ConvertTo-Json -InputObject $data) -ContentType application/json | Out-Null

void heron
#

ok thanks, yeh that change didn't help, maybe it is the api

dense bobcat
#

you might consider grabbing something like Fiddler so you can inspect requests and compare between the thing that does work and the thing that does not

covert apex
#

I dont know if you are using the v4 beta api or not, but here is some updated code that should work if you are.
If you can link me to the github repo or were you got the code it would be nice to see the rest of the code.

#
$Headers = @{
    'X-Api-Key' = $Sonarr_Api_Key
}
if (($null -ne $Sonarr_EpisodeId) -and ($Sonarr_Monitored -eq $true)) {
    Log -LogString "[$(Get-Date -Format F)] - Sonarr Episode ID Found: $Sonarr_EpisodeId"
    $Data = @{
        seriesId  = $Sonarr_SeriesId
        id        = $Sonarr_EpisodeId
        monitored = $false
    } | ConvertTo-Json -Depth 50

    # Update Sonarr monitored status's
    Invoke-RestMethod -Uri "$GetSonarrEpisodes$Sonarr_EpisodeId" -Method Put -Body $Data -Headers $Headers -ContentType 'application/json'
    Log -LogString "[$(Get-Date -Format F)] - Sonarr Episode ID: $($Sonarr_EpisodeId.ToString() -f "#####0")       Monitored Status Updated to False."
}
#

Using string concatenation is not necessary you can use a string literal for the Uri parameter and the api key can be sent via the X-Api-Key header.

#

The positional parameter issue is probably due to a parameter input not being syntactically correct. So powershell gets confused where that parameter ends and the next one begins.

void heron
#

Hi,

yes using V4 Beta, i have no contact with the original author or where i got the files i just have them and been using for a while, i have tried googling the authors name that's in the script but no luck there,

i just tried what you provided above but get new error (see picture)

when i'm back later i'll upload the scripts to my github

void heron
#

@covert apex i have just made the GitHub repository public on my GitHub, i have the original files that i was using for V3 and also a folder for the new V4Beta which currently holds the changes you sent me above, plus all the changes i had already done to get to the point of changing the status to false
https://github.com/scalda/EmbyToSonarr
i hope you can look and see what i have or was doing wrong one so i can learn and two so it can work

GitHub

To update the monitored status of Episodes in Sonaar that have been watched in EmbyServer - GitHub - scalda/EmbyToSonarr: To update the monitored status of Episodes in Sonaar that have been watched...

void heron
#

Looking for help with my Powershell script

covert apex
#

Goodness what an eyesore this code is. @void heron It will take me a little bit but I am willing to work it out for you. I could use the practice. Ideally if you have time we should have a call about this and you can help me understand the purpose of the code, and what you know is a problem / what you want it to be. It would also be beneficial for me to be able to properly test the code meaning I would need your help to expedite setting up what you have currently. I will rewrite it completely once I can understand this. I have already cloned the repo and created a branch. Let me know when you have time.

void heron
#

thanks for the help, i don't think a rewrite of all of it is in order, i did just make a new commit to the github, where i have added the a new part to sonarrs api that allows is used for the monitoring of some thing but the -body doesn't like the $episodesid tag in the -body

covert apex
#

You should use the $Data variable for the body.

#

if the incorrect keys are present modify the hashtable to have the correct keys.

#

I am willing to do this properly, and make sure it will continue to work for the forseeable future. If you prefer to try to fix this code thats fine but I am offering full help to make it best practice code.

#

'{"episodeIds": [$Sonarr_EpisodeId],"monitored": false}'
using brackets "[]" around $Sonnar_EpisodeId would make the command read the EpisodeIds as an array. But, using single quotes arround the whole string means that the variable $Sonar_EpisodeId cannot be expanded to its value.

void heron
#

Yeah i tried the $data varible but it didn't like it,

i just set an actaully episode id in the -body and it worked and gave me feedback correctly in the console and did set the episode to false in sonarr, so yes it's just geting the -body correct i think think with the $data now that it's using the correct part of the api

-Body '{"episodeIds": [4400],"monitored": false}'

covert apex
#

So you used a number instead of the variable

#

thats the thing I was just saying was the problem.

#

The variable couldnt be used because of the single quotes around the whole string

void heron
#

ok i see the issue with that now

#

just changed to this -Body "{"episodeIds": $Sonarr_EpisodeId,"monitored": false}" And it now gets the id but powershelll isn't happy

covert apex
#
$Data = @{
    episodeIds = @( $Sonarr_EpisodeId )
    monitored  = $false
} | ConvertTo-Json -Depth 50

# Update Sonarr monitored status's
Invoke-RestMethod -Uri "$setSonarrMonitor&apikey=$Sonarr_Api_Key" -Method Put -Body $Data -ContentType 'application/json'
#

try this

#

I edited it for the correct url

void heron
#

i see what i was doing wrong now with the $Data

covert apex
#

Cool, you should delete your post with the API key in it.

#

Ya, you can use an array literal for the episodeId thats the @()

#

Everything working now?

void heron
#

yeah that's what i was doing wrong, yes all seems to be working now,
if you want to go ahead and rewrite to learn something thats great but at the moment it works for me which is what's important to me and my data cap on my internet, but i can help if you want to rewrite

I will make these changes to the github and update it

thank you so much for your time and teaching me something

covert apex
#

Sure, if you think this code would be beneficial for other people I am happy to help re-write. Is it a common combination?

#

Emby and Sonarr?

void heron
#

it is common, but in powershell might not be so much thats the only thing

covert apex
#

What do you think people usually use?

void heron
#

python would perhaps be a more universal language to probably cover most platforms

#

just another thing how do we get the console side output like the attached picture to the logs as that doesn't show up in the logs

covert apex
#

Uh

void heron
#

as this is all i get in the log

[22 March 2023 16:27:27] - Sonarr Series ID Found: 556
[22 March 2023 16:27:27] - Sonarr Episode ID Found: 
[22 March 2023 16:27:27] - Sonarr Episode ID: 54062  Monitored Status Updated to False.
#

if you don't know that's fine it's not overly important, was just carious if it was a thing

covert apex
#

You want the object?

#

or?

void heron
#

if object is the green text base stuff then yes

covert apex
#

can you paste the console output?

void heron
#

[22 March 2023 16:35:23] - Episode to be Updated in Sonarr -> No.  65 Falling Skies                  Season 4   Episode 8   A Thing with Feathers                              Last Played Date: Wednesday, 22 March 2023 16:34:31
[22 March 2023 16:35:23] - Sonarr Series ID Found: 381
[22 March 2023 16:35:23] - Sonarr Episode ID Found: 38757
seriesId                 : 381
tvdbId                   : 4768094
episodeFileId            : 61397
seasonNumber             : 4
episodeNumber            : 8
title                    : A Thing with Feathers
airDate                  : 2014-08-10
airDateUtc               : 11/08/2014 02:00:00
runtime                  : 0
overview                 : The 2nd Mass picks up the pieces after a devastating alien attack leaves Maggie fighting for her life…and Hal fighting for Maggie. Meanwhile, Tom and Dingaan are cut off from the rest of the group, buried
                           under tons of rubble, where they must rely on an Enemy Ship to rescue them.
hasFile                  : True
monitored                : False
absoluteEpisodeNumber    : 38
unverifiedSceneNumbering : False
grabbed                  : False
id                       : 38757

[22 March 2023 16:35:23] - Sonarr Episode ID: 38757  Monitored Status Updated to False.```
covert apex
#
$Response = Invoke-RestMethod -Uri "$setSonarrMonitor&apikey=$Sonarr_Api_Key" -Method Put -Body $Data -ContentType 'application/json'
Log -logString $($Response | Format-Table -AutoSize)
#

try this

void heron
#

tried and didn't work, it's ok don't worry abut it, it's not important, thank you for all the help today

muted hornet
#

I am assuming that the Sonarr API does not require any header for authentication and authorization

For easy of debugging, I avoid unnecessary piping of CmdLets so you can more easily analyse each step in the code. For Example:

Invoke-RestMethod -Uri $getSonarrEpisodes$Sonarr_EpisodeId"&apikey="$Sonarr_Api_Key -Method Put -Body (ConvertTo-Json -InputObject $data) | Out-Null

$uri = "$($getSonarrEpisodes)$($Sonarr_EpisodeId)&apikey=$($Sonarr_Api_Key)"
Write-Host ("Using: $uri")
Write-Host ("Data: $($data | Out-String)")
$Results = Invoke-RestMethod -Uri $uri -Method Put -Body ($data | ConvertTo-Json -Depth 99)
Write-Host ("Results"`n$($Results | Out-String)")

Many APIs return an Error Code in the Results which it should not be discarded using | Out-Null
The Log Strings can be better formatted. Here are two Approaches

$logString = "[$time] - Sonarr Episode ID: $($Sonarr_EpisodeId.ToString("#####0").PadRight(6)) Monitored Status Updated to False.")
$logString = "[{0}] - Sonarr Episode ID: {1} Monitored Status Updated to False." -f $time, $Sonarr_EpisodeId.ToString("#####0").PadRight(6)