-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressDelete
More file actions
159 lines (132 loc) · 4.42 KB
/
Copy pathAddressDelete
File metadata and controls
159 lines (132 loc) · 4.42 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
param(
[string]$FortiGate = "TARGET",
[string]$ApiToken = "RESTAPIKEY",
[string]$MatchPhrase = 'VPN_Block',
[string]$ExcludePhrase = 'ADDR',
[switch]$SkipCertificateCheck,
[switch]$DryRun = $true
)
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if ($SkipCertificateCheck) {
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) { return true; }
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}
$FortiGate = $FortiGate.Trim()
if ($FortiGate -notmatch '^https?://') {
$FortiGate = "https://$FortiGate"
}
$FortiGate = $FortiGate.TrimEnd('/')
$baseUri = "$FortiGate/api/v2/cmdb/firewall/address"
$headers = @{
Authorization = "Bearer $ApiToken"
Accept = "application/json"
}
function Invoke-FortiGateApi {
param(
[ValidateSet('GET','DELETE')]
[string]$Method,
[string]$Uri
)
Invoke-RestMethod -Method $Method -Uri $Uri -Headers $headers -ContentType 'application/json'
}
function Get-MatchingAddresses {
$encodedMatch = [System.Uri]::EscapeDataString("name=@$MatchPhrase")
$uri = "$($baseUri)?format=name&q_origin_key&filter=$encodedMatch"
Write-Host "GET $uri"
$resp = Invoke-FortiGateApi -Method GET -Uri $uri
if (-not $resp.results) {
return @()
}
return $resp.results | ForEach-Object {
$n = if ($_.q_origin_key) { $_.q_origin_key } elseif ($_.name) { $_.name } else { $null }
if ($n) {
[PSCustomObject]@{ Name = $n }
}
} | Sort-Object Name -Unique
}
function Remove-AddressByName {
param([string]$Name)
$encodedName = [System.Uri]::EscapeDataString($Name)
$uri = "$($baseUri)/$encodedName"
Invoke-FortiGateApi -Method DELETE -Uri $uri | Out-Null
}
try {
Write-Host "Using base URI: $baseUri"
Write-Host "Finding address objects containing '$MatchPhrase' but excluding '$ExcludePhrase'..."
$allCandidates = Get-MatchingAddresses
$targets = $allCandidates | Where-Object {
$_.Name -notlike "*$ExcludePhrase*"
}
$skipped = $allCandidates | Where-Object {
$_.Name -like "*$ExcludePhrase*"
}
Write-Host ""
Write-Host "Matched address objects: $($allCandidates.Count)"
Write-Host "Will delete: $($targets.Count)"
Write-Host "Will preserve: $($skipped.Count)"
Write-Host ""
if ($targets.Count -gt 0) {
Write-Host "Delete candidates:"
$targets | Select-Object -First 100 | ForEach-Object {
Write-Host " [DELETE] $($_.Name)"
}
if ($targets.Count -gt 100) {
Write-Host " ...and $($targets.Count - 100) more"
}
}
Write-Host ""
if ($skipped.Count -gt 0) {
Write-Host "Preserved because they contain '$ExcludePhrase':"
$skipped | Select-Object -First 100 | ForEach-Object {
Write-Host " [SKIP] $($_.Name)"
}
if ($skipped.Count -gt 100) {
Write-Host " ...and $($skipped.Count - 100) more"
}
}
if ($DryRun) {
Write-Host ""
Write-Host "DRY RUN enabled. No address objects were deleted."
Write-Host "Re-run with -DryRun:`$false to perform deletion."
return
}
Write-Host ""
Write-Host "Deleting $($targets.Count) address objects..."
$deleted = 0
$failed = New-Object System.Collections.Generic.List[object]
foreach ($t in $targets) {
try {
Remove-AddressByName -Name $t.Name
$deleted++
Write-Host "[DELETED] $($t.Name)"
}
catch {
Write-Warning "[FAILED ] $($t.Name) -- $($_.Exception.Message)"
$failed.Add([PSCustomObject]@{
Name = $t.Name
Error = $_.Exception.Message
})
}
}
Write-Host ""
Write-Host "Done."
Write-Host "Deleted: $deleted"
Write-Host "Failed : $($failed.Count)"
if ($failed.Count -gt 0) {
$failed | Export-Csv -NoTypeInformation -Path ".\fortigate_address_delete_failures.csv"
Write-Host "Failure log written to .\fortigate_address_delete_failures.csv"
}
}
catch {
Write-Error $_.Exception.Message
exit 1
}