Get-APIRemoval.ps1

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
[CmdletBinding(DefaultParameterSetName = 'Everything')]
Param (
    [Parameter(ParameterSetName = 'Everything')]
    [switch]$ShowEverything,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowCompleted,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowTodo,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowSkipped,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowBugs,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowRequiresWork,

    [Parameter(ParameterSetName = 'NotAll')]
    [switch]$ShowCounts,

    [Parameter(ParameterSetName = 'Everything')]
    [switch]$OutputStats
)

Remove-Variable * -Exclude ('ShowEverything','ShowCompleted','ShowTodo','ShowSkipped','ShowBugs','ShowRequiresWork','ShowCounts','OutputStats') -ErrorAction SilentlyContinue
Clear-Host

[string]  $path     = '/home/mike/Development/PowerShell/Rapid7/Rapid7Nexpose/public/'    # Module Path
[object[]]$scripts  = (Get-ChildItem -Path $path -Filter '*.ps1')

Function Get-ScriptData {
    [int]$cnt = 0
    [hashtable]$functionality = @{}
    ForEach ($item In $scripts) {
        [string]$name = $item.BaseName
        Write-Progress -Id 0 -Activity 'Loading Functions' -CurrentOperation $name -PercentComplete ((100 / ($scripts.Count)) * $cnt++)
        Write-Verbose -Message $name
        . "$path$name.ps1"    # Dot-Source Function

        [object]$Help = (Get-Help -Name $name -ErrorAction SilentlyContinue -Full)
        If (([string]::IsNullOrEmpty($Help.Functionality) -eq $false) -and (($Help.Functionality) -ne 'None')) {
            ForEach ($func In ($Help.Functionality -split '\n')) {
                [string]$act =  $func.Split(':')[0].Trim()
                [string]$url = ($func.Split(':')[1]).Split('#')[0].Trim()
                If ($url -match '\-') { $url = $url.Split('-')[1].Trim() }

                If (-not $functionality.$url)      { $functionality.Add($url, @{})      }
                If (-not $functionality.$url.$act) { $functionality.$url.Add($act, @()) }
                $functionality.$url.$act += "$item ($($act.ToUpper()))"
            }
        }
    }

    Write-Progress -Id 0 -Completed -Activity 'Loading Functions'
    Return $functionality
}

[int]   $port     = 3780
[string]$Hostname = '192.168.42.108'
[string]$Username = 'APIUser'
[string]$Password = 'Passw0rd'
[pscredential]$Credential = (New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $Username, ($Password | ConvertTo-SecureString -AsPlainText -Force))
$Status = (Connect-NexposeAPI -HostName $Hostname -Port $port -Credential $Credential -SkipSSLCheck)
Write-Host "$($status.StatusCode) $($Status.StatusDescription)"
If ($Status.StatusDescription -ne 'OK') {
    Throw "Failed to connect to the Nexpose API - $($Status.StatusCode) $($Status.StatusDescription)"
}

[psobject]$API = (Invoke-RestMethod -WebSession $Global:NexposeSession -Method Get -Uri "https://$($hostname):$($port)/api/3/json" -SkipCertificateCheck)
[hashtable]$funcScriptData = Get-ScriptData
[hashtable]$funcApiData    = @{}

[int]$cnt = 0
[int]$tot = ($API.paths | Get-Member -MemberType *Property).Count

ForEach ($func In $(($API.paths | Get-Member -MemberType NoteProperty).Name)) {
    If ($func -eq '/api/3') { Continue }
    $func = $func.Replace('/api/3/', '')
    Write-Progress -Id 1 -Activity 'Parsing Data' -CurrentOperation $func -PercentComplete ((100 / $tot) * $cnt++)

    ForEach ($method In ($api.paths."/api/3/$func" | Get-Member -MemberType NoteProperty)) {

        If (-not $funcApiData.$func) { $funcApiData.Add($func, @{}) }
        If (-not $funcApiData.$func.$($method.Name)) { $funcApiData.$func.Add($($method.Name), @()) }

        If ($funcScriptData.$func.$($method.Name)) {
            $funcApiData.$func.$($method.Name) += ($funcScriptData.$func.$($method.Name))
            $funcScriptData.$func.Remove($($method.Name))
        }
        Else {
            $funcApiData.$func.$($method.Name) += 'MISSING'
            $script:Todo++
        }
    }

    If ($funcScriptData.$func.Count -eq 0) { $funcScriptData.Remove($func) }
}

$funcScriptData | ConvertTo-Json -Depth 100