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-specLogMessage "Found assigned required groups based on persona: $commaSeparatedArray`n" -Colour 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-specLogMessage "Adding device to required groups`n" -Colour DarkCyan
    foreach ($groupID in $groupIDsToAdd) {
        Write-specLogMessage "Processing management ID: [$managementID]" -Colour DarkCyan
        # 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-specLogMessage "Failed to add device to group with ID [$groupID]" -DoNotUseWriteHost
            return 1
        }

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

    return $allGroups
}