Posted by mal_member
Photon

Delete a computer using Powershell

Anyone have a powershell script that deletes a computer using powershell?

7 Replies
Posted by Kentain
Trigon

Re: Delete a computer using Powershell

Should be something like:

$computer = "put the dID here"
$token = "Paste your api token here"

$bearer = "Bearer", $token
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization",$bearer)

Invoke-WebRequest  -Uri "https://webapi.teamviewer.com/api/v1/devices/$computer" -Method Delete -Headers $header

Posted by Nfoster
Tetragon

Re: Delete a computer using Powershell

This script does not work. Fails with "Invalid URI".

Posted by Kentain
Trigon

Re: Delete a computer using Powershell

The script works, either you are doing it wrong or the site was unavailable, i get 204's.

For dID you probably put in the contact ID, complete with spaces, and not the actual device ID.

To get the dIDs, you can do something like this:
$thing = Invoke-WebRequest  -Uri "https://webapi.teamviewer.com/api/v1/devices/" -Method get -Headers $header
ConvertFrom-Json ($thing) | Select-Object -expand devices | ConvertTo-Csv -NoTypeInformation

Posted by Nfoster
Tetragon

Re: Delete a computer using Powershell

Thank you Kentain,

I have a script that was posted elsewhere but never got it to work with delete device in the TeamViewer portal. So I was trying your script to see if I could resolve my issue.

# Check to see if device already exists, prompt to delete it if it does
$devices = Invoke-RestMethod -Uri "$base_url/api/v1/devices" -Headers $headers
$existing_device = $devices.devices | where { $_.alias -eq $device_alias }
if ($existing_device) {
$title = "Delete Device"
$message = "A device with the name $device_alias already exists. Do you wish to delete it?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes the device $device_alias."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Cancels the current installation."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result) {
0 {
try {
Invoke-RestMethod -Method Delete `
-Uri "$base_url/api/v1/devices/$($existing_device.device_id)" `
-Headers $headers
}
catch {
throw "Error deleting device"
}
}
1 {
throw "Device already exists"
}
}
}

However, when I run this I get the following error;

Error deleting device
At C:\temp\DTV12.ps1:45 char:17
+ throw "Error deleting device"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Error deleting device:String) [], RuntimeException
+ FullyQualifiedErrorId : Error deleting device

Line 45 Char: 17 is refering to "throw "Error deleting device"" in my script.

Any thougts?

Posted by Kentain
Trigon

Re: Delete a computer using Powershell

Thoughts while having my morning coffee on vacation day and without a computer on hand to test the parts I’m less than familiar with, sent from my iPhone:

$base_url is missing definition before being called, and so is $header.. unless you skipped pasting them for brevity...

I don’t know the default method behaviour for Invoke-RestMethod, but it probably is GET anyway..

If it’s neither.. I’d need a computer
Highlighted
Posted by Nfoster
Tetragon

Re: Delete a computer using Powershell

Here is the script that I am using. Everything works with the exception of deleting an exsisting device.

Also try to find how to pass the "Customer Name" and "Y" when prompted to delete the devide within the script, not as a PS switch.

https://community.teamviewer.com/t5/TeamViewer-General/PowerShell-Based-Host-Deployment-msi/m-p/1415...

1 Reply
Posted by Kentain
Trigon

Re: Delete a computer using Powershell

I see..

You should post your concerns with that topic to that topic. I might take a look at that some day.