public/Remove-VPASEPVGroup.ps1
<#
.Synopsis DELETE EPV GROUP CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com .DESCRIPTION USE THIS FUNCTION TO DELETE AN EPV GROUP .LINK https://vpasmodule.com/commands/Remove-VPASEPVGroup .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 GroupName Target EPV group name that a user will be added to .PARAMETER GroupID Target EPV group ID that a user will be added to .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 .PARAMETER InputParameters HashTable of values containing the parameters required to make the API call .EXAMPLE $WhatIfSimulation = Remove-VPASEPVGroup -GroupName {GROUPNAME VALUE} -WhatIf .EXAMPLE $DeleteEPVGroupStatus = Remove-VPASEPVGroup -GroupName {GROUPNAME VALUE} .EXAMPLE $DeleteEPVGroupStatus = Remove-VPASEPVGroup -GroupID {GROUPID VALUE} .EXAMPLE $InputParameters = @{ GroupName = "TargetGroupName" WhatIf = $true|$false HideWhatIfOutput = $true|$false } $DeleteEPVGroupStatus = Remove-VPASEPVGroup -InputParameters $InputParameters .EXAMPLE $InputParameters = @{ GroupID = "22_123" WhatIf = $true|$false HideWhatIfOutput = $true|$false } $DeleteEPVGroupStatus = Remove-VPASEPVGroup -InputParameters $InputParameters .OUTPUTS $true if successful --- $false if failed #> function Remove-VPASEPVGroup{ [OutputType([bool],'System.Object')] [CmdletBinding(DefaultParameterSetName='Set1')] Param( [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Perform search on target EPVGroup via GroupName")] [String]$GroupName, [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Perform search on target EPVGroup via GroupID")] [String]$GroupID, [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)] [Parameter(Mandatory=$false,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true)] [Switch]$WhatIf, [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)] [Parameter(Mandatory=$false,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true)] [Switch]$HideWhatIfOutput, [Parameter(Mandatory=$true,ParameterSetName='InputParameters',ValueFromPipelineByPropertyName=$true,HelpMessage="Hashtable of parameters required to make API call, refer to get-help -examples for valid inputs")] [hashtable]$InputParameters, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)] [hashtable]$token ) Begin{ $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain,$EnableTroubleshooting = Get-VPASSession -token $token $CommandName = $MyInvocation.MyCommand.Name $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND } Process{ try{ if($PSCmdlet.ParameterSetName -eq "InputParameters"){ $KeyHash = @{ set1 = @{ AcceptableKeys = @("GroupName","WhatIf","HideWhatIfOutput") MandatoryKeys = @("GroupName") } set2 = @{ AcceptableKeys = @("GroupID","WhatIf","HideWhatIfOutput") MandatoryKeys = @("GroupID") } } $CheckSet = Test-VPASHashtableKeysHelper -InputHash $InputParameters -KeyHash $KeyHash if(!$CheckSet){ $log = Write-VPASTextRecorder -inputval "FAILED TO FIND TARGET PARAMETER SET" -token $token -LogType MISC Write-Verbose "FAILED TO FIND TARGET PARAMETER SET" Write-VPASOutput -str "FAILED TO FIND TARGET PARAMETER SET...VIEW EXAMPLES BELOW:" -type E $examples = Write-VPASExampleHelper -CommandName $CommandName return $false } else{ foreach($key in $InputParameters.Keys){ Set-Variable -Name $key -Value $InputParameters.$key } } } }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 RETRIEVE EPV GROUPS" Write-VPASOutput -str $_ -type E return $false } try{ if($GroupID){ write-verbose "SUPPLIED GROUPID, SKIPPING HELPER FUNCTION" } else{ Write-Verbose "CONSTRUCTING SEARCH STRING TO QUERY CYBERARK" $searchQuery = "$GroupName" Write-Verbose "INVOKING HELPER FUNCTION TO RETRIEVE GROUPID" $GroupID = Get-VPASEPVGroupIDHelper -token $token -GroupName $GroupName } if($NoSSL){ Write-Verbose "NO SSL ENABLED, USING HTTP INSTEAD OF HTTPS" $uri = "http://$PVWA/PasswordVault/api/UserGroups/$GroupID" } else{ Write-Verbose "SSL ENABLED BY DEFAULT, USING HTTPS" $uri = "https://$PVWA/PasswordVault/api/UserGroups/$GroupID" } $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-VPASAllEPVGroups -IncludeMembers -token $token foreach($WhatIfGroup in $WhatIfInfo.value){ $WhatIfGroupID = $WhatIfGroup.id $WhatIfGroupGroupType = $WhatIfGroup.groupType $WhatIfGroupMembers = $WhatIfGroup.members $WhatIfGroupGroupName = $WhatIfGroup.groupName $WhatIfGroupDescription = $WhatIfGroup.description $WhatIfGroupLocation = $WhatIfGroup.location if($WhatIfGroupID -eq $GroupID){ #AFFECTED MEMBERS $WhatIfMembers = @() foreach($mem in $WhatIfGroupMembers){ $targetuser = $mem.username $targetid = $mem.id $minihash = @{ username = $targetuser id = $targetid } $WhatIfMembers += $minihash } #AFFECTED SAFES $WhatIfAffectedSafesCount = 0 $WhatIfAffectedSafes = @() $CheckSafes = Get-VPASAllSafes -token $token foreach($safe in $CheckSafes.value){ $CheckSafeName = $safe.safeName $CheckTargetGroup = Get-VPASSafeMemberSearch -safe $CheckSafeName -member $WhatIfGroupGroupName -token $token 6> $null if($CheckTargetGroup){ $WhatIfAffectedSafesCount += 1 $WhatIfAffectedSafes += $CheckSafeName } } #AFFECTED ACCOUNTS $WhatIfAffectedAccountsCounter = 0 $WhatIfAffectedAccounts = @() foreach($safe in $WhatIfAffectedSafes){ $miniHash = @{} $AffectedAccounts = Get-VPASAccountDetails -safe $safe -token $token -HideWarning foreach($AffectedAcct in $AffectedAccounts.value){ $AffectedAcctSafe = $AffectedAcct.safeName if($AffectedAcctSafe -eq $safe){ $WhatIfAffectedAccountsCounter += 1 $miniHash = @{ SafeName = $AffectedAcct.safeName ID = $AffectedAcct.id Address = $AffectedAcct.address Username = $AffectedAcct.userName Name = $AffectedAcct.name } $WhatIfAffectedAccounts += $miniHash } } } if(!$HideWhatIfOutput){ Write-VPASOutput -str "====== BEGIN COMMAND SIMULATION ======" -type S Write-VPASOutput -str "THE FOLLOWING EPV GROUP WOULD BE DELETED:" -type S Write-VPASOutput -str "ID : $WhatIfGroupID" -type S Write-VPASOutput -str "GroupType : $WhatIfGroupGroupType" -type S Write-VPASOutput -str "Members : $WhatIfMembers" -type S Write-VPASOutput -str "GroupName : $WhatIfGroupGroupName" -type S Write-VPASOutput -str "Description : $WhatIfGroupDescription" -type S Write-VPASOutput -str "Location : $WhatIfGroupLocation" -type S Write-VPASOutput -str "NumberOfAffectedAccounts : $WhatIfAffectedAccountsCounter" -type S Write-VPASOutput -str "AffectedAccounts : $WhatIfAffectedAccounts" -type S Write-VPASOutput -str "NumberOfAffectedSafes : $WhatIfAffectedSafesCount" -type S Write-VPASOutput -str "AffectedSafes : $WhatIfAffectedSafes" -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 = @{ ID = $WhatIfGroupID GroupType = $WhatIfGroupGroupType Members = $WhatIfMembers GroupName = $WhatIfGroupGroupName Description = $WhatIfGroupDescription Location = $WhatIfGroupLocation NumberOfAffectedSafes = $WhatIfAffectedSafesCount AffectedSafes = $WhatIfAffectedSafes NumberOfAffectedAccounts = $WhatIfAffectedAccountsCounter AffectedAccounts = $WhatIfAffectedAccounts RestURI = $uri RestMethod = "DELETE" Disclaimer = "THIS EPV GROUP WILL BE DELETED IF -WhatIf FLAG IS REMOVED" Disclaimer2 = "THIS CODE SIMULATION DOES NOT DIG INTO NESTED GROUPS...YET..." } } $WhatIfJSON = $WhatIfHash | ConvertTo-Json | ConvertFrom-Json $log = Write-VPASTextRecorder -inputval $WhatIfJSON -token $token -LogType RETURNARRAY $log = Write-VPASTextRecorder -token $token -LogType WHATIF2 return $WhatIfJSON } } $log = Write-VPASTextRecorder -inputval "UNABLE TO FIND TARGET EPV GROUP ID...RETURNING FALSE" -token $token -LogType MISC $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC $log = Write-VPASTextRecorder -token $token -LogType WHATIF2 Write-Verbose "UNABLE TO FIND TARGET EPV GROUP ID...RETURNING FALSE" Write-VPASOutput "UNABLE TO FIND TARGET EPV GROUP ID" -type E return $false } else{ Write-Verbose "MAKING API CALL TO CYBERARK" 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 "REST API COMMAND RETURNED: TRUE" -token $token -LogType MISC Write-Verbose "SUCCESSFULLY DELETED $GroupID" 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 "UNABLE TO DELETE $GroupID" Write-VPASOutput -str $_ -type E return $false } } End{ $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER } } |