Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions Cmdlets/Public/Set-TeamViewerManagedDevice.ps1
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
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 = @{}

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) {
Expand All @@ -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 `
Expand Down
29 changes: 27 additions & 2 deletions Docs/Help/Set-TeamViewerManagedDevice.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ Change properties of a TeamViewer managed device.

```powershell
Set-TeamViewerManagedDevice [-ApiToken] <SecureString> [-Device] <Object> [[-Name] <String>]
[[-Policy] <Object>] [[-ManagedGroup] <Object>] [-WhatIf] [-Confirm] [<CommonParameters>]
[[-Policy] <Object>] [[-ManagedGroup] <Object>] [[-Description] <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions Tests/Public/Set-TeamViewerManagedDevice.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand Down