JakeBentz

Export-VC-Migration.ps1

Jan 19th, 2018
249
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Export-VCDB.ps1
  3. #
  4. # Version:  1.0
  5. # Auhtor:   Arnim van Lieshout
  6. #
  7. # This script will export your vCenter database objects to several .xml files.
  8. # These files can be used in combination with the Import-VCDB.ps1 script to recreate those objects into another vCenter database
  9. #
  10. # I've put every step in a seperate function, so you can easily select the steps you need in your environment
  11. #
  12. # Known Limitations:
  13. # - Datacenter folders are not supported
  14. # - Resourcepools are not supported
  15. # - Objectnames used in vCenter must be unique, because the name is used as a unique reference
  16. # - Cluster EVC mode is not supported
  17. #
  18.  
  19. $src = Connect-VIServer "vCenterServerName" -NotDefault
  20. $ClusterName = "Clustername"
  21.  
  22. #Export datacenters
  23. function Export-Datacenters {
  24.     Get-Datacenter -Server $src | Select Name | Export-Clixml "Datacenters.xml"
  25. }
  26.  
  27. #Export folder structure (no datacenter folders!)
  28. function Export-Folders {
  29.     filter Get-FolderStructure {
  30.         $folder = "" | select Name,Children
  31.         $folder.Name = $_.name
  32.         $folder.Children = @($_ | Get-Folder -NoRecursion | Get-FolderStructure )
  33.         $folder
  34.     }
  35.     $FolderStructure=@{}
  36.     Get-Datacenter -Server $src | %{$FolderStructure[$_.name] = $_ | Get-Folder -NoRecursion | Get-FolderStructure}
  37.     $FolderStructure | Export-Clixml "Folders.xml"
  38. }
  39.  
  40. #Export clusters
  41. function Export-Clusters {
  42.     $Clusters = @()
  43.     ForEach ($dc in (Get-Datacenter -Server $src)) {
  44.         foreach ($c in ($dc | Get-Cluster -Server $src)) {
  45.             $c | Add-Member -MemberType Noteproperty -Name Datacenter -Value $dc.name
  46.             $Clusters += $c
  47.         }
  48.     }
  49.     $Clusters | Export-Clixml "Clusters.xml"
  50. }
  51.  
  52. #Export custom attributes definitions
  53. function Export-CustomAttributes {
  54.     Get-CustomAttribute -Server $src | Where-Object {$_.TargetType} | Export-Clixml "CustomAttributes.xml"
  55. }
  56.  
  57. #Export customization profiles
  58. function Export-CustomizationProfiles {
  59.     $profiles = @()
  60.     $view = Get-View -Server $src CustomizationSpecManager
  61.     ForEach ($cp in $view.info) {$profiles += $view.CustomizationSpecItemToXml($view.GetCustomizationSpec($cp.name))}
  62.     $profiles | Export-Clixml "CustomizationProfiles.xml"
  63. }
  64.  
  65. #Export roles
  66. function Export-Roles {
  67.     Get-VIRole -Server $src | Where-Object {-not $_.IsSystem} | Export-Clixml "Roles.xml"
  68. }
  69.  
  70. #Export vm locations
  71. function Export-VmLocations {
  72.     filter Get-Path {
  73.         param($child)
  74.         $folder = Get-View -Server $src $_.parent
  75.    
  76.         if ($folder.gettype().name -eq "Datacenter") {
  77.             $child
  78.         } else {
  79.             $path = "" | select Name,Child
  80.             $path.name = $folder.name
  81.             $path.child = $child
  82.             $folder | Get-Path($path)
  83.         }
  84.     }
  85.     $VmLocations = @()
  86.     foreach ($vm in Get-VM -Server $src) {
  87.         $VmObj = "" | Select Name,Datacenter,Path
  88.         $VmObj.Name = $vm.name
  89.         $VmObj.Datacenter = $($vm | Get-Datacenter).name
  90.         $VmObj.Path = $vm | Get-View | Get-Path
  91.         $VmLocations += $VmObj
  92.     }
  93.     $VmLocations | Export-Clixml "VmLocations.xml"
  94. }
  95.  
  96. #Export DRS rules
  97. function Export-DrsRules {
  98.     $DrsRules=@()
  99.     ForEach ($c in (Get-Cluster $ClusterName -Server $src)) {
  100.         ForEach($r in ($c | Get-DrsRule -Server $src)) {
  101.             $RuleObj = "" | Select Cluster, Name, Enabled, KeepTogether, VMs
  102.             $RuleObj.Cluster = $c.name
  103.             $RuleObj.Name = $r.name
  104.             $RuleObj.Enabled = $r.enabled
  105.             $RuleObj.KeepTogether = $r.KeepTogether
  106.             $RuleObj.vms = @($r.VMIds | % {(Get-VM -Server $src -Id $_).Name})
  107.             $DrsRules += $RuleObj
  108.         }
  109.     }
  110.     $DrsRules | Export-Clixml "DrsRules.xml"
  111. }
  112.  
  113. #Export permissions
  114. function Export-Permissions {
  115.     $Permissions=@()
  116.     foreach ($p in Get-VIPermission -Server $src) {
  117.         $PermObj = "" | Select EntityName, EntityType, Principal, Propagate, Group, Role
  118.         $PermObj.Principal = $p.Principal
  119.         $PermObj.Propagate = $p.Propagate
  120.         $PermObj.Group = $p.IsGroup
  121.         $PermObj.Role = $p.Role
  122.         $pv = Get-view -Server $src -Id $p.EntityId
  123.         $PermObj.EntityName = $pv.Name
  124.         $PermObj.EntityType = $pv.GetType().Name
  125.         $Permissions += $PermObj
  126.     }
  127.     $Permissions | Export-Clixml "Permissions.xml"
  128. }
  129.  
  130. #Export vmhosts
  131. function Export-VMHosts {
  132.     filter Get-Path {
  133.         param($child)
  134.         $folder = Get-View -Server $src $_.parent
  135.    
  136.         if ($folder.gettype().name -eq "Datacenter") {
  137.             $child
  138.         } else {
  139.             $path = "" | select Name,Child
  140.             $path.name = $folder.name
  141.             $path.child = $child
  142.             $folder | Get-Path($path)
  143.         }
  144.     }
  145.     $VMHosts = @()
  146.     foreach ($h in (Get-Cluster $ClusterName -Server $src | Get-VMHost)) {
  147.         $HostObj = "" | Select Name,Datacenter,Cluster,Path
  148.         $HostObj.Name = $h.name
  149.         $HostObj.Datacenter = $(Get-Datacenter -VMHost $h).name
  150.         $HostObj.Cluster = $(Get-Cluster -VMHost $h).name
  151.         if (-not $HostObj.Cluster) {$HostObj.Path = Get-view -Server $src $($h | Get-View).parent | Get-Path}
  152.         $VMHosts += $HostObj
  153.     }
  154.     $VMHosts | Export-Clixml "VMHosts.xml"
  155. }
  156.  
  157. #Disconnect hosts
  158. function Disconnect-Hosts {
  159.     #Disconnect ESX hosts
  160.     Get-Cluster $ClusterName -Server $src | Get-VMHost | Set-VMHost -State "Disconnected" -Confirm:$false
  161. }
  162.  
  163. #Main
  164. #Export-Datacenters
  165. #Export-Folders
  166. #Export-Clusters
  167. #Export-CustomAttributes
  168. #Export-CustomizationProfiles
  169. #Export-Roles
  170. Export-VmLocations
  171. Export-DrsRules
  172. #Export-Permissions
  173. Export-VMHosts
  174. #[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  175. #$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)
  176. #if($answer -eq [Windows.Forms.DialogResult]::Yes){
  177. #   Disconnect-Hosts
  178. #   }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment