Public/Add-specDeviceToTVGroups.ps1

function Add-specDeviceToTVGroups {
    <#
    .SYNOPSIS
        Adds a TeamViewer device to the specified groups.
 
    .DESCRIPTION
        The Add-specDeviceToTVGroups function adds a TeamViewer device to the specified groups based on the provided parameters.
        It queries the registry for the ManagementID, matches required groups with their IDs, and adds the device to each group.
 
    .PARAMETER MachineName
        Specifies the name of the machine. Defaults to the environment variable COMPUTERNAME.
 
    .PARAMETER TableName
        Specifies the name of the Azure table.
 
    .PARAMETER StorageAccount
        Specifies the name of the Azure Storage Account.
 
    .PARAMETER SASToken
        Specifies the Shared Access Signature (SAS) token for accessing the Azure Storage Account.
 
    .PARAMETER TVAPIKey
        Specifies the API key for TeamViewer.
 
    .PARAMETER MissingGroups
        Specifies an array of missing TeamViewer groups.
 
    .PARAMETER AllGroups
        Specifies an array of all TeamViewer groups.
 
    .EXAMPLE
        $currentGroups = Get-specCurrentGroups
        $requiredGroups = Get-specRequiredTVGroups -Persona "ExamplePersona" -TableName "ExampleTable" -StorageAccount "ExampleAccount" -SASToken "ExampleToken"
        $missingGroups = Get-specMissingGroups -CurrentGroups $currentGroups -RequiredGroups $requiredGroups
        Add-specDeviceToTVGroups -MachineName "ExampleMachine" -TableName "ExampleTable" -StorageAccount "ExampleAccount" -SASToken "ExampleToken" -TVAPIKey "ExampleAPIKey" -MissingGroups $missingGroups -AllGroups $allGroups
        Adds the TeamViewer device to the specified groups.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0
 
    #>


    [cmdletbinding()]

    param (
        [parameter (mandatory = $false)]
        [string]$MachineName = $env:COMPUTERNAME,

        [parameter (mandatory = $true)]
        [string]$TVAPIKey,

        [parameter (mandatory = $true)]
        [array]$MissingGroups,

        [parameter (mandatory = $true)]
        [array]$AllGroups,

        [parameter (mandatory = $true)]
        [string]$ManagementID
    )


    $commaSeparatedArray = $MissingGroups -join ', '
    Write-Host "Found assigned required groups based on persona: $commaSeparatedArray`n" -ForegroundColor DarkGray

    # Match the required groups with their IDs
    $groupIDsToAdd = $missingGroups | ForEach-Object {
        $groupName = $_
        ($allGroups.resources | Where-Object { $_.name -eq $groupName }).id
    }

    # Add the device to each group

    Write-Host "Adding device to required groups`n" -ForegroundColor DarkGray
    foreach ($groupID in $groupIDsToAdd) {
        Write-Host "Processing management ID: [$managementID]" -ForegroundColor DarkGray
        # Below line needs to be uncommented to make live API calls
        $addResult = Add-specComputerToTVGroup -GroupID $groupID -ManagementID $managementID -APIKey $TVAPIKey

        if ($addResult -eq 1) {
            Write-Host "Failed to add device to group with ID [$groupID]" -ForegroundColor DarkYellow
            return 1
        }

        Write-Host "Added device to group with ID: $groupID`n" -ForegroundColor DarkGray
    }

    return $allGroups
}