Powershell script Invoke-RestMethod : Operation has timeout

Hi

I am trying to read the service health of office 365 applications using the template office 365 service communication API .

When i run the script from the server has administrator i get the output and script works fine. but when i run the same script from the solarwinds  SAM console it give me timeout error.

I have noticed that the Invoke-RestMethod command in the script is timing out or not able to authenticate. Execution mode for the script is "Local Host".

Please advice how i can fix this issue .

#Pass Credentials from Solarwinds
Function SecureStringToString($value)
{
[System.IntPtr] $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($value);
try
{
[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);
}
finally
{
[System.Runtime.InteropServices.Marshal]::FreeBSTR($bstr);
}
}
$c = Get-Credential -credential ${CREDENTIAL}
[string] $username = $c.Username
[string] $password = SecureStringToString $c.Password

# Objects
$service = $args[0]
$tenantId = $args[1]
$client_id = $username
$client_secret = $password


# Construct URI for OAuth Token
$uri = "">login.microsoftonline.com/.../token"

# Construct Body for OAuth Token
$body = @{
client_id = $client_id
scope = "">manage.office.com/.default"
client_secret = $client_secret
grant_type = "client_credentials"
}

# Get OAuth 2.0 Token
$tokenRequest = try {

Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -ErrorAction Stop

}
catch [System.Net.WebException] {

Write-Warning "Exception was caught: $($_.Exception.Message)"

}

$token = $tokenRequest.access_token

$o365status = try {

Invoke-RestMethod -Method Get -Uri "">manage.office.com/.../CurrentStatus" -ContentType "application/json" -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop

}
catch [System.Net.WebException] {

Write-Warning "Exception was caught: $($_.Exception.Message)"

}

# Get Office 365 Message
$o365message = try {

Invoke-RestMethod -Method Get -Uri "">manage.office.com/.../Messages" -ContentType "application/json" -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop

}
catch [System.Net.WebException] {

Write-Warning "Exception was caught: $($_.Exception.Message)"

}

$Status = $o365status.Value | ? {$_.Id -eq $Service}
$IncidentIds = $Status.IncidentIds

if ($Status.status -match "ServiceOperational") {$Stat = 99}
if ($Status.status -match "FalsePositive") {$Stat = 6}
if ($Status.status -match "ExtendedRecovery") {$Stat = 3}
if ($Status.status -match "InformationUnavailable") {$Stat = 8}
if ($Status.status -match "PIRPublished") {$Stat = 7}
if ($Status.status -match "RestoringService") {$Stat = 2}
if ($Status.status -match "ServiceDegradation") {$Stat = 1}
if ($Status.status -match "ServiceInterruption") {$Stat = 0}
if ($Status.status -match "Investigating") {$Stat = 4}
if ($Status.status -match "ServiceRestored") {$Stat = 5}


#Write-host 'Statistic.Status: '$Stat
#write-host 'Message.Status: '$Status.StatusDisplayname
Write-host 'Statistic: '$Stat

if ($Status.status -notlike "ServiceOperational") {
$Result = foreach ($ID in $IncidentIds) {$MessageID = $o365message.Value | where-object {$_.ID -eq $ID}
$Output = '<p>' + $MessageID.Status + ' - ID: ' + $MessageID.ID + ' - ' + $MessageID.ImpactDescription + '</p>'
$Output
}
write-host 'Message: '$Result
}
Else {
write-host 'Message: '$Status.StatusDisplayname
}
exit

=====================================================================================================================================
Errors: ==============================================
Exception was caught: The operation has timeout.
Exception was caught: Unable to connect to the remote server
Exception was caught: Unable to connect to the remote server
=====================================================================================================================================
Parents Reply Children
No Data