Skip to content

Commit

Permalink
Merge pull request #51 from AlexandrosAlexiou/Enrich-Managed-Groups-P…
Browse files Browse the repository at this point in the history
…olicy-Management

Add managed group set/remove policy functionality
  • Loading branch information
karthickgandhiTV authored Oct 26, 2023
2 parents 9deab1e + f75da9f commit 991d54a
Show file tree
Hide file tree
Showing 8 changed files with 6,369 additions and 35 deletions.
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
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

0 comments on commit 991d54a

Please sign in to comment.