Public/Invoke-SpecTeamViewerConfig.ps1
function Invoke-SpecTeamViewerConfig { <# .SYNOPSIS Invokes TeamViewer configuration based on the detected device persona and assigns the device to specific groups. .DESCRIPTION The Invoke-SpecTeamViewerConfig function automates TeamViewer configuration by assigning devices to specific groups based on their detected persona. The function uses the machine name prefix to determine the persona and maps it to predefined groups in TeamViewer. It then adds the device to the corresponding TeamViewer groups using the provided API key. .PARAMETER APIKey Specifies the TeamViewer API key for authentication. This parameter is mandatory. .EXAMPLE Invoke-SpecTeamViewerConfig -APIKey "YourTeamViewerAPIKey" Invokes TeamViewer configuration based on the detected device persona and assigns the device to specific groups using the provided API key. .NOTES Version : 1.0 #> [cmdletbinding()] param ( [parameter (mandatory = $true)] [string]$APIKey ) # Determine the device persona based on the machine name prefix $machineName = hostname Log-Message "Detected machine name: $machineName" -Color "Darkcyan" $persona = if ($machineName -like "UKC-*") { "UKC" } elseif ($machineName -like "UKRO-*") { "UKRO" } elseif ($machineName -like "UKRS-*") { "UKRS" } elseif ($machineName -like "UKTO-*") { "UKTO" } else { "Unknown" } Log-Message "Determined device persona: $persona" -Color "Darkcyan" # Map personas to their respective groups $personaGroups = @{ "UKC" = @("S EMEA", "S EMEA RO", "S EMEA CC") "UKRO" = @("S EMEA", "S EMEA RO") "UKRS" = @("S EMEA", "S EMEA RR") "UKTO" = @("S EMEA", "S EMEA RR") } # Check if the persona exists in the hashtable if ($personaGroups.ContainsKey($persona)) { $requiredGroups = $personaGroups[$persona] } else { Log-Message "Unknown device persona detected. Exiting script." -Color "Red" exit 1 } Log-Message "Assigned required groups based on persona: $($requiredGroups -join ', ')" -Color "Darkcyan" # Define the URL and token $url = "https://webapi.teamviewer.com/api/v1/managed/groups" $token = $APIKey # Replace with your actual token # Set up headers $headers = @{ "Authorization" = "Bearer $token" "Accept" = "application/json" "Content-Type" = "application/json" # Added this line } # Fetch all managed groups (assuming you've defined $url, $token, and $headers already) $allGroups = Invoke-RestMethod -Uri $url -Method Get -Headers $headers # Match the required groups with their IDs $groupIDsToAdd = $requiredGroups | ForEach-Object { $groupName = $_ ($allGroups.resources | Where-Object { $_.name -eq $groupName }).id } # Add the device to each group $managementID = Get-TeamViewerManagementId # Log the retrieved $managementID Log-Message "Retrieved device ID (ManagementId) from registry: $managementID" -Color "Darkcyan" # Sanitize the ManagementId by removing curly braces $managementID = $managementID -replace '[{}]', '' # Log the retrieved and sanitized $managementID Log-Message "Retrieved and sanitized device ID (ManagementId) from registry: $managementID" -Color "Darkcyan" # Check if $managementID is null or empty if (-not $managementID) { Log-Message "ManagementId not found or is empty. Exiting script." -Color "Red" exit 1 } foreach ($groupID in $groupIDsToAdd) { $addDeviceUrl = "$url/$groupID/devices" $body = @{ id = $managementID } | ConvertTo-Json Log-Message "Request Body: $body" -Color "Yellow" # Log the body try { $addDeviceResponse = Invoke-RestMethod -Uri $addDeviceUrl -Method Post -Headers $headers -Body $body Log-Message "Added device to group with ID: $groupID" -Color "Green" } catch { $responseError = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($responseError) $errorMessage = $reader.ReadToEnd() | ConvertFrom-Json Log-Message "Failed to add device to group with ID $groupID. Error: $($errorMessage.error_description)" -Color "Red" } } Log-Message "Device addition to groups completed." -Color "Darkcyan" } |