Need a restpi query which can delete past week actions using a pattern in their naming convention

Hi All, Would there be a rest api call which can help in deleting the past week expired actions after matching their naming patterns? Like if a action has prefix “ABCD” then it should delete the past week expired actions matching the prefix. Thank you!!

That wouldn’t be a single REST API call but you could build a script to leverage the REST API to do this.

2 Likes

Try something like this. Sorry about the formatting; everyting between the lines is powershell.


$relevance = “(name of it,id of it,state of it,name of issuer of it,now - time issued of it) of bes actions whose ((state of it = ““Stopped”” or state of it = ““Expired””) and (now - time issued of it > 7 * day) and (not exists parent group of it))”

$restName = “temp_user1”
$restPassword = “Password123”
$encodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($restName + ‘:’ + $restPassword)
$encodedPassword = [System.Convert]::ToBase64String($encodedAuthorization)
$headers = @{“Authorization”=“Basic $($encodedPassword)”}

$uri = “http://localhost:52311/api/query?relevance=$relevance
[xml]$XmlDocument = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers -verbose:$false

$csv = "C:\Temp\Report_Output.csv"
if (Test-Path $csv){
Remove-Item $csv
}

write-host “Processing …”
“Action Name||ID||State||Issued By||Age” | Out-File -FilePath $csv -Append -Encoding ascii | Out-Null
Write-Host “__________________________rn”

foreach ($entry in $XmlDocument.BESAPI.Query.Result.Tuple) {

$id = $entry.Answer[1].'#text'
    
Write-Host "Action Name: " $entry.Answer[0].'#text'
Write-Host "Action ID: " $entry.Answer[1].'#text'
Write-Host "State: " $entry.Answer[2].'#text'
Write-Host "Issued By: " $entry.Answer[3].'#text'
Write-Host "Age: " $entry.Answer[4].'#text'
Write-Host "__________________________`r`n"

$entry.Answer[0].’#text’ + “||” + $entry.Answer[1].’#text’ + “||” + $entry.Answer[2].’#text’ + “||” + $entry.Answer[3].’#text’ + “||” + $entry.Answer[4].’#text’ | Out-File -FilePath $csv -Append -Encoding ascii | Out-Null

try {
    $uri = "https://localhost:52311/api/action/$id"
    Invoke-WebRequest -Uri $uri -Method DELETE -Headers $headers -verbose:$false | out-null
    Write-host "Action [$id] deleted."
} catch {
    write-host "Something went wrong with [$uri], which returns error [$_]."   
}

}

write-host “Finished”


1 Like