From c64c28e8211640bd7fce10a5d7cb18b17ed32c96 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 ++++++++++++++----- .../Set-TeamViewerManagedDevice.Tests.ps1 | 39 +++++++++++++++ 2 files changed, 75 insertions(+), 13 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/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 `