SCOM 2019 UR3 Invoke-RestMethod fails with agent, succeeds manually

D de Jong 1 Reputation point
2021-11-26T13:53:29.48+00:00

Hi, i've created below powershell script which queries the Rubrik API. When I run the script manually in ISE, it all works as expected. But when the script is executed by de SCOM agent, it fails on creating a session with Rubrik ($sessionResponse - last line in script). The returned error is: The underlying connection was closed: An unexpected error occurred on a send.

I've found on multiple sites that TLS1.2 should be enabled in the script, and self-signed certificates should be ignored.
I've implemented them both, but still the issue remains.

# Set protocol and ignore certificate issues
$source = @"
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;
    }
}
"@

# Ignore unsigned certificates
Add-Type -TypeDefinition $source
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

# Enable TLS 1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

# Building Rubrik API string
$baseURL = "https://" + $ipaddress + "/api/v1/"
$sessionURL = $baseURL + "session"
$queryURL = $baseURL + "host"
$type = "application/json"

# Create a session
$sessionHeader = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($queryUsr+":"+$queryPwd))}


# Try authenticating with Rubrik API
$sessionResponse = Invoke-RestMethod -Method POST -Uri $sessionURL -Headers $sessionHeader -ContentType $type

Any ideas what the issue could be?

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,458 questions
Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,413 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Georg Matviak 176 Reputation points
    2021-12-14T16:59:38.833+00:00

    Rubrik API is a product independantly from Microsoft. You should open a query to Rubrik support or community forums as they will be better experimented and knowledgeable about the set up of their API.

    Hope this helps with your query,


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Bob Cornelissen 251 Reputation points MVP
    2021-12-24T18:29:42.137+00:00

    Hello,
    I see this is happening when you are doing a POST.
    I did a webinar recently for Silect MP University about monitoring a Hue bridge through web api.
    Monitoring wise i could do everything using invoke-restmethod -uri and throwing in the correct string and it gave a json back which you can process.
    As soon as I wanted to interact with the devices to make it do something, such as turn on or off a light, the invoke-restmethod did not and would not do it.
    I had to use
    invoke-webrequest -method put -uri "theurl" -body "thebody"
    Im actually creating a set of blog posts about this, but got caught up with a load of work before I could finish it. Im 3 blog posts in and still need to create a few more. But Im sure the webinar at MP uni can be found and checked for what I mean (the second and fourth demo are about these subjects).
    Have a check if connecting using this second command type will work for you.

    0 comments No comments