· 7 years ago · Aug 28, 2018, 02:00 PM
1Try
2{
3 $VPCs = $null
4 $VPCs = Get-EC2Vpc -Filter @{Name="isDefault"; Values="true"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
5 $Script:TotalAPICalls ++
6 ##Write-Host "Successfully gathered VPCs in account $Account" -ForegroundColor Green
7}
8Catch
9{
10 Write-Host " ERROR gathered VPCs in account $Account - $_" -ForegroundColor Red
11 break
12}
13foreach($VPC in $VPCs)
14{
15 $VPCId = $null
16 $VPCId = $VPC.VpcId
17 $VPCCIDR = $null
18 $VPCCIDR = $VPC.CidrBlock
19 Write-Host "Default VPC Found - $VPCId - CIDR: $VPCCIDR in Region: $Region on AWS Account: $Account" -ForegroundColor Green
20
21 Try
22 {
23 $Instances = $null
24 $Instances = Get-EC2Instance -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
25 $Script:TotalAPICalls ++
26 }
27 Catch
28 {
29 Write-Host " ERROR GETTING Instances in account $Account and region: $Region - $_" -ForegroundColor Red
30 }
31 if($Instances)
32 {
33 Write-Host " THERE ARE INSTANCES IN THIS REGION IN THE DEFAULT VPC in account $Account and region: $Region - $_" -ForegroundColor Red
34 pause
35 }
36
37 Try
38 {
39 $NICs = $null
40 $NICs = Get-EC2NetworkInterface -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
41 $Script:TotalAPICalls ++
42 }
43 Catch
44 {
45
46 Write-Host " ERROR GETTING NICs in account $Account and region: $Region - $_" -ForegroundColor Red
47 }
48 if($NICs)
49 {
50 foreach($NIC in $NICs)
51 {
52
53 $Attached = $null
54 $Attached = $NIC.Attachment
55 foreach($Attach in $Attached)
56 {
57 $AttachId = $null
58 $AttachId = $Attach.AttachmentId
59 $NICId = $null
60 $NICId = $NIC.NetworkInterfaceId
61
62 Dismount-EC2NetworkInterface -AttachmentId $AttachId -ForceDismount:$true -Force -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken
63
64 Remove-EC2NetworkInterface -NetworkInterfaceId $NICId -Force -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken
65 }
66 }
67 }
68
69
70
71 Try
72 {
73 $IGW = $null
74 $IGW = (Get-EC2InternetGateway -Filter @{Name="attachment.vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop).InternetGatewayId
75 $Script:TotalAPICalls ++
76 }
77 Catch
78 {
79 Write-Host " ERROR GETTING IGWs in account $Account and region: $Region - $_" -ForegroundColor Red
80 }
81 if($IGW)
82 {
83 Try
84 {
85 Dismount-EC2InternetGateway -InternetGatewayId $IGW -VpcId $VPCId -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
86 $Script:TotalAPICalls ++
87 Write-Host " DISMOUNTING INTERNET GATEWAY $IGW from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
88 }
89 Catch
90 {
91 Write-Host " ERROR DISMOUNTING IGW $IGW from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
92 }
93
94 Try
95 {
96 Remove-EC2InternetGateway -InternetGatewayId $IGW -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
97 $Script:TotalAPICalls ++
98 Write-Host " REMOVING INTERNET GATEWAY $IGW from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
99 }
100 Catch
101 {
102 Write-Host " ERROR REMOVING IGW $IGW from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
103 }
104
105 }
106 Try
107 {
108 $SubnetObjects = $null
109 $SubnetObjects = Get-EC2Subnet -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
110 $Script:TotalAPICalls ++
111 foreach($Sub in $SubnetObjects)
112 {
113
114 $SubnetId = $null
115 $SubnetId = $Sub.SubnetId
116 Try
117 {
118 Remove-EC2Subnet -SubnetId $SubnetId -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
119 $Script:TotalAPICalls ++
120 Write-Host " REMOVING SUBNET $SubnetId from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
121 }
122 Catch
123 {
124 Write-Host " ERROR REMOVING SUBNET from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
125 }
126
127 }
128 }
129 Catch
130 {
131 Write-Host " ERROR GETTING SUBNET from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
132 }
133
134
135 Try
136 {
137
138 $RouteTables = $null
139 $RouteTables = Get-EC2RouteTable -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
140 $Script:TotalAPICalls ++
141 foreach($RouteTable in $RouteTables)
142 {
143 $RouteTableId = $null
144 $RouteTableAssociations = $null
145 $RouteTableId = $RouteTable.RouteTableId
146 $RouteTableAssociations = $RouteTable.Associations
147
148 foreach ($RTBAssoc in $RouteTableAssociations)
149 {
150
151 if ($RTBAssoc.Main)
152 {
153 ######### don't touch the main route table
154 Write-Output "$Account; $AccountName; $Region; $DefaultVPC; main route table $RouteTableId"
155 }
156 else
157 {
158 $RTBAssocId = $RTBAssoc.RouteTableAssociationId
159 #Write-Output "$Account; $AccountName; $Region; $DefaultVPC; route table to delete $RouteTableId; $RTBAssocId"
160
161 ######### let's unregister the route table to prepare to delete it
162 Write-Output "$Account; $AccountName; $Region; $DefaultVPC; detaching route table $RouteTableId; $RTBAssocId"
163
164 try
165 {
166 $RTBUnregister = Unregister-EC2RouteTable -AssociationId $RTBAssocId -Region $Region -Force -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
167 }
168
169 catch
170 {
171 $Failures = "Yes"
172 Write-Output "$Account; $AccountName; $Region; ERROR ERROR ERROR on GET-EC2Instance"
173 $ErrorMessage = $_.Exception.Message
174 $FailedItem = $_.Exception.ItemName
175 Write-Output "`n $ErrorMessage "
176 Write-Output "`n $FailedItem "
177 }
178
179 Write-Output "$Account; $AccountName; $Region; $DefaultVPC; route table detached $RouteTableId; $RTBAssocId"
180
181
182 ######### now time to delete the route table
183 Write-Output "$Account; $AccountName; $Region; $DefaultVPC; deleting route table $RouteTableId; $RTBAssocId"
184
185 try
186 {
187 $RTBDelete = Remove-EC2RouteTable -RouteTableId $RouteTableId -Region $Region -Force -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
188 }
189
190 catch
191 {
192 $Failures = "Yes"
193 Write-Output "$Account; $AccountName; $Region; ERROR ERROR ERROR on Remove-EC2RouteTable"
194 $ErrorMessage = $_.Exception.Message
195 $FailedItem = $_.Exception.ItemName
196 Write-Output "`n $ErrorMessage "
197 Write-Output "`n $FailedItem "
198 }
199
200 Write-Output "$Account; $AccountName; $Region; $DefaultVPC; deleted route table $RouteTableId; $RTBAssocId"
201
202 }
203 }
204
205
206
207
208
209 }
210 }
211 Catch
212 {
213 Write-Host "Error getting ROUTETABLE INFO" -ForegroundColor Red
214 }
215
216
217 Try
218 {
219 $NACLs = $null
220 $NACLs = Get-EC2NetworkAcl -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
221 $Script:TotalAPICalls ++
222 }
223 Catch
224 {
225 Write-Host " ERROR GETTING NACL from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
226 }
227
228
229 foreach($NACL in $NACLs)
230 {
231 $NACLId =$null
232 $NACLId = $NACL.NetworkAclId
233
234 if($NACLId)
235 {
236
237 Try
238 {
239 Remove-EC2NetworkAcl -NetworkAclId $NACLId -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
240 $Script:TotalAPICalls ++
241 Write-Host " REMOVING NACL $NACLId from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
242 }
243 Catch
244 {
245 Write-Host " ERROR REMOVING NACL $NACLId from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
246 }
247
248 }
249 }
250
251 Try
252 {
253 $SecurityGroups = $null
254 $SecurityGroups = Get-EC2SecurityGroup -Filter @{Name="vpc-id"; Values="$VPCId"} -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop
255 $Script:TotalAPICalls ++
256
257 }
258 Catch
259 {
260 Write-Host " ERROR GETTING EC2 SECURITY GROUP from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
261 }
262
263 foreach($SecurityGroup in $SecurityGroups)
264 {
265 Try
266 {
267 if(!$SecurityGroup.GroupName)
268 {
269 $SecurityGroupId = $null
270 $SecurityGroupId = $SecurityGroup.GroupId
271 Remove-EC2SecurityGroup -GroupId $SecurityGroupId -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
272 $Script:TotalAPICalls ++
273 Write-Host " REMOVING EC2 SECURITY GROUP $SecurityGroupId from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
274 }
275 }
276 Catch
277 {
278 Write-Host " ERROR REMOVING EC2 SECURITY GROUP from VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
279 }
280 }
281
282 Try
283 {
284 Remove-EC2Vpc -VpcId $VPCId -Region $Region -AccessKey $AccessKey -SecretKey $SecretAccessKey -SessionToken $SessionToken -ErrorAction Stop -Force
285 $Script:TotalAPICalls ++
286 Write-Host " REMOVING VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Green
287
288 }
289 Catch
290 {
291 Write-Host " ERROR REMOVING VPC $VPCId in region $Region and account $Account - $_" -ForegroundColor Red
292 }
293
294
295 }