Forum Discussion

Will_S's avatar
Will_S
Icon for Neophyte rankNeophyte
3 years ago

PowerShell API - Error 1007

The Error code points to a possible Duplicate.

That's all...that is the only error code I'm getting.  How can I debug the PS Script.  

The Data piece has the Object I'm adding to it but as you can see...this is a canned script.    Just need to know how to debug it.  the object is not in the API 

i only have the System name and Collector listed. If more needs to be listed, I can add that, the API scripting Info is vague so I'm using their example

**************************
Status:1007
Response:

***************************

 

 

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

<# 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 + $data + $resourcePath

$data = '{"name":"system","displayName":"system","preferredCollectorID":"8"}'

<# 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')

<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers

<# Print status and body of response #>
$status = $response.status
$body = $response.data| ConvertTo-Json -Depth 5

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

 

  • Anonymous's avatar
    Anonymous

    Their example doesn't actually include a value for $resourcePath. What is it you're trying to do with the API? Have you tried getting your call to work in postman first? Once you get it working in postman, you'll know you have the API details correct. Then you can work on putting them into a powershell script.

  • Thanks Stuart, I was using the Standard PowerShell API on the LM Controller.  
    I missed the part where Postman was required for this work.  I'll reach out to LM for more information.

  • Anonymous's avatar
    Anonymous

    LM Controller? You mean the Collector? Using PowerShell on the Collector doesn't actually get you anything you wouldn't have running powershell anywhere else (like your laptop). 

    What exactly are you trying to accomplish through the API? Knowing what your goal is can help us guide you to the right way of getting there.

    Postman is not required. It's just easier to compartmentalize the API call from the scripting. It lets you get the API call details sorted out without having to battle scripting challenges at the same time.

    Btw, here's a href="https://communities.logicmonitor.com/topic/1964-accessing-the-logicmonitor-rest-api-with-postman-and-lmv1-api-token-authentication/" rel="">information you'll need to get postman setup.