Forum Discussion

kapcreations's avatar
kapcreations
New Member
3 years ago

Delete computers via PowerShell & API

I didn't find any examples online so I thought to post my working example I wrote to use PowerShell to interact with the API and delete stale computers.

 

TODO: automate the following manual Steps to occur prior to PS script

 

  1. Get a list of all Hosts in LogMeIn Central (separate script). Description contains FQDN of PC. This also correlates the PC name in the description field to the LMI ID.
  2. Get a list of all Windows devices in Intune (separate script). Append AD domain to PC name.
  3. Combine the results in XLSX. Find the delta using vlookup. Filter results for items in LMI but not in Intune.
  4. Manually edit LMI entries with "(1)", "(2)", etc in description. This will align LMI w/AD list.
  5. Add results to C:\temp\Delete-LogMeInCentralByID.csv, which is a CSV of just the LMI IDs.

So now you have a CSV file containing IDs you wish to delete from LMI Central.

 

Here's how I approached this. There is probably a more elegant way to improve some of my hacks.

 

 

#------------------------------------

$jsonFile = "c:\temp\output.json"

Remove-Item -Force $jsonFile

$myString = ""

 

#read the contents of the CSV 

$myHosts = import-csv -delimiter "`t" "C:\temp\Delete-LogMeInCentralByID.csv"

 

#iterate through the CSV and build an array of strings

$int = 0

foreach ($myHost in $myHosts)
{
if($int = 0)
{
$mystring += $myHost.id

}
else
{
$mystring += $myHost.id + ", "

}
$int = $int + 1
}

 

#get rid of the trailing ", "
$myString = $myString -replace ".{2}$"

 

# Convert comma separated string of numbers to array of numbers, with object type number.
$aryIds = $myString.split(',') | % {iex $_}

 

#write the JSON to file, mirroring the format required by LogMeIn-Central API from developer.logmein.com
@{hostIds=$aryIds} | ConvertTo-Json | Add-Content -Path $jsonFile

 

# we now have the json file of hostIds to delete.

# execute call from LogMeIn API


<# LMI-Central AUTHENTICATION #>
$un = '<get this from your API configuration>'
$pw = '<get this from your API configuration>'

 

# create the token

$token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $un,$pw)))

 

# create the header

$header = @{
'Authorization' = 'Basic ' + $token
}

 

#set URL (note, this works with v1, not v2)

$uri = "https://secure.logmein.com/public-api/v1/authentication"

 

# set the auth token

$auth = Invoke-WebRequest -method get -h $header -uri $uri

$newheader = @{
'Authorization' = 'Basic ' + $token
'Content-Type' = 'application/json'
}
<# END AUTHENTICATION #>

 

#set URI for the DELETE. Note, uses v1, not v2
$hosts_uri = "https://secure.logmein.com/public-api/v1/hosts"

 

# read the contents of the previously written json file.
$myjson = Get-Content -Path $jsonFile

 

# execute the delete statement in LMI using method Delete and json.
$delHosts = Invoke-RestMethod -Method Delete -Headers $newHeader -Uri $hosts_uri -Body $myjson

 

#this should not return anything

$delHosts