-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from TheJumpCloud/SA-3318_RemoveJCPolicy-2
SA-3318: Remove-JCPolicy Redo
- Loading branch information
Showing
9 changed files
with
412 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
PowerShell/JumpCloud Module/Public/Policies/Remove-JCPolicy.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
PowerShell/JumpCloud Module/Tests/Public/Policies/Remove-JCPolicy.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.