SCOM Rest API with SCOM 2022 UR1

Not sure if I’m just having a bad day but using the Rest API for the first time on a SCOM 2022 UR1 instance and having a problem.

I’m following the document in the house and it has worked a treat before:
Quick Start - SCOM REST API — Cookdown

But today I’m getting an error on this line - $CSRFtoken = $WebSession.Cookies.GetCookies($UriBase) | ? { $_.Name -eq ‘SCOM-CSRF-TOKEN’ }

“You cannot call a method on a null-valued expression.” which suggests an issue $WebSession.Cookies.GetCookies but the $URIBase is correct.

Just looking for a quick sanity check that it still works fine in SCOM 2022 UR1. It may well be a security setting somewhere that needs a tweak.

Thanks

Graham

OS = Windows 2019 Standard

Looking at that blog post it looks like the websession might be initialized after that call.

It’s used on that line, but not actually initialized until the Invoke-RestMethod a few lines later (-SessionVariable defines the variable that the websession object gets stored in: Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn).
This would fit the null reference error.

If you run it twice in the ISE/VSCode (VSCode judging by the screenshot on the blog) I’d guess it would work because these tend to hang on to variables, whereas scripts throw away the scope once they’re done.

Thanks - it did seem to be a strange to be calling the WebSession before it was initialised. I ended up using the following which works fine:

SCOM 2019 UR1 REST-API | stelianposteablog (wordpress.com)

1 Like