-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExport-VCDB.ps1
178 lines (163 loc) · 5.36 KB
/
Export-VCDB.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
#
# Export-VCDB.ps1
#
# Version: 1.0
# Author: Arnim van Lieshout
#
# This script will export your vCenter database objects to several .xml files.
# These files can be used in combination with the Import-VCDB.ps1 script to recreate those objects into another vCenter database
#
# I've put every step in a seperate function, so you can easily select the steps you need in your environment
#
# Known Limitations:
# - Datacenter folders are not supported
# - Resourcepools are not supported
# - Objectnames used in vCenter must be unique, because the name is used as a unique reference
# - Cluster EVC mode is not supported
#
$src = Connect-VIServer "vCenterServerName" -NotDefault
$ClusterName = "Clustername"
#Export datacenters
function Export-Datacenters {
Get-Datacenter -Server $src | Select Name | Export-Clixml "Datacenters.xml"
}
#Export folder structure (no datacenter folders!)
function Export-Folders {
filter Get-FolderStructure {
$folder = "" | select Name,Children
$folder.Name = $_.name
$folder.Children = @($_ | Get-Folder -NoRecursion | Get-FolderStructure )
$folder
}
$FolderStructure=@{}
Get-Datacenter -Server $src | %{$FolderStructure[$_.name] = $_ | Get-Folder -NoRecursion | Get-FolderStructure}
$FolderStructure | Export-Clixml "Folders.xml"
}
#Export clusters
function Export-Clusters {
$Clusters = @()
ForEach ($dc in (Get-Datacenter -Server $src)) {
foreach ($c in ($dc | Get-Cluster -Server $src)) {
$c | Add-Member -MemberType Noteproperty -Name Datacenter -Value $dc.name
$Clusters += $c
}
}
$Clusters | Export-Clixml "Clusters.xml"
}
#Export custom attributes definitions
function Export-CustomAttributes {
Get-CustomAttribute -Server $src | Where-Object {$_.TargetType} | Export-Clixml "CustomAttributes.xml"
}
#Export customization profiles
function Export-CustomizationProfiles {
$profiles = @()
$view = Get-View -Server $src CustomizationSpecManager
ForEach ($cp in $view.info) {$profiles += $view.CustomizationSpecItemToXml($view.GetCustomizationSpec($cp.name))}
$profiles | Export-Clixml "CustomizationProfiles.xml"
}
#Export roles
function Export-Roles {
Get-VIRole -Server $src | Where-Object {-not $_.IsSystem} | Export-Clixml "Roles.xml"
}
#Export vm locations
function Export-VmLocations {
filter Get-Path {
param($child)
$folder = Get-View -Server $src $_.parent
if ($folder.gettype().name -eq "Datacenter") {
$child
} else {
$path = "" | select Name,Child
$path.name = $folder.name
$path.child = $child
$folder | Get-Path($path)
}
}
$VmLocations = @()
foreach ($vm in Get-VM -Server $src) {
$VmObj = "" | Select Name,Datacenter,Path
$VmObj.Name = $vm.name
$VmObj.Datacenter = $($vm | Get-Datacenter).name
$VmObj.Path = $vm | Get-View | Get-Path
$VmLocations += $VmObj
}
$VmLocations | Export-Clixml "VmLocations.xml"
}
#Export DRS rules
function Export-DrsRules {
$DrsRules=@()
ForEach ($c in (Get-Cluster $ClusterName -Server $src)) {
ForEach($r in ($c | Get-DrsRule -Server $src)) {
$RuleObj = "" | Select Cluster, Name, Enabled, KeepTogether, VMs
$RuleObj.Cluster = $c.name
$RuleObj.Name = $r.name
$RuleObj.Enabled = $r.enabled
$RuleObj.KeepTogether = $r.KeepTogether
$RuleObj.vms = @($r.VMIds | % {(Get-VM -Server $src -Id $_).Name})
$DrsRules += $RuleObj
}
}
$DrsRules | Export-Clixml "DrsRules.xml"
}
#Export permissions
function Export-Permissions {
$Permissions=@()
foreach ($p in Get-VIPermission -Server $src) {
$PermObj = "" | Select EntityName, EntityType, Principal, Propagate, Group, Role
$PermObj.Principal = $p.Principal
$PermObj.Propagate = $p.Propagate
$PermObj.Group = $p.IsGroup
$PermObj.Role = $p.Role
$pv = Get-view -Server $src -Id $p.EntityId
$PermObj.EntityName = $pv.Name
$PermObj.EntityType = $pv.GetType().Name
$Permissions += $PermObj
}
$Permissions | Export-Clixml "Permissions.xml"
}
#Export vmhosts
function Export-VMHosts {
filter Get-Path {
param($child)
$folder = Get-View -Server $src $_.parent
if ($folder.gettype().name -eq "Datacenter") {
$child
} else {
$path = "" | select Name,Child
$path.name = $folder.name
$path.child = $child
$folder | Get-Path($path)
}
}
$VMHosts = @()
foreach ($h in (Get-Cluster $ClusterName -Server $src | Get-VMHost)) {
$HostObj = "" | Select Name,Datacenter,Cluster,Path
$HostObj.Name = $h.name
$HostObj.Datacenter = $(Get-Datacenter -VMHost $h).name
$HostObj.Cluster = $(Get-Cluster -VMHost $h).name
if (-not $HostObj.Cluster) {$HostObj.Path = Get-view -Server $src $($h | Get-View).parent | Get-Path}
$VMHosts += $HostObj
}
$VMHosts | Export-Clixml "VMHosts.xml"
}
#Disconnect hosts
function Disconnect-Hosts {
#Disconnect ESX hosts
Get-Cluster $ClusterName -Server $src | Get-VMHost | Set-VMHost -State "Disconnected" -Confirm:$false
}
#Main
#Export-Datacenters
#Export-Folders
#Export-Clusters
#Export-CustomAttributes
#Export-CustomizationProfiles
#Export-Roles
Export-VmLocations
Export-DrsRules
#Export-Permissions
Export-VMHosts
#[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#$answer = [System.Windows.Forms.MessageBox]::show("You are about to disconnect your ESX(i) host(s) from your vCenter server`nAre you sure you want to disconnect your hosts?","Disconnect ESX(i) hosts", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Exclamation)
#if($answer -eq [Windows.Forms.DialogResult]::Yes){
# Disconnect-Hosts
# }