Delete a computer using Powershell

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

Comments

  • 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

  • Nfoster
    Nfoster Posts: 53 ✭✭

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

  • 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

  • Nfoster
    Nfoster Posts: 53 ✭✭

    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?

  • 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
  • Nfoster
    Nfoster Posts: 53 ✭✭

    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/14151#M7840

  • I see..

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