From 83a90c90b90c130b061010d8740b42a8bd7c3567 Mon Sep 17 00:00:00 2001 From: Radu Cristea Date: Fri, 21 Nov 2025 14:32:30 +0100 Subject: [PATCH] Updated the set-teamviewermanageddevice function with additional description endpoint --- .../Public/Set-TeamViewerManagedDevice.ps1 | 49 ++++++++++++++----- Docs/Help/Set-TeamViewerManagedDevice.md | 29 ++++++++++- .../Set-TeamViewerManagedDevice.Tests.ps1 | 39 +++++++++++++++ 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 b/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 index d047331..477209c 100644 --- a/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 +++ b/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 @@ -1,29 +1,46 @@ function Set-TeamViewerManagedDevice { [CmdletBinding(SupportsShouldProcess = $true)] param( - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'Default')] + [Parameter(Mandatory = $true, ParameterSetName = 'ByPolicyId')] + [Parameter(Mandatory = $true, ParameterSetName = 'ByManagedGroupId')] + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateDescription')] [securestring] $ApiToken, - [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'Default', ValueFromPipeline = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'ByPolicyId', ValueFromPipeline = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'ByManagedGroupId', ValueFromPipeline = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateDescription', ValueFromPipeline = $true)] [ValidateScript( { $_ | Resolve-TeamViewerManagedDeviceId } )] [Alias('DeviceId')] [object] $Device, + [Parameter(Mandatory = $false, ParameterSetName = 'Default')] + [Parameter(Mandatory = $false, ParameterSetName = 'ByPolicyId')] + [Parameter(Mandatory = $false, ParameterSetName = 'ByManagedGroupId')] + [Parameter(Mandatory = $false, ParameterSetName = 'UpdateDescription')] [Alias('Alias')] [string] $Name, + [Parameter(Mandatory = $true, ParameterSetName = 'ByPolicyId')] [ValidateScript( { $_ | Resolve-TeamViewerPolicyId } )] [Alias('PolicyId')] [object] $Policy, + [Parameter(Mandatory = $true, ParameterSetName = 'ByManagedGroupId')] [ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId } )] [Alias('ManagedGroupId')] [object] - $ManagedGroup + $ManagedGroup, + + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateDescription')] + [Alias('DeviceDescription')] + [string] + $Description ) Begin { $body = @{} @@ -31,17 +48,17 @@ function Set-TeamViewerManagedDevice { if ($Name) { $body['name'] = $Name } - if ($Policy) { - $body['teamviewerPolicyId'] = $Policy | Resolve-TeamViewerPolicyId - } - elseif ($ManagedGroup) { - $body['managedGroupId'] = $ManagedGroup | Resolve-TeamViewerManagedGroupId - } - if ($Policy -And $ManagedGroup) { - $PSCmdlet.ThrowTerminatingError( - ('The combination of parameters -Policy and -ManagedGroup is not allowed.' | ` - ConvertTo-ErrorRecord -ErrorCategory InvalidArgument)) + switch ($PsCmdlet.ParameterSetName) { + 'ByPolicyId' { + $body['teamviewerPolicyId'] = $Policy | Resolve-TeamViewerPolicyId + } + 'ByManagedGroupId' { + $body['managedGroupId'] = $ManagedGroup | Resolve-TeamViewerManagedGroupId + } + 'UpdateDescription' { + $body['deviceDescription'] = $Description + } } if ($body.Count -eq 0) { @@ -54,6 +71,12 @@ function Set-TeamViewerManagedDevice { $deviceId = $Device | Resolve-TeamViewerManagedDeviceId $resourceUri = "$(Get-TeamViewerApiUri)/managed/devices/$deviceId" + switch ($PsCmdlet.ParameterSetName) { + 'UpdateDescription' { + $resourceUri += '/description' + } + } + if ($PSCmdlet.ShouldProcess($Device.ToString(), 'Change managed device entry')) { Invoke-TeamViewerRestMethod ` -ApiToken $ApiToken ` diff --git a/Docs/Help/Set-TeamViewerManagedDevice.md b/Docs/Help/Set-TeamViewerManagedDevice.md index 3ee7414..2039ce4 100644 --- a/Docs/Help/Set-TeamViewerManagedDevice.md +++ b/Docs/Help/Set-TeamViewerManagedDevice.md @@ -15,13 +15,14 @@ Change properties of a TeamViewer managed device. ```powershell Set-TeamViewerManagedDevice [-ApiToken] [-Device] [[-Name] ] - [[-Policy] ] [[-ManagedGroup] ] [-WhatIf] [-Confirm] [] + [[-Policy] ] [[-ManagedGroup] ] [[-Description] ] [-WhatIf] [-Confirm] [] ``` ## DESCRIPTION Changes properties of a managed device. For example, the name of the managed -device or the policy can be changed. +device or the policy or the description can be changed. +You cannot combine any of those three parameters together. For changing the device name, the current account needs `DeviceAdministration` manager permissions on the device. @@ -55,6 +56,14 @@ PS /> Set-TeamViewerManagedDevice -Device '33a2e2e1-27ef-43e2-a175-f97ee0344033' Inherit the TeamViewer policy from a managed group to the device (the device has to be part of the managed group specified). +### Example 4 + +```powershell +PS /> Set-TeamViewerManagedDevice -Device '33a2e2e1-27ef-43e2-a175-f97ee0344033' -Description 'Test description' +``` + +Changes the description of the device. + ## PARAMETERS ### -ApiToken @@ -159,6 +168,22 @@ Accept pipeline input: True (ByValue) Accept wildcard characters: False ``` +### -Description + +New description for the managed device. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DeviceDescription + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -WhatIf Shows what would happen if the cmdlet runs. diff --git a/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 b/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 index 3e8fd17..8e659a9 100644 --- a/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 +++ b/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 @@ -45,6 +45,45 @@ Describe 'Set-TeamViewerManagedDevice' { $body.managedGroupId | Should -Be 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' } + It 'Should update the managed device alias and the managed device policy to the managed group' { + Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -Name 'Foo Bar' -ManagedGroup 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' + $mockArgs.Body | Should -Not -BeNullOrEmpty + $body = [System.Text.Encoding]::UTF8.GetString($mockArgs.Body) | ConvertFrom-Json + $body.name | Should -Be 'Foo Bar' + $body.managedGroupId | Should -Be 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' + } + + It 'Should update the managed device description' { + Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -Description 'Test description' + $mockArgs.Body | Should -Not -BeNullOrEmpty + + $body = [System.Text.Encoding]::UTF8.GetString($mockArgs.Body) | ConvertFrom-Json + $body.deviceDescription | Should -Be 'Test description' + + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { + $ApiToken -eq $testApiToken -And ` + $Uri -eq "//unit.test/managed/devices/$testDeviceId/description" -And ` + $Method -eq 'Put' + } + } + + It 'Should not allow description together with policy' { + { Set-TeamViewerManagedDevice ` + -ApiToken $testApiToken ` + -Device $testDeviceId ` + -Description 'Test description' ` + -Policy '2871c013-3040-4969-9ba4-ce970f4375e8' } | Should -Throw + } + + It 'Should not allow description together with managed group' { + { Set-TeamViewerManagedDevice ` + -ApiToken $testApiToken ` + -Device $testDeviceId ` + -Description 'Test description' ` + -ManagedGroup 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' } | Should -Throw + } + + It 'Should not be possible to inherit and set a policy at the same time' { { Set-TeamViewerManagedDevice ` -ApiToken $testApiToken `