Skip to content

Commit

Permalink
Merge pull request #491 from TheJumpCloud/SA-3318_RemoveJCPolicy-2
Browse files Browse the repository at this point in the history
SA-3318: Remove-JCPolicy Redo
  • Loading branch information
gweinjc authored May 15, 2023
2 parents dbe73bc + f0aae8d commit c2a729e
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ parameters:
PublishToPSGallery:
description: "When `true` and when run against Master branch, this workflow will publish the latest code to PSGallery"
type: boolean
default: false
default: true
ManualModuleVersion:
description: "When `true` the pipeline will use the Module Version specified in JumpCloud Module JumpCloud.psd1 file"
type: boolean
Expand Down
5 changes: 4 additions & 1 deletion PowerShell/JumpCloud Module/Docs/JumpCloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module Name: JumpCloud
Module Guid: 31c023d1-a901-48c4-90a3-082f91b31646
Download Help Link: https://github.com/TheJumpCloud/support/wiki
Help Version: 2.4.0
Help Version: 2.5.0
Locale: en-US
---

Expand Down Expand Up @@ -157,6 +157,9 @@ Removes a JumpCloud Command Result
### [Remove-JCCommandTarget](Remove-JCCommandTarget.md)
Removes the association between a JumpCloud system or a JumpCloud system group from a JumpCloud command

### [Remove-JCPolicy](Remove-JCPolicy.md)
Removes a JumpCloud Policy

### [Remove-JCRadiusReplyAttribute](Remove-JCRadiusReplyAttribute.md)
Removes Radius reply attributes from a JumpCloud user group.

Expand Down
105 changes: 105 additions & 0 deletions PowerShell/JumpCloud Module/Docs/Remove-JCPolicy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
external help file: JumpCloud-help.xml
Module Name: JumpCloud
online version: https://github.com/TheJumpCloud/support/wiki/
schema: 2.0.0
---

# Remove-JCPolicy

## SYNOPSIS
Removes a JumpCloud Policy

## SYNTAX

### ByID
```
Remove-JCPolicy [-PolicyID] <String> [-force] [<CommonParameters>]
```

### Name
```
Remove-JCPolicy [-Name <String>] [-force] [<CommonParameters>]
```

## DESCRIPTION
The Remove-JCPolicy function will remove a JumpCloud Policy from the JumpCloud organization.

## EXAMPLES

### Example 1
```powershell
PS C:\> Remove-JCPolicy -Name "Allow The Use of Biometrics"
```

This will remove the JumpCloud Policy with the name Allow The Use of Biometrics

### Example 2
```powershell
PS C:\> Remove-JCPolicy -PolicyID "645bea14b069dd0001bbe232"
```

This will remove the JumpCloud Policy by its corresponding ID

## PARAMETERS

### -force
A SwitchParameter which suppresses the warning message when removing a JumpCloud Policy.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Name
The Name of the JumpCloud policy you wish to remove.
```yaml
Type: System.String
Parameter Sets: Name
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -PolicyID
The PolicyID of the JumpCloud policy you wish to remove.
```yaml
Type: System.String
Parameter Sets: ByID
Aliases: _id, id

Required: True
Position: 0
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
### System.String
## OUTPUTS
### System.Object
## NOTES
## RELATED LINKS
## RELATED LINKS
2 changes: 1 addition & 1 deletion PowerShell/JumpCloud Module/JumpCloud.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>JumpCloud</id>
<version>2.4.0</version>
<version>2.5.0.8557-202305112148</version>
<description>PowerShell functions to manage a JumpCloud Directory-as-a-Service</description>
<authors>JumpCloud Solutions Architect Team</authors>
<owners>JumpCloud</owners>
Expand Down
9 changes: 5 additions & 4 deletions PowerShell/JumpCloud Module/JumpCloud.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: JumpCloud Solutions Architect Team
#
# Generated on: 4/27/2023
# Generated on: 5/11/2023
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'JumpCloud.psm1'

# Version number of this module.
ModuleVersion = '2.4.0'
ModuleVersion = '2.5.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -87,8 +87,9 @@ FunctionsToExport = 'Add-JCAssociation', 'Add-JCCommandTarget',
'New-JCImportTemplate', 'New-JCPolicy', 'New-JCRadiusServer',
'New-JCSystemGroup', 'New-JCUser', 'New-JCUserGroup',
'Remove-JCAssociation', 'Remove-JCCommand', 'Remove-JCCommandResult',
'Remove-JCCommandTarget', 'Remove-JCRadiusReplyAttribute',
'Remove-JCRadiusServer', 'Remove-JCSystem', 'Remove-JCSystemGroup',
'Remove-JCCommandTarget', 'Remove-JCPolicy',
'Remove-JCRadiusReplyAttribute', 'Remove-JCRadiusServer',
'Remove-JCSystem', 'Remove-JCSystemGroup',
'Remove-JCSystemGroupMember', 'Remove-JCSystemUser', 'Remove-JCUser',
'Remove-JCUserGroup', 'Remove-JCUserGroupMember',
'Send-JCPasswordReset', 'Set-JCCommand', 'Set-JCOrganization',
Expand Down
104 changes: 104 additions & 0 deletions PowerShell/JumpCloud Module/Public/Policies/Remove-JCPolicy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Function Remove-JCPolicy () {
param
(
[Parameter(Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName = 'ByID',
Position = 0,
HelpMessage = 'The PolicyID of the JumpCloud policy you wish to remove.')]
[Alias('_id', 'id')]
[String]$PolicyID,
[Parameter(
ValueFromPipelineByPropertyName,
ParameterSetName = 'Name',
HelpMessage = 'The Name of the JumpCloud policy you wish to remove.')]
[String]$Name,
[Parameter(HelpMessage = 'A SwitchParameter which suppresses the warning message when removing a JumpCloud Policy.')]
[Switch]
$force
)
begin {
Write-Debug 'Verifying JCAPI Key'
if ($JCAPIKEY.length -ne 40) {
Connect-JCOnline
}

if ($PsCmdlet.ParameterSetName -eq "Name") {
$policyHash = Get-JCPolicy | Select-Object Id, Name
}

$deletedArray = @()
}
process {
switch ($PsCmdlet.ParameterSetName) {
ByID {
if (!$force) {
Write-Warning "Are you sure you wish to delete policy: $PolicyID ?" -WarningAction Inquire

try {
$removePolicy = Remove-JcSdkPolicy -Id $PolicyID -ErrorAction Stop
$Status = 'Deleted'
} catch {
$Status = $_.ErrorDetails
}

$FormattedResults = [PSCustomObject]@{
'Policy' = $PolicyID
'Results' = $Status
}
} else {
try {
$removePolicy = Remove-JcSdkPolicy -Id $PolicyID -ErrorAction Stop
$Status = 'Deleted'
} catch {
$Status = $_.ErrorDetails
}

$FormattedResults = [PSCustomObject]@{
'Policy' = $PolicyID
'Results' = $Status
}
}
$deletedArray += $FormattedResults
}
Name {
# Check if Policy exists with given name
if ($policyHash.Name -ccontains ($Name)) {
$Policy = $policyHash | Where-Object Name -CEQ $Name
} else {
throw "Policy does not exist. Run 'Get-JCPolicy | Select-Object Name' to see a list of all your JumpCloud policies"
}

if (!$force) {

Write-Warning "Are you sure you wish to delete policy: $Name ?" -WarningAction Inquire
try {
$removePolicy = Remove-JcSdkPolicy -Id $Policy.id -ErrorAction Stop
$Status = 'Deleted'
} catch {
$Status = $_.ErrorDetails
}
$FormattedResults = [PSCustomObject]@{
'Policy' = $Policy.Name
'Results' = $Status
}
} else {
try {
$removePolicy = Remove-JcSdkPolicy -Id $Policy.id -ErrorAction Stop
$Status = 'Deleted'
} catch {
$Status = $_.ErrorDetails
}
$FormattedResults = [PSCustomObject]@{
'Policy' = $Policy.Name
'Results' = $Status
}
}
$deletedArray += $FormattedResults
}
}
}
end {
return $deletedArray
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Describe -Tag:('JCPolicy') 'Remove-JCPolicy 1.10' {
BeforeAll { Connect-JCOnline -JumpCloudApiKey:($PesterParams_ApiKey) -force | Out-Null }
It 'Remove Policy by PolicyID' {
# Create test policy for removal
$policyTemplates = Get-JcSdkPolicyTemplate
$policyTemplate = $policyTemplates | Where-Object { $_.name -eq "rename_local_administrator_account_windows" }
$templateId = $policyTemplate.id
$stringPolicy = New-JCPolicy -Name "Pester - Remove Policy Test" -templateID $templateId -ADMINISTRATORSTRING "Test String"

$DeletedPolicy = Remove-JCPolicy -Id $stringPolicy.Id -Force
$DeletedPolicy.results | Should -Be 'Deleted'
}
It 'Remove Policy by Name' {
# Create test policy for removal
$policyTemplates = Get-JcSdkPolicyTemplate
$policyTemplate = $policyTemplates | Where-Object { $_.name -eq "rename_local_administrator_account_windows" }
$templateId = $policyTemplate.id
$stringPolicy = New-JCPolicy -Name "Pester - Remove Policy Test" -templateID $templateId -ADMINISTRATORSTRING "Test String"

$DeletedPolicy = Remove-JCPolicy -Name $stringPolicy.Name -Force
$DeletedPolicy.results | Should -Be 'Deleted'
}
It "Remove Policy by Non-existant Name" {
$DeletedPolicy = { Remove-JCPolicy -Name "Pester - Fake Policy Test" -Force -ErrorAction Stop } | Should -Throw
}
}
Loading

0 comments on commit c2a729e

Please sign in to comment.