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]$TableName,

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

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

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

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

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


    $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 "Querying registry for ManagementID" -Colour DarkCyan

    # query the registry for managementID - reg path is different depending if 32 bit or 64 bit device
    $managementID = Get-specRegistryValue -RegistryPath (Get-specRegistryPath)

    # Check if $managementID is null or empty
    if ($managementID -eq 1) {
        throw "ManagementId not found in the registry, or is empty. Exiting script."
    }

    # Sanitize the ManagementId by removing curly braces
    $managementID = $managementID -replace '[{}]', ''

    # Log the retrieved $managementID
    Write-specLogMessage "Retrieved: $managementID`n" -Colour DarkGreen

    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        
    }
    Write-specLogMessage "Device addition to groups completed`n" -Colour DarkGreen  
    return $allGroups   
}