MobiControl 13.4 - syntax to delete Device ID via Powershell script

Solved
GG
Gerald Gilmore
Canadian Tire

Trying to automate device deletion on 13.4 using the invoke-restmethod API script provided here (which currently works for me for folder/group creation)

https://discussions.soti.net/thread/using-powershell-to-make-api-calls/

I can currently delete the deviceID via API rest interface, with no issues:

Curl: curl -X DELETE --header -Authorization: Bearer XYZ123

Request URL: https://servername:443/MobiControl/api/devices/DEVICEID

Response Body: no content

Response Code: 204

However when trying to use the same 'Invoke-RestMethod' method to delete the same Request URL, I receive a message of 'The underlying connection was closed: An unexpected error occurred on a send'. 

Can someone from SOTI confirm what the correct syntax should be for a request like this?

 
6 years ago
SOTI MobiControl
ANSWERS
GG
Gerald Gilmore
6 years ago

Looks like the following works. 

Invoke-restmethod -Uri  https://address/MobiControl/api/devices/$DeviceID -Method Delete -Header $Header2 -Body $Body

Solution
D
De_Johan
6 years ago

Can you share your entire script? I'm looking for something simular (trying to get devices deleted that aren't connected for 1 month)

GG
Gerald Gilmore
6 years ago (edited 6 years ago)

I'd first suggest reading through the details in that original post above, lots of good stuff posted in there. Essentially there are 3 steps:

1) Token Authentication

2) Read input from CSV file

3) Process CSV contents, in this case it's a delete

1) Token Script is almost identical to what was posted originally on the SOTI Forum

2) **********READ FROM CSV******************

   . .\Token.ps1
GetToken
$Token = GetToken

#Read from Stores file
$csv = import-csv deviceIDs.csv


##############LOOP THROUGH EACH ENTRY IN THE CSV###################
foreach($item in $csv)
{
$DeviceID = $item.DeviceID.ToString()

. .\DeleteID.ps1
DeleteDeviceID $DeviceID $Token
}

FORMAT THE CSV LIKE THIS:

DeviceID
DEVICEID1

DEVICEID2

3)  **********DELETE EACH DEVICE ID PASSING THE DEVICE ID FROM STEP2******************

function DeleteDeviceID ([string]$DeviceID, [string]$Token)
{

$MCUsername = "USERRNAME"
$MCPassword = "PASSWORD"
$MCFQDN = "MCFQDN:443"
$ClientID = "CLIENTID"
$ClientSecret = "CLIENTSECRET"

$IDSecret = $ClientID + ":" + $ClientSecret
$EncodedIDSecret = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($IDSecret))

$Body = @{}
$Body["grant_type"]="password"
$Body["username"]=$MCUsername
$Body["password"]=$MCPassword

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

#Delete the Device ID
try
{
Invoke-restmethod -Uri https://SERVERNAME:443/MobiControl/api/devices/$DeviceID -Method Delete -Header $Header2 -Body $Body
Write-Host "Deleted"
}
catch
{
$($_.Exception.Message)
}
}