Quantcast
Channel: THWACK: All Content - Server & Application Monitor
Viewing all articles
Browse latest Browse all 12281

Anyone tried to use PowerShell script monitor with Invoke-RestMethod?

$
0
0

I'm trying to test web page with Powershell Script monitor using REST method as normal http for login monitor in SAM does not work against it.

PS v3 is installed on polling engine.

Script works and I get successful login on PowerShell console.

But when I try to use it on PowerShell Script monitor, login fails.

What I have found so far, $uri variable placing gets somehow mixed.

Output or $uri is "/rest/api/latest/mypermissionshttps://foobar.somewhere" when it shoud be: "https://foobar.somewhere/rest/api/latest/mypermissions"

If same kind of happens to other variables, it's no wonder that cannot work.

 

Is there some restrictions when using PS on SAM Component Monitor?

Or should PS get somehow initialized?

Sample code here:

 

$username = "UserName"
$password = "Password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr    param($Issue, $Credentials = $Credentials, $BaseURI = "https://foobar.somewhere")           function ConvertTo-UnsecureString(        [System.Security.SecureString][parameter(mandatory=$true)]$SecurePassword)    {        $unmanagedString = [System.IntPtr]::Zero;        try        {            $unmanagedString = [Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecurePassword)            return [Runtime.InteropServices.Marshal]::PtrToStringUni($unmanagedString)        }        finally        {            [Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($unmanagedString)        }    }        function ConvertTo-Base64($string) {       $bytes  = [System.Text.Encoding]::UTF8.GetBytes($string);       $encoded = [System.Convert]::ToBase64String($bytes);           return $encoded;    }        function ConvertFrom-Base64($string) {       $bytes  = [System.Convert]::FromBase64String($string);       $decoded = [System.Text.Encoding]::UTF8.GetString($bytes);           return $decoded;    }        function Get-HttpBasicHeader($Credentials, $Headers = @{})    {            $b64 = ConvertTo-Base64 "$($Credentials.UserName):$(ConvertTo-UnsecureString $Credentials.Password)"            $Headers["Authorization"] = "Basic $b64"            return $Headers    }        if($Issue) {            $uri = "$BaseURI/rest/api/latest/issue/$Issue"    } else {            $uri = "$BaseURI/rest/api/latest/mypermissions"    }        $headers = Get-HttpBasicHeader $Credentials    $Result = Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"       if ($Result) {        Write-Host "Statistic.0: 1"        Write-Host "Message.0: Login successful."        Exit 0    }    else {        Write-Host "Statistic.1: 1"        Write-Host "Message.1: Login failed."        Exit 2       }

Viewing all articles
Browse latest Browse all 12281

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>