public/Remove-VPASDPAPolicy.ps1
<#
.Synopsis DELETE DPA POLICY CREATED BY: Vadim Melamed, EMAIL: vmelamed5@gmail.com .DESCRIPTION USE THIS FUNCTION TO DELETE A POLICY FROM DPA .PARAMETER token HashTable of data containing various pieces of login information (PVWA, LoginToken, HeaderType, etc). If -token is not passed, function will use last known hashtable generated by New-VPASToken .PARAMETER PolicyID UniqueID of the target policy in DPA .PARAMETER PolicyName Unique name of the target policy in DPA .PARAMETER WhatIf Run code simulation to see what is affected by running the command as well as any possible implications This is a code simulation flag, meaning the command will NOT actually run .PARAMETER HideWhatIfOutput Suppress any code simulation output from the console .EXAMPLE $DeletePolicy = Remove-VPASDPAPolicy -PolicyID {POLICY ID VALUE} .EXAMPLE $DeletePolicy = Remove-VPASDPAPolicy -PolicyName {POLICY NAME VALUE} -WhatIf .OUTPUTS $true if successful --- $false if failed #> function Remove-VPASDPAPolicy{ [OutputType([bool],'System.Object')] [CmdletBinding()] Param( [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=0)] [String]$PolicyID, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=1)] [String]$PolicyName, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)] [Switch]$WhatIf, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=3)] [Switch]$HideWhatIfOutput, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=4)] [hashtable]$token ) Begin{ $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain = Get-VPASSession -token $token $CommandName = $MyInvocation.MyCommand.Name $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND } Process{ Write-Verbose "SUCCESSFULLY PARSED SUBDOMAIN VALUE" Write-Verbose "SUCCESSFULLY PARSED TOKEN VALUE" try{ if($SubDomain -eq "N/A"){ Write-VPASOutput -str "SelfHosted + PriviledgeCloud Standard solutions do not support this API Call, returning false" -type E $log = Write-VPASTextRecorder -inputval "SelfHosted + PrivilegeCloud Standard solutions do not support this API Call, returning false" -token $token -LogType MISC $log = Write-VPASTextRecorder -inputval $false -token $token -LogType RETURN return $false } if([String]::IsNullOrEmpty($PolicyID)){ Write-Verbose "NO POLICY ID PROVIDED...INVOKING HELPER FUNCTION TO RETRIEVE UNIQUE POLICY ID BASED ON SPECIFIED PARAMETERS" $PolicyID = Get-VPASDPAPolicyIDHelper -token $token -SearchQuery $PolicyName Write-Verbose "RETURNING POLICY ID" } else{ Write-Verbose "POLICY ID SUPPLIED, SKIPPING HELPER FUNCTION" } if($PolicyID -eq -1){ $log = Write-VPASTextRecorder -inputval "COULD NOT FIND TARGET POLICY ID: $PolicyID" -token $token -LogType MISC $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC Write-Verbose "COULD NOT FIND TARGET POLICY ID: $PolicyID" Write-VPASOutput -str "COULD NOT FIND TARGET POLICY ID: $PolicyID" -type E return $false } else{ write-verbose "MAKING API CALL TO CYBERARK" $uri = "https://$SubDomain.dpa.cyberark.cloud/api/access-policies/$PolicyID" Write-Verbose "CONSTRUCTING URI: $uri" $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI $log = Write-VPASTextRecorder -inputval "DELETE" -token $token -LogType METHOD if($WhatIf){ $log = Write-VPASTextRecorder -token $token -LogType WHATIF1 $WhatIfHash = @{} Write-Verbose "INITIATING COMMAND SIMULATION" $WhatIfInfo = Get-VPASDPAPolicyDetails -PolicyID $PolicyID -token $token $WhatIfPolicyID = $WhatIfInfo.policyId $WhatIfPolicyName = $WhatIfInfo.policyName $WhatIfStatus = $WhatIfInfo.status $WhatIfDescription = $WhatIfInfo.description $WhatIfStartDate = $WhatIfInfo.startDate $WhatIfEndDate = $WhatIfInfo.endDate $WhatIfProviderData = $WhatIfInfo.providersData $WhatIfUserAccessRules = $WhatIfInfo.userAccessRules if(!$HideWhatIfOutput){ Write-VPASOutput -str "====== BEGIN COMMAND SIMULATION ======" -type S Write-VPASOutput -str "THE FOLLOWING POLICY WOULD BE DELETED:" -type S Write-VPASOutput -str "PolicyID : $WhatIfPolicyID" -type S Write-VPASOutput -str "PolicyName : $WhatIfPolicyName" -type S Write-VPASOutput -str "Status : $WhatIfStatus" -type S Write-VPASOutput -str "Description : $WhatIfDescription" -type S Write-VPASOutput -str "StartDate : $WhatIfStartDate" -type S Write-VPASOutput -str "EndDate : $WhatIfEndDate" -type S Write-VPASOutput -str "ProviderData : $WhatIfProviderData" -type S Write-VPASOutput -str "UserAccessRules : $WhatIfUserAccessRules" -type S Write-VPASOutput -str "---" -type S Write-VPASOutput -str "URI : $uri" -type S Write-VPASOutput -str "METHOD : DELETE" -type S Write-VPASOutput -str " " -type S Write-VPASOutput -str "======= END COMMAND SIMULATION =======" -type S } $WhatIfHash = @{ WhatIf = @{ PolicyID = $WhatIfPolicyID PolicyName = $WhatIfPolicyName Status = $WhatIfStatus Description = $WhatIfDescription StartDate = $WhatIfStartDate EndDate = $WhatIfEndDate ProviderData = $WhatIfProviderData UserAccessRules = $WhatIfUserAccessRules RestURI = $uri RestMethod = "DELETE" Disclaimer = "THIS POLICY WILL BE DELETED IF -WhatIf FLAG IS REMOVED" } } $WhatIfJSON = $WhatIfHash | ConvertTo-Json | ConvertFrom-Json $log = Write-VPASTextRecorder -inputval $WhatIfJSON -token $token -LogType RETURNARRAY $log = Write-VPASTextRecorder -token $token -LogType WHATIF2 return $WhatIfJSON } else{ if($sessionval){ $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method DELETE -ContentType "application/json" -WebSession $sessionval } else{ $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method DELETE -ContentType "application/json" } $log = Write-VPASTextRecorder -inputval $response -token $token -LogType RETURNARRAY Write-Verbose "SUCCESSFULLY DELETED DPA POLICY: $PolicyID" Write-Verbose "RETURNING TRUE" return $true } } }catch{ $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC Write-Verbose "FAILED TO DELETE DPA POLICY" Write-VPASOutput -str $_ -type E return $false } } End{ $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER } } |