Public/Remove-specDeviceFromTVGroup.ps1

Function Remove-specDeviceFromTVGroup {
    <#
    .SYNOPSIS
        Removes a TeamViewer device from a specified group.
 
    .DESCRIPTION
        The Remove-specDeviceFromTVGroup function removes a TeamViewer device from the specified group using the provided Group ID,
        Management ID, and API key. It sends a DELETE request to the TeamViewer API to perform the removal.
 
    .PARAMETER GroupID
        Specifies the ID of the TeamViewer group from which the device should be removed.
 
    .PARAMETER ManagementID
        Specifies the Management ID of the TeamViewer device to be removed from the group.
 
    .PARAMETER APIKey
        Specifies the TeamViewer API key to authenticate the removal operation.
 
    .EXAMPLE
        Remove-specDeviceFromTVGroup -GroupID "123" -ManagementID "456" -APIKey $secureApiKey
        Removes the TeamViewer device with Management ID "456" from the group with ID "123" using the provided API key.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0
 
    #>



    [cmdletbinding()]

    param (
        [string]$GroupID,

        [string]$ManagementID,

        [string]$APIKey
    )

    $url = "https://webapi.teamviewer.com/api/v1/managed/groups"
    $token = $APIKey

    $addDeviceUrl = "$url/$groupID/devices/$ManagementID"

    $body = @{
        id = $managementID
    } | ConvertTo-Json

    # Set up headers
    $headers = @{
        "Authorization" = "Bearer $token"
        "Accept"        = "application/json"
        "Content-Type"  = "application/json"  # Added this line
    }

    try {
        $removeDeviceResponse = Invoke-RestMethod -Uri $addDeviceUrl -Method Delete -Headers $headers -Body $body -ea stop
        return 0
    } catch {
        Write-specLogMessage "Failed to remove device with managementID [$managementid] from the group with ID: [$groupID]" -Colour DarkYellow
        Write-specLogMessage $_ -Colour DarkYellow
        return 1
    }
}