diff --git a/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 b/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 index d047331..2c5a569 100644 --- a/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 +++ b/Cmdlets/Public/Set-TeamViewerManagedDevice.ps1 @@ -23,9 +23,14 @@ function Set-TeamViewerManagedDevice { [ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId } )] [Alias('ManagedGroupId')] [object] - $ManagedGroup + $ManagedGroup, + + [Parameter(ParameterSetName = 'UpdateDescription')] + [Alias('Description')] + [string] + $deviceDescription ) - Begin { + begin { $body = @{} if ($Name) { @@ -38,10 +43,20 @@ function Set-TeamViewerManagedDevice { $body['managedGroupId'] = $ManagedGroup | Resolve-TeamViewerManagedGroupId } - if ($Policy -And $ManagedGroup) { + if ($Policy -and $ManagedGroup) { $PSCmdlet.ThrowTerminatingError( ('The combination of parameters -Policy and -ManagedGroup is not allowed.' | ` - ConvertTo-ErrorRecord -ErrorCategory InvalidArgument)) + ConvertToErrorRecord -ErrorCategory InvalidArgument)) + } + + if ($deviceDescription -and ($Policy -or $ManagedGroup)) { + $PSCmdlet.ThrowTerminatingError( + ('The parameter -deviceDescription cannot be combined with -Policy or -ManagedGroup.' | + ConvertTo-ErrorRecord -ErrorCategory InvalidArgument)) + } + + if ($deviceDescription) { + $body['deviceDescription'] = $deviceDescription } if ($body.Count -eq 0) { @@ -50,10 +65,16 @@ function Set-TeamViewerManagedDevice { ConvertTo-ErrorRecord -ErrorCategory InvalidArgument)) } } - Process { + process { $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..76636dd 100644 --- a/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 +++ b/Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1 @@ -19,8 +19,8 @@ Describe 'Set-TeamViewerManagedDevice' { It 'Should call the correct API endpoint to update a managed device' { Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -Name 'Foo Bar' Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { - $ApiToken -eq $testApiToken -And ` - $Uri -eq "//unit.test/managed/devices/$testDeviceId" -And ` + $ApiToken -eq $testApiToken -and ` + $Uri -eq "//unit.test/managed/devices/$testDeviceId" -and ` $Method -eq 'Put' } } @@ -45,6 +45,37 @@ Describe 'Set-TeamViewerManagedDevice' { $body.managedGroupId | Should -Be 'e579cfeb-0b29-4d91-9e81-2d9507f53ff8' } + It 'Should update the managed device description' { + Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDeviceId -deviceDescription '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 ` + -deviceDescription '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 ` + -deviceDescription '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 ` @@ -68,8 +99,8 @@ Describe 'Set-TeamViewerManagedDevice' { $testDevice = @{ id = $testDeviceId; name = 'test device' } | ConvertTo-TeamViewerManagedDevice Set-TeamViewerManagedDevice -ApiToken $testApiToken -Device $testDevice -Name 'Foo Bar' Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { - $ApiToken -eq $testApiToken -And ` - $Uri -eq "//unit.test/managed/devices/$testDeviceId" -And ` + $ApiToken -eq $testApiToken -and ` + $Uri -eq "//unit.test/managed/devices/$testDeviceId" -and ` $Method -eq 'Put' } }