-
Notifications
You must be signed in to change notification settings - Fork 2
/
NsxObjectCapture.ps1
312 lines (252 loc) · 9.93 KB
/
NsxObjectCapture.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#NSX Object Capture Script
#Nick Bradford
#Version 0.1
<#
Copyright © 2015 VMware, Inc. All Rights Reserved.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License version 2 for more details.
You should have received a copy of the General Public License version 2 along with this program.
If not, see https://www.gnu.org/licenses/gpl-2.0.html.
The full text of the General Public License 2.0 is provided in the COPYING file.
Some files may be comprised of various open source software components, each of which
has its own license that is located in the source code of the respective component.
#>
#Requires -Version 3.0
#Requires -Modules PowerNSX
#This script captures the necessary objects information from NSX and persists
#them to disk in order for topology reconstruction to be done by a sister script
#NSXDiagram.ps1.
param (
[pscustomobject]$Connection=$DefaultNsxConnection
)
If ( (-not $Connection) -and ( -not $Connection.ViConnection.IsConnected ) ) {
throw "No valid NSX Connection found. Connect to NSX and vCenter using Connect-NsxServer first. You can specify a non default PowerNSX Connection using the -connection parameter."
}
Set-StrictMode -Off
#########################
#Define Windows environment stuff
$filename = "NSX-ObjectCapture-$($Connection.Server)-$(get-date -format "yyyy_MM_dd_HH_mm_ss").zip"
if ($psversiontable.PSEdition -ne "Core") {
#$ExportPath = "$([system.Environment]::GetFolderPath('MyDocuments'))\VMware\NSXObjectCapture"
$ExportPath = "$PSScriptRoot\NSXSourceCapture"
$ExportFile = "$ExportPath\$filename"
$TempDir = "$($env:Temp)\VMware\NSXObjectCapture"
}
else {
#zip output path
$ExportPath = "/tmp/VMware/NSXObjectCaptureOutput"
$ExportFile = "$ExportPath/$filename"
#XML output folder
$TempDir = "/tmp/VMware/NSXObjectCapture/"
#Create core dir if required
if ( -not ( test-path $ExportPath )) {
New-Item -Type Directory $ExportPath | out-null
}
}
#Export
$maxdepth = 5
$maxCaptures = 10
if ( -not ( test-path $TempDir )) {
New-Item -Type Directory $TempDir | out-null
}
else {
Get-ChildItem $TempDir | Remove-Item -force -recurse
}
if ( -not ( test-path $ExportPath )) {
New-Item -Type Directory $ExportPath | out-null
}
$LsExportFile = "$TempDir\LsExport.xml"
$VdPgExportFile = "$TempDir\VdPgExport.xml"
$StdPgExportFile = "$TempDir\StdPgExport.xml"
$LrExportFile = "$TempDir\LrExport.xml"
$EdgeExportFile = "$TempDir\EdgeExport.xml"
$VmExportFile = "$TempDir\VmExport.xml"
$CtrlExportFile = "$TempDir\CtrlExport.xml"
$MacAddressExportFile = "$TempDir\MacExport.xml"
$IpSetExportFile = "$TempDir\IpSetExport.xml"
$ServicesExportFile = "$TempDir\ServicesExport.xml"
$ServiceGroupExportFile = "$TempDir\ServiceGroupExport.xml"
$SecurityGroupExportFile = "$TempDir\SecurityGroupExport.xml"
$SecurityTagExportFile = "$TempDir\SecurityTagExport.xml"
$SecPolExportFile = "$TempDir\SecPolExport.xml"
$dfwConfigFile = "$TempDir\DfwConfigExport.xml"
$LsHash = @{}
$VdPortGroupHash = @{}
$StdPgHash = @{}
$LrHash = @{}
$EdgeHash = @{}
$VmHash = @{}
$CtrlHash = @{}
$MacHash = @{}
$IpSetHash = @{}
$ServiceHash = @{}
$ServiceGroupHash = @{}
$SecurityGroupHash = @{}
$SecurityTagHash = @{}
$SecPolHash = @{}
write-host -ForeGroundColor Green "PowerNSX Object Capture Script"
write-host -ForeGroundColor Green "`nGetting NSX Objects"
write-host " Getting LogicalSwitches"
Get-NsxLogicalSwitch -connection $connection | % {
$LsHash.Add($_.objectId, $_.outerXml)
}
write-host " Getting DV PortGroups"
Get-VDPortGroup -server $connection.ViConnection | % {
if ( $_.VlanConfiguration ) {
if ( $_.VlanConfiguration.VlanId ) {
$VlanID = $_.VlanConfiguration.VlanId
}
else {
$VlanId = "0"
}
}
else {
$VlanId = "0"
}
$VdPortGroupHash.Add( $_.ExtensionData.Moref.Value, [pscustomobject]@{ "MoRef" = $_.ExtensionData.Moref.Value; "Name" = $_.Name; "VlanId" = $VlanId } )
}
write-host " Getting VSS PortGroups"
Get-VirtualPortGroup -server $connection.ViConnection | ? { $_.key -match 'key-vim.host.PortGroup'} | Sort-Object -Unique | % {
$StdPgHash.Add( $_.Name, [pscustomobject]@{ "Name" = $_.Name; "VlanId" = $VlanId } )
}
write-host " Getting Logical Routers"
$LogicalRouters = Get-NsxLogicalRouter -connection $connection
$LogicalRouters | % {
$LrHash.Add($_.Id, $_.outerXml)
}
write-host " Getting Edges"
$edges = Get-NsxEdge -connection $connection
$edges | % {
$EdgeHash.Add($_.id, $_.outerxml)
}
write-host " Getting NSX Controllers"
$Controllers = Get-NsxController -connection $connection
$Controllers | % {
$CtrlHash.Add($_.id, $_.outerxml)
}
write-host " Getting VMs"
Get-Vm -server $connection.ViConnection| % {
$IsManager = $false
$IsEdge = $false
$IsLogicalRouter = $false
$IsController = $false
$Nics = @()
#Tag any edge, DLR or controller vms...
$moref = $_.id.replace("VirtualMachine-","")
if ( $Edges.appliances.appliance.vmid ) {
if ( $Edges.appliances.appliance.vmid.Contains($moref) ) {
$IsEdge = $true
}
}
if ( $LogicalRouters.appliances.appliance.vmid ) {
if ( $LogicalRouters.appliances.appliance.vmid.Contains($moref) ) {
$IsLogicalRouter = $true
}
}
if ( $Controllers.virtualMachineInfo.objectId ) {
if ( $Controllers.virtualMachineInfo.objectId.Contains($moref) ) {
$IsController = $true
}
}
#NSX Keeps some metadata about Managers and Edges (not controllers) in the extraconfig data of the associated VMs.
$configview = $_ | Get-View -Property Config
$NSXAppliance = ($configview.Config.ExtraConfig | ? { $_.key -eq "vshield.vmtype" }).Value
If ( $NSXAppliance -eq "Manager" ) {
$IsManager = $true
}
$_ | Get-NetworkAdapter -server $connection.ViConnection | % {
If ( $_.ExtensionData.Backing.Port.PortgroupKey ) {
$PortGroup = $_.ExtensionData.Backing.Port.PortgroupKey;
}
elseif ( $_.NetworkName ) {
$PortGroup = $_.NetworkName;
}
else {
#No nic attachment
Break
}
$Nics += [pscustomobject]@{
"PortGroup" = $PortGroup
"MacAddress" = $_.MacAddress
}
}
$VmHash.Add($Moref, [pscustomobject]@{
"MoRef" = $MoRef;
"Name" = $_.name ;
"Nics" = $Nics;
"IsManager" = $IsManager;
"IsEdge" = $IsEdge;
"IsLogicalRouter" = $IsLogicalRouter;
"IsController" = $IsController;
"ToolsIp" = $_.Guest.Ipaddress })
}
write-host " Getting IP and MAC details from Spoofguard"
Get-NsxSpoofguardPolicy -connection $connection | Get-NsxSpoofguardNic -connection $connection | % {
if ($MacHash.ContainsKey($_.detectedmacAddress)) {
write-warning "Duplicate MAC ($($_.detectedMacAddress) - $($_.nicname)) found. Skipping NIC!"
}
else {
$MacHash.Add($_.detectedmacaddress, $_)
}
}
write-host " Getting configured IP Set objects"
Get-NsxIpSet -connection $connection | % {
$IpSetHash.Add($_.objectid, $_.outerxml)
}
write-host " Getting configured Services"
Get-NsxService -connection $connection | % {
$ServiceHash.Add($_.objectid, $_.outerxml)
}
write-host " Getting configured Service groups"
Get-NsxServiceGroup -connection $connection | % {
$ServiceGroupHash.Add($_.objectid, $_.outerxml)
}
write-host " Getting configured Security groups"
Get-NsxSecurityGroup -connection $connection | % {
$SecurityGroupHash.Add($_.objectid, $_.outerxml)
}
write-host " Getting configured Security tags"
Get-NsxSecurityTag -connection $connection | % {
$SecurityTagHash.Add($_.objectid, $_.outerxml)
}
write-host " Getting configured DFW Rules"
$uri="/api/4.0/firewall/globalroot-0/config"
$response = invoke-nsxwebrequest -URI $uri -Method GET -connection $connection
$response.content | format-xml | Out-File $dfwConfigFile
write-host " Getting Security Policies"
Get-NsxSecurityPolicy -connection $connection | % {
$SecPolHash.Add($_.objectId, $_.outerxml)
}
write-host -ForeGroundColor Green "`nCreating Object Export Bundle"
#Export files
$LsHash | export-clixml -depth $maxdepth $LsExportFile
$VdPortGroupHash | export-clixml -depth $maxdepth $VdPgExportFile
$StdPgHash | export-clixml -depth $maxdepth $StdPgExportFile
$LrHash | export-clixml -depth $maxdepth $LrExportFile
$EdgeHash | export-clixml -depth $maxdepth $EdgeExportFile
$VmHash | export-clixml -depth $maxdepth $VmExportFile
$CtrlHash | export-clixml -depth $maxdepth $CtrlExportFile
$MacHash | export-clixml -depth $maxdepth $MacAddressExportFile
$IpSethash | export-clixml -depth $maxdepth $IpSetExportFile
$ServiceHash | export-clixml -depth $maxdepth $ServicesExportFile
$ServiceGroupHash | export-clixml -depth $maxdepth $ServiceGroupExportFile
$SecurityGroupHash | export-clixml -depth $maxdepth $SecurityGroupExportFile
$SecurityTagHash | export-clixml -depth $maxdepth $SecurityTagExportFile
$SecPolHash | export-clixml -depth $maxdepth $SecPolExportFile
#Desktop extract to zip
if ($psversiontable.PSEdition -ne "Core"){
Add-Type -assembly "system.io.compression.filesystem"
}
[system.io.compression.zipfile]::CreateFromDirectory($TempDir, $ExportFile)
#Clean up stale captures
$Captures = Get-ChildItem $ExportPath -filter 'NSX-ObjectCapture-*.zip'
while ( ( $Captures | measure ).count -ge $maxCaptures ) {
write-warning "Maximum number of captures reached. Removing oldest capture."
$captures | sort-object -property LastWriteTime | select-object -first 1 | remove-item -confirm:$false
$Captures = Get-ChildItem $ExportPath
}
write-host -ForeGroundColor Green "`nCapture Bundle created at $ExportFile"