Private/Get-specAllTVGroups.ps1

Function Get-specAllTVGroups {
    <#
    .SYNOPSIS
        Retrieves all managed groups from TeamViewer using the provided API key.
 
    .DESCRIPTION
        The Get-specAllTVGroups function fetches all managed groups from TeamViewer using the specified API key for authentication.
        It sends a GET request to the TeamViewer API endpoint and retrieves a list of all groups associated with the provided API key.
        It returns 1 if there is any type of error.
 
    .PARAMETER APIKey
        Specifies the TeamViewer API key for authentication. This parameter is mandatory.
 
    .EXAMPLE
        $allGroups = Get-specAllTVGroups -APIKey "YourTeamViewerAPIKey"
        Retrieves all managed groups from TeamViewer and stores the result in the variable $allGroups.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0
 
    #>


    [cmdletbinding()]

    param (
        [Parameter(Mandatory = $true)]
        [string]$APIKey
    )

    # Define the URL and token
    $url = "https://webapi.teamviewer.com/api/v1/managed/groups"
    $token = $APIKey

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

    # Fetch all managed groups
    try {
        $allGroups = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ea stop
    } catch {
        Write-Verbose "Unable to retrieve all groups from TeamViewer. Error: $_"
        Write-specLogMessage (($_ | ConvertFrom-Json).error_description) -Colour DarkYellow
        return 1
    }

    return $allGroups

}