-1

I need generate token with "hostIDs" and "Fields" data. If I use only "hostIDs" it works but that token is invalid.

Below is the JSON body code which works with Postman but not in PowerShell.

{   
"hostIds":[8876767,6736742,0986374],    
"fields": ["ServiceTag","HardwareManufacturer","HardwareModel"]
}

Below JSON body works with Powershell only with 'hostIDs'. I also want to add another line to this body 'fields' which will fulfill the token generation. How to add multi-line?

$body = ConvertTo-Json @{   
          hostIds = 8876767,6736742,0986374
          
}

PowerShell code I am using for this API:-

#Credentials
$username = "xxxxxxx"
$password = "xxxxxxxxxxx"
$headers = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

#JSON Body
$body = ConvertTo-Json @{   
          hostIds = 8876767, 6736742,0986374             
}

$EndPointURI = 'https://secure.logmein.com/public-api/v1/inventory/system/reports'

$response = Invoke-RestMethod -Uri $EndPointURI -Method Post -Headers @{Authorization=("Basic {0}" -f $headers)} -Body $body -ContentType 'application/json'
$token = $response.token
  • It sounds like the issue is not with the code creating the JSON. Could you add the error message you're seeing? – derekbaker783 Jun 25 '20 at 09:56
  • There is no error message. I need help in putting JSON body with both **'hostIDs'** **'fields'** to post data using PowerShell. Where as it is working fine with only *hostIDs* Please see above PS code I am using – Chetan Naik Jun 25 '20 at 10:15

1 Answers1

0

The syntax would be like this:

$body = ConvertTo-Json @{   
    hostIds = @('8876767', '6736742', '0986374')
    fields = @('ServiceTag', 'HardwareManufacturer', 'HardwareModel')
}

Or like this:

$body = ConvertTo-Json @{   
    hostIds = @('8876767', '6736742', '0986374');
    fields = @('ServiceTag', 'HardwareManufacturer', 'HardwareModel');
}
derekbaker783
  • 8,109
  • 4
  • 36
  • 50
  • This gives error 'Invoke-RestMethod : {"message":"Invalid filed names.","fieldNames":["ServiceTag","HardwareManufacturer","HardwareModel"]}' – Chetan Naik Jun 25 '20 at 10:21
  • FYI This is the LogMeIn API I am working on: https://documentation.logmein.com/webhelp/EN/CentralDeveloper/LogMein_Central_Developer/LogMeIn_Central_POST_New_HardwareInventory_Report.html – Chetan Naik Jun 25 '20 at 10:23
  • @ChetanNaik, have you verified the fields you have available? https://documentation.logmein.com/webhelp/EN/CentralDeveloper/LogMein_Central_Developer/LogMeIn_Central_GET_HardwareInventory_List.html#topic_3AD6F38B73DE482A9FD720C18F90B68D What does this endpoint return? Also, what is the HTTP status code associated with the error you listed above? – derekbaker783 Jun 25 '20 at 10:27
  • Yes @derekbaker783. Fields are verified and works with Postman API tool. – Chetan Naik Jun 25 '20 at 10:36
  • The endpoint returns Token with just two rows like this : `{ "expires": "2020-06-25 10:41:12Z", "token": "AFNcTqchSF2Bdrtg5zfr5678d33" }` And this token I will use for next step to fetch 'Fields' data in the terms inventory – Chetan Naik Jun 25 '20 at 10:53