8 years ago
Rest API and Powershell
So I have had pretty good luck converting the Python examples to Powershell.
The one thing I can not figure out how to do is return volume usage for a specific host.
Anyone got any example ...
I'm working on transitioning some of our custom reporting over from SCOM to LM as we transition. I'm having a difficult time getting REST to give me anything at all. I have an API Token created and have saved it into my credential store. I'm using the CredentialManager 2.0 module to access them. Here's the rest:
Import-Module CredentialManager
$Cred = Get-StoredCredential -Target LogicMonitor
$accessId = $cred.UserName
$accessKey = $cred.GetNetworkCredential().Password
$company = "ourcompany"
$httpVerb = "GET"
$resourcePath = "/device/devices"
$epoch = [Math]::Round(
( New-TimeSpan `
-start ( Get-Date -Date "1/1/1970" ) `
-end ( Get-Date ).ToUniversalTime() `
).TotalMilliseconds
)
<# Concatenate Request Details #>
$requestVars = $httpVerb + $epoch + $resourcePath
<# Construct Signature #>
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes( $accessKey )
$signatureBytes = $hmac.ComputeHash( [Text.Encoding]::UTF8.GetBytes( $requestVars ) )
$signatureHex = [System.BitConverter]::ToString( $signatureBytes ) -replace '-'
$signature = [System.Convert]::ToBase64String( [System.Text.Encoding]::UTF8.GetBytes( $signatureHex.ToLower() ) )
<# Construct Headers #>
$auth = "LMv1 $accessId`:$signature`:$epoch"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add( "Authorization",$auth )
$headers.Add( "Content-Type" ,'application/json' )
<# Construct URL #>
$url = "https://$company.logicmonitor.com/santaba/rest$resourcePath"
<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers
I'm getting this response:
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:19
+ ... nse = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $he ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
what am I missing here? All of the parts look right going into the invoke:
PS C:\Users\cole.mcdonald> $url;$httpVerb;$headers
https://beyondimpact.logicmonitor.com/santaba/rest/device/devices
GET
Key Value
--- -----
Authorization LMv1 m27K656aHIjt946ST92q:ZGMzNjk5NTIyZDE2YzMyYTBiOTc0MTFiM2U4MGVmOGQyMWNjNGRiOWNmZDJjNjU2Y2VjYjQxNWViOTg2MDI0NQ==:1553558842750
Content-Type application/json
(Moderator: please feel free to move this reply if necessary - Not sure if you want this in a new thread or not.)