PowerShell Script to Delete Data

Below is a sample PowerShell script to delete data from a Software Vulnerability Research system via automation.

Caution:Use extreme caution when running this script as THERE IS NO OPTION TO RESTORE DELETED DATA. The line #DeleteData ($URL + $item.id + "/") is commented out in the script by default. If you want the script to actually delete data, you need to uncomment this line.

$global:WebServiceHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$global:WebServiceHeader.Add("Content-Type", 'application/json')

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$LogFile = (Join-Path $PSScriptRoot  "CleanUp.txt")

$global:ErrorArray = @()

#PROD

$global:WebServiceHeader.Add("Authorization", 'Token YOURTOKENTOKEN')

$global:WebServiceURLSecunia = "https://api.app.flexerasoftware.com/api/"

function Write-Message ($Message, $Error)

    $Header = $Message

    if ($Error)

    {

        Write-Host $Header -ForegroundColor Yellow

    }

    else

    {

        Write-Host $Header -ForegroundColor Green

    }

    $Header | Out-File $LogFile -Append

function Display-Errors ()

{

    if ($global:ErrorArray.Count -eq 0)

    {

         Write-Message (" All Good "  + (Write-Header)) $false

    }

    else

    {

        Write-Message (" Errors: ") $true

        foreach ($item in $global:ErrorArray)

        {

            Write-Message ("     " + $item + (Write-Header)) $true

        }

    }

}

function DeleteData ($URL)

{

    try

    {

        $result = Invoke-RestMethod ($global:WebServiceURLSecunia + $URL) -Method Delete -Headers $global:WebServiceHeader

    }

    catch

    {

        $global:ErrorArray += ("Error QueryData " + $global:WebServiceURLSecunia + $URL + " " +  $_.Exception.Message + " " + $_.Exception.ItemName)

    }

}

function QueryData ($URL)

{

    # Get First Page of results (20 items)

    $result = @()

    $results = @()

    try

    {

        $result = Invoke-RestMethod ($global:WebServiceURLSecunia + $URL) -Method Get -Headers $global:WebServiceHeader

        $results = $result.results

    }

    catch

    {

        $global:ErrorArray += ("Error QueryData1 " + $global:WebServiceURLSecunia + $URL + " " +  $_.Exception.Message + " " + $_.Exception.ItemName)

    }

    #Get the next pages of results, if any

    while (![string]::IsNullOrWhiteSpace($result.next))

    {

        try

        {

            $result = Invoke-RestMethod $result.next -Method Get -Headers $global:WebServiceHeader

            $results += $result.results

        }

        catch

        {

            $global:ErrorArray += ("Error QueryData2 " + $URL + $result.next + " " +  $_.Exception.Message + " " + $_.Exception.ItemName)

            return $results

        }

    }

    return $results

}

function RemoveData ($URL, $match)

{

    $Hosts = QueryData $URL

    foreach  ($item in $Hosts)

    {

        if ($item.name -like $match)

        {

            Write-Message ('Deleting ' + $item.id + ' ' + $item.name) $true

            #DeleteData ($URL + $item.id + "/")

        }

        else

        {

            Write-Message ('Not Deleting ' +  $item.id + " " + $item.name) $false

        }

    }

}

RemoveData "inventory/hosts/" "*"

RemoveData "patch/customer-patch-templates/" "*"

RemoveData "patch/packages/" "*"

Display-Errors