Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add managed group set/remove policy functionality #51

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
39 changes: 39 additions & 0 deletions Cmdlets/Public/Remove-TeamViewerPolicyFromManagedGroup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Remove-TeamviewerPolicyFromManagedGroup {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[securestring]
$ApiToken,

[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId } )]
[Alias('GroupId')]
[object]
$Group,

[Parameter(Mandatory = $true)]
[PolicyType]
$PolicyType
)
Begin {
$body = @{
'policy_type' = [int]$PolicyType
}
}
Process {
$groupId = $Group | Resolve-TeamViewerManagedGroupId
$resourceUri = "$(Get-TeamViewerApiUri)/managed/groups/$groupId/policy/remove"

if ($PSCmdlet.ShouldProcess($Group.ToString(), 'Change managed group entry')) {
Invoke-TeamViewerRestMethod `
-ApiToken $ApiToken `
-Uri $resourceUri `
-Method Put `
-ContentType 'application/json; charset=utf-8' `
-Body ([System.Text.Encoding]::UTF8.GetBytes(($body | ConvertTo-Json))) `
-WriteErrorTo $PSCmdlet `
-ErrorAction Stop | `
Out-Null
}
}
}
22 changes: 11 additions & 11 deletions Cmdlets/Public/Set-TeamViewerManagedDevice.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ function Set-TeamViewerManagedDevice {

[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript( { $_ | Resolve-TeamViewerManagedDeviceId } )]
[Alias("DeviceId")]
[Alias('DeviceId')]
[object]
$Device,

[Alias("Alias")]
[Alias('Alias')]
[string]
$Name,

[ValidateScript( { $_ | Resolve-TeamViewerPolicyId } )]
[Alias("PolicyId")]
[Alias('PolicyId')]
[object]
$Policy,

[ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId } )]
[Alias("ManagedGroupId")]
[Alias('ManagedGroupId')]
[object]
$ManagedGroup
)
Expand All @@ -38,28 +38,28 @@ 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))
('The combination of parameters -Policy and -ManagedGroup is not allowed.' | `
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}

if ($body.Count -eq 0) {
$PSCmdlet.ThrowTerminatingError(
("The given input does not change the managed device." | `
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
('The given input does not change the managed device.' | `
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}
}
Process {
$deviceId = $Device | Resolve-TeamViewerManagedDeviceId
$resourceUri = "$(Get-TeamViewerApiUri)/managed/devices/$deviceId"

if ($PSCmdlet.ShouldProcess($Device.ToString(), "Change managed device entry")) {
if ($PSCmdlet.ShouldProcess($Device.ToString(), 'Change managed device entry')) {
Invoke-TeamViewerRestMethod `
-ApiToken $ApiToken `
-Uri $resourceUri `
-Method Put `
-ContentType "application/json; charset=utf-8" `
-ContentType 'application/json; charset=utf-8' `
-Body ([System.Text.Encoding]::UTF8.GetBytes(($body | ConvertTo-Json))) `
-WriteErrorTo $PSCmdlet `
-ErrorAction Stop | `
Expand Down
60 changes: 46 additions & 14 deletions Cmdlets/Public/Set-TeamViewerManagedGroup.ps1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Confirm-PolicyParametersProvided required. Isn't it possible to combine them with mandatory and parametersetnames? Please investigate

Copy link
Contributor Author

@AlexandrosAlexiou AlexandrosAlexiou Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed Confirm-PolicyParametersProvided and simplified the whole logic.

Mandatory parameters do not do the trick here since we do not want to force parameters to be provided for the script to work. eg. In case the user wants to change the group name and does not wish to modify the group policy.

If I am missing something please let me know.

Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,87 @@ function Set-TeamViewerManagedGroup {

[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript( { $_ | Resolve-TeamViewerManagedGroupId })]
[Alias("GroupId")]
[Alias("Id")]
[Alias('GroupId')]
[Alias('Id')]
[object]
$Group,

[Parameter(Mandatory = $true, ParameterSetName = 'ByParameters')]
[Parameter(ParameterSetName = 'ByParameters')]
[string]
$Name,

[Parameter(ParameterSetName = 'ByParameters')]
[ValidateScript( { $_ | Resolve-TeamViewerPolicyId } )]
[Alias('Policy')]
[object]
$PolicyId,

[Parameter(ParameterSetName = 'ByParameters')]
[PolicyType]
$PolicyType,

[Parameter(Mandatory = $true, ParameterSetName = 'ByProperties')]
[hashtable]
$Property
)

Begin {
# Warning suppresion doesn't seem to work.
# Warning suppression doesn't seem to work.
# See https://github.com/PowerShell/PSScriptAnalyzer/issues/1472
$null = $Property

$body = @{}
switch ($PSCmdlet.ParameterSetName) {
'ByParameters' {
$body['name'] = $Name
if ($Name) {
$body['name'] = $Name
}

if ($PolicyId -or $PolicyType) {
if (-not ($PolicyId -and $PolicyType)) {
$PSCmdlet.ThrowTerminatingError(
('PolicyId and PolicyType must be specified together' | ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}
$body['policy'] = @{
'policy_id' = $PolicyId
'policy_type' = $PolicyType
}
}
}
'ByProperties' {
@('name') | `
Where-Object { $Property[$_] } | `
ForEach-Object { $body[$_] = $Property[$_] }
@('name') | Where-Object { $Property[$_] } | ForEach-Object { $body[$_] = $Property[$_] }

if ($Property.ContainsKey('policy_id') -or $Property.ContainsKey('policy_type')) {
if (-not ($Property.ContainsKey('policy_id') -and $Property.ContainsKey('policy_type'))) {
$PSCmdlet.ThrowTerminatingError(
('PolicyId and PolicyType must be specified together' | ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}
$body['policy'] = @{
'policy_id' = $Property['policy_id']
'policy_type' = [PolicyType]$Property['policy_type']
}
}
}
}

if ($body.Count -eq 0) {
$PSCmdlet.ThrowTerminatingError(
("The given input does not change the managed group." | `
ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
('The given input does not change the managed group.' | ConvertTo-ErrorRecord -ErrorCategory InvalidArgument))
}
}

Process {
$groupId = $Group | Resolve-TeamViewerManagedGroupId
$resourceUri = "$(Get-TeamViewerApiUri)/managed/groups/$groupId"

if ($PSCmdlet.ShouldProcess($groupId, "Update managed group")) {
if ($PSCmdlet.ShouldProcess($groupId, 'Update managed group')) {
Invoke-TeamViewerRestMethod `
-ApiToken $ApiToken `
-Uri $resourceUri `
-Method Put `
-ContentType "application/json; charset=utf-8" `
-ContentType 'application/json; charset=utf-8' `
-Body ([System.Text.Encoding]::UTF8.GetBytes(($body | ConvertTo-Json))) `
-WriteErrorTo $PSCmdlet | `
Out-Null
-WriteErrorTo $PSCmdlet | Out-Null
}
}
}
Loading