Forum Discussion

Garry_Gearhart's avatar
3 years ago

PowerShell script template for API Get website/websites

I'm having some trouble getting website listing via API requests to complete via PowerShell and would welcome any feedback. This has been modified to be explicit for '/website/websites'. I have another version of the code below where $resourcepath is set to '/device/devices' and x-version is 1 that is successful but X-Version 1 fails with website requests using both tools. In Postman I can get a successful response with X-Version 3 but I'm not finding the differentiating factor between that and this script.

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

<# account info #>
$accessId = 
$accessKey = 
$company = 

<# request details #>
$httpVerb = 'GET'
$resourcePath = '/website/websites'

<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath

<# Get current time in milliseconds #>
$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("X-Version",'3')
$headers.Add("Content-Type",'application/json')

<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers
$response.ToString()
<# Print status and body of response #>
$status = $response.status
$body = $response.data| ConvertTo-Json -Depth 5

Write-Host "Status:$status"
Write-Host "Response:$body" 

 

  • Going off the examples on LogicMonitors website your script looks correct to me.   I am also having issues using their examples successfully.   I have a current open ticket with them.  

    Like you the same script with "/device/devices" works for me however "/website/websites" does not work.   I got a 404 error.  

    Where did you find to use x-version 3 in postman?  Not sure I have found the documentation on when to use what X-Version.   

  • Gary, after i posted the last comment i actually think i got it figured out.  Looking at the swagger looks like collectorIds is something you can query on.  So i looked at my websites (we have 30) and all are being monitored internally by our collector group.  So i just choose a collector and added these queryparams...

    Make sure you add the "+ $queryparams" on the back of your URL and let me know how you make out.  I am doing this with "X-Version 2"

     

  • Anonymous's avatar
    Anonymous

    To set the version in postman (or any API interaction), you can either set the "X-Version" header to the version you want to use:

    Or you can add a query parameter called "v" to the URL and specify the version: