Device Renaming with PowerShell API

JB
Jeff Burns
Aclara (formerly Smart Grid Solutions, LLC)

I have been reading through these forums and finding a lot of help regarding API request. So far I'm able to request my API token via PS and use it for device management!  

I'm fairly new to using API calls in PS and I can't seem to get around renaming a device.  I' almost certain the issue is with how I'm using the -Body portion of the PS code. 

Here is what I have so far:

#ALLOWS USER INPUT OF DEVICE NAME

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$title = 'Device Rename'
$msg = 'Enter New Device Name:'
$Input = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

$Header2 = @{}
$Header2["Authorization"] = "Bearer " + $Token

try
{
$response2 = Invoke-restmethod -ContentType "application/json" -Uri https://$MCFQDN/mobicontrol/api/devices/$DevID/actions -Method POST -Headers $Header2 -Body {\"Action\": \"Rename\",\"Name\" : \"$Input\"}


}
catch
{
$($_.Exception.Message)
}

As stated, I think the -Body section is where I'm failing, not sure how to convert the curl request to a body in PowerShell.

Greatly appreciate any help!

5 years ago
Windows
ANSWERS
S
Scott
5 years ago

Probably easier to determine if you include the code and content of the server response.

JB
Jeff Burns
5 years ago

Sorry, I never updated this.  I was able to get the correct body using the following: 


$Body2 = "{`"Action`": `"Rename`",`"Name`" : `"$Input`"}"

Works like a charm!

D
DDMOD@SOTI
5 years ago

Hi Jeff Burns,

Thank you for requesting an answer from SOTI.

Can you please confirm if the above code works for you to rename the device?