Private/Add-specComputerToTVGroup.ps1

Function Add-specComputerToTVGroup {
    <#
    .SYNOPSIS
        Adds a specific computer to a TeamViewer group using the provided API key and group ID.
 
    .DESCRIPTION
        The Add-specComputerToTVGroup function adds a computer, identified by its Management ID, to a specified TeamViewer group.
        It sends a POST request to the TeamViewer API endpoint, adding the computer to the specified group using the provided API key and group ID.
 
    .PARAMETER GroupID
        Specifies the ID of the TeamViewer group to which the computer will be added. This parameter is mandatory.
 
    .PARAMETER ManagementID
        Specifies the Management ID of the computer that needs to be added to the TeamViewer group. This parameter is mandatory.
 
    .PARAMETER APIKey
        Specifies the TeamViewer API key for authentication. This parameter is mandatory.
 
    .EXAMPLE
        Add-specComputerToTVGroup -GroupID "123456" -ManagementID "7890abcdef" -APIKey "YourTeamViewerAPIKey"
        Adds the computer with Management ID "7890abcdef" to the TeamViewer group with ID "123456" using the specified 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"

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

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

    try {
        $addDeviceResponse = Invoke-RestMethod -Uri $addDeviceUrl -Method Post -Headers $headers -Body $body -ea stop
        return $addDeviceResponse
    } catch {
        write-verbose "Failed to add device with managementID [$managementid] to group with ID: [$groupID]"
        return 1
    }
}