From 00b1e23342e93ecd3a2a64e9944150e5ca51fab9 Mon Sep 17 00:00:00 2001 From: Mark Hensler Date: Wed, 26 Feb 2025 19:43:36 -0800 Subject: [PATCH 1/3] fix typo --- .../Set/Set-NinjaOneOrganisationPolicies.ps1 | 166 +++++++++--------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 index 1bbfa54..5400289 100644 --- a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 +++ b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 @@ -1,84 +1,84 @@ -function Set-NinjaOneOrganisationPolicies { - <# - .SYNOPSIS - Sets policy assignment for node role(s) for an organisation. - .DESCRIPTION - Sets policy assignment for node role(s) for an organisation using the NinjaOne v2 API. - .FUNCTIONALITY - Organisation Policies - .OUTPUTS - A powershell object containing the response. - .LINK - https://docs.homotechsual.dev/modules/ninjaone/commandlets/set/organisationpolicies - #> - [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] - [OutputType([Array])] - [Alias('snoop', 'Set-NinjaOneOrganizationPolicies', 'snorpa', 'Set-NinjaOneNodeRolePolicyAssignment', 'unorpa', 'Update-NinjaOneNodeRolePolicyAssignment', 'snopm', 'Set-NinjaOneOrganisationPolicyMappings', 'Set-NinjaOneOrganizationPolicyMappings', 'Set-NinjaOneOrganisationPolicyMapping', 'Set-NinjaOneOrganizationPolicyMapping', 'unopm', 'Update-NinjaOneOrganisationPolicyMappings', 'Update-NinjaOneOrganisationPolicyMapping', 'Update-NinjaOneOrganizationPolicyMappings', 'Update-NinjaOneOrganizationPolicyMapping')] - [MetadataAttribute( - '/v2/organization/{id}/policies', - 'put' - )] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'Uses dynamic parameter parsing.')] - Param( - # The organisation to update the policy assignment for. - [Parameter(Mandatory, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName)] - [Parameter(Mandatory, ParameterSetName = 'Multiple', Position = 0, ValueFromPipelineByPropertyName)] - [Alias('id', 'organizationId')] - [Int]$organisationId, - # The node role id to update the policy assignment for. - [Parameter(Mandatory, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName)] - [Int]$nodeRoleId, - # The policy id to assign to the node role. - [Parameter(Mandatory, ParameterSetName = 'Single', Position = 2, ValueFromPipelineByPropertyName)] - [Int]$policyId, - # The node role policy assignments to update. Should be an array of objects with the following properties: nodeRoleId, policyId. - [Parameter(Mandatory, ParameterSetName = 'Multiple', Position = 1, ValueFromPipelineByPropertyName)] - [Object[]]$policyAssignments - ) - process { - try { - if ($PSCmdlet.ParameterSetName -eq 'Single') { - try { - $Body = @{ - 'nodeRoleId' = $nodeRoleId - 'policyId' = $policyId - } - } catch { - New-NinjaOneError -ErrorRecord $_ - } - } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { - $Body = [System.Collections.Generic.List[Object]]::new() - $policyAssignments | ForEach-Object { - try { - $Body.Add( - @{ - 'nodeRoleId' = $_.nodeRoleId - 'policyId' = $_.policyId - } - ) | Out-Null - } catch { - New-NinjaOneError -ErrorRecord $_ - } - } - } - $Resource = ('v2/organization/{0}/policies' -f $organisation.id) - $RequestParams = @{ - Resource = $Resource - Body = $Body - } - if ($PSCmdlet.ParameterSetName -eq 'Single') { - $RequestParams.AsArray = $true - } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { - $RequestParams.AsArray = $false - } - if ($PSCmdlet.ShouldProcess(('Assign policy {0} to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId), 'Update')) { - $NodeRolePolicyAssignment = New-NinjaOnePUTRequest @RequestParams - if ($NodeRolePolicyAssignment -eq 204) { - Write-Information ('Policy {0} assigned to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId) - } - } - } catch { - New-NinjaOneError -ErrorRecord $_ - } - } +function Set-NinjaOneOrganisationPolicies { + <# + .SYNOPSIS + Sets policy assignment for node role(s) for an organisation. + .DESCRIPTION + Sets policy assignment for node role(s) for an organisation using the NinjaOne v2 API. + .FUNCTIONALITY + Organisation Policies + .OUTPUTS + A powershell object containing the response. + .LINK + https://docs.homotechsual.dev/modules/ninjaone/commandlets/set/organisationpolicies + #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] + [OutputType([Array])] + [Alias('snoop', 'Set-NinjaOneOrganizationPolicies', 'snorpa', 'Set-NinjaOneNodeRolePolicyAssignment', 'unorpa', 'Update-NinjaOneNodeRolePolicyAssignment', 'snopm', 'Set-NinjaOneOrganisationPolicyMappings', 'Set-NinjaOneOrganizationPolicyMappings', 'Set-NinjaOneOrganisationPolicyMapping', 'Set-NinjaOneOrganizationPolicyMapping', 'unopm', 'Update-NinjaOneOrganisationPolicyMappings', 'Update-NinjaOneOrganisationPolicyMapping', 'Update-NinjaOneOrganizationPolicyMappings', 'Update-NinjaOneOrganizationPolicyMapping')] + [MetadataAttribute( + '/v2/organization/{id}/policies', + 'put' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'Uses dynamic parameter parsing.')] + Param( + # The organisation to update the policy assignment for. + [Parameter(Mandatory, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName)] + [Parameter(Mandatory, ParameterSetName = 'Multiple', Position = 0, ValueFromPipelineByPropertyName)] + [Alias('id', 'organizationId')] + [Int]$organisationId, + # The node role id to update the policy assignment for. + [Parameter(Mandatory, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName)] + [Int]$nodeRoleId, + # The policy id to assign to the node role. + [Parameter(Mandatory, ParameterSetName = 'Single', Position = 2, ValueFromPipelineByPropertyName)] + [Int]$policyId, + # The node role policy assignments to update. Should be an array of objects with the following properties: nodeRoleId, policyId. + [Parameter(Mandatory, ParameterSetName = 'Multiple', Position = 1, ValueFromPipelineByPropertyName)] + [Object[]]$policyAssignments + ) + process { + try { + if ($PSCmdlet.ParameterSetName -eq 'Single') { + try { + $Body = @{ + 'nodeRoleId' = $nodeRoleId + 'policyId' = $policyId + } + } catch { + New-NinjaOneError -ErrorRecord $_ + } + } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { + $Body = [System.Collections.Generic.List[Object]]::new() + $policyAssignments | ForEach-Object { + try { + $Body.Add( + @{ + 'nodeRoleId' = $_.nodeRoleId + 'policyId' = $_.policyId + } + ) | Out-Null + } catch { + New-NinjaOneError -ErrorRecord $_ + } + } + } + $Resource = ('v2/organization/{0}/policies' -f $organisationId) + $RequestParams = @{ + Resource = $Resource + Body = $Body + } + if ($PSCmdlet.ParameterSetName -eq 'Single') { + $RequestParams.AsArray = $true + } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { + $RequestParams.AsArray = $false + } + if ($PSCmdlet.ShouldProcess(('Assign policy {0} to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId), 'Update')) { + $NodeRolePolicyAssignment = New-NinjaOnePUTRequest @RequestParams + if ($NodeRolePolicyAssignment -eq 204) { + Write-Information ('Policy {0} assigned to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId) + } + } + } catch { + New-NinjaOneError -ErrorRecord $_ + } + } } \ No newline at end of file From 5d56be328abc3637edfd5b3fb2818ba28e7b6b1b Mon Sep 17 00:00:00 2001 From: Mark Hensler Date: Wed, 26 Feb 2025 20:08:58 -0800 Subject: [PATCH 2/3] code optimization --- .../Set/Set-NinjaOneOrganisationPolicies.ps1 | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 index 5400289..7ea188c 100644 --- a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 +++ b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 @@ -37,16 +37,28 @@ function Set-NinjaOneOrganisationPolicies { ) process { try { + $RequestParams = @{ + Resource = ('v2/organization/{0}/policies' -f $organisationId) + Body = $Body + } if ($PSCmdlet.ParameterSetName -eq 'Single') { try { + $RequestParams.AsArray = $true $Body = @{ 'nodeRoleId' = $nodeRoleId 'policyId' = $policyId } + if ($PSCmdlet.ShouldProcess(('Assign policy {0} to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId), 'Update')) { + $NodeRolePolicyAssignment = New-NinjaOnePUTRequest @RequestParams + if ($NodeRolePolicyAssignment -eq 204) { + Write-Information ('Policy {0} assigned to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId) + } + } } catch { New-NinjaOneError -ErrorRecord $_ } } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { + $RequestParams.AsArray = $false $Body = [System.Collections.Generic.List[Object]]::new() $policyAssignments | ForEach-Object { try { @@ -60,21 +72,11 @@ function Set-NinjaOneOrganisationPolicies { New-NinjaOneError -ErrorRecord $_ } } - } - $Resource = ('v2/organization/{0}/policies' -f $organisationId) - $RequestParams = @{ - Resource = $Resource - Body = $Body - } - if ($PSCmdlet.ParameterSetName -eq 'Single') { - $RequestParams.AsArray = $true - } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { - $RequestParams.AsArray = $false - } - if ($PSCmdlet.ShouldProcess(('Assign policy {0} to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId), 'Update')) { - $NodeRolePolicyAssignment = New-NinjaOnePUTRequest @RequestParams - if ($NodeRolePolicyAssignment -eq 204) { - Write-Information ('Policy {0} assigned to role {1} for {2}.' -f $policyId, $nodeRoleId, $organisationId) + if ($PSCmdlet.ShouldProcess(('Update policies for {0}.' -f $organisationId), 'Update')) { + $NodeRolePolicyAssignment = New-NinjaOnePUTRequest @RequestParams + if ($NodeRolePolicyAssignment -eq 204) { + Write-Information ('Policies updated for {0}.' -f $organisationId) + } } } } catch { From 92efecdb69dc54238138879773c148530ae55eae Mon Sep 17 00:00:00 2001 From: Mark Hensler Date: Wed, 26 Feb 2025 21:19:28 -0800 Subject: [PATCH 3/3] fix $RequestParams.Body --- .../Management/Set/Set-NinjaOneOrganisationPolicies.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 index 7ea188c..4636d01 100644 --- a/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 +++ b/Source/Public/Management/Set/Set-NinjaOneOrganisationPolicies.ps1 @@ -39,12 +39,12 @@ function Set-NinjaOneOrganisationPolicies { try { $RequestParams = @{ Resource = ('v2/organization/{0}/policies' -f $organisationId) - Body = $Body + Body = $null } if ($PSCmdlet.ParameterSetName -eq 'Single') { try { $RequestParams.AsArray = $true - $Body = @{ + $RequestParams.Body = @{ 'nodeRoleId' = $nodeRoleId 'policyId' = $policyId } @@ -59,10 +59,10 @@ function Set-NinjaOneOrganisationPolicies { } } elseif ($PSCmdlet.ParameterSetName -eq 'Multiple') { $RequestParams.AsArray = $false - $Body = [System.Collections.Generic.List[Object]]::new() + $RequestParams.Body = [System.Collections.Generic.List[Object]]::new() $policyAssignments | ForEach-Object { try { - $Body.Add( + $RequestParams.Body.Add( @{ 'nodeRoleId' = $_.nodeRoleId 'policyId' = $_.policyId