Public/Remove-RiskSenseHost.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Remove-RiskSenseHost {
    <#
    .SYNOPSIS
        Remove RiskSense Hosts
    .DESCRIPTION
        Delete hosts from RiskSense.
    .EXAMPLE
        Remove-RiskSenseHost -ClientID 1 -HostID 1, 5, 10 -Token 'secrettoken'
    #>


    [CmdletBinding()]
    param(
        # ClientID
        [Parameter(Mandatory,
                   Position=0,
                   ValueFromPipelineByPropertyName)]
        [int]$ClientID,

        # HostID
        [Parameter(Mandatory,
            Position=1,
            ValueFromPipelineByPropertyName)]
        [Alias("ID")]
        [String[]]$HostID,

        # RiskSense API Key
        [Parameter(Mandatory)]
        $Token
    )

    begin {
        $headers = Get-AuthHeader $Token
        $hostTable = @()
    }

    process {
        # Build a table of each ClientID and HostID in a 1-to-1 relationship
        foreach ($id in $HostID) {
            $hostTable += [PSCustomObject]@{
                ClientID = $ClientID
                HostID = $id       
            }
        }
    }

    end {
        # Submit one job per ClientID by grouping the objects
        $hostTable | Group-Object ClientID | ForEach-Object {
            $body = @{
                filterRequest = @{
                    filters = @(@{
                        field = 'id'
                        exclusive = $false 
                        operator = 'IN'
                        value = $_.Group.HostID -join ','
                    })
                }
            } | ConvertTo-Json -Depth 10 -Compress
            $result = Invoke-RestMethod -Uri "$uri/client/$($_.Name)/host/delete" -Method Post -Body $body -Headers $headers
            [PSCustomObject]@{
                JobID = $result.id
                Created = Get-Date $result.created
            }
        }
    }
}