简体   繁体   中英

How to use below JSON body in powershell for invoke-restmethod

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

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM