JakeBentz

Import-VC-Migration.ps1

Jan 19th, 2018
240
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Import-VCDB.ps1
  3. #
  4. # Version:  1.0
  5. # Auhtor:   Arnim van Lieshout
  6. #
  7. # This script will import your vCenter database objects from .xml files created with Export-VCDB.ps1
  8. #
  9. # I've put every step in a seperate function, so you can easily select the steps you need in your environment
  10. #
  11.  
  12. $dst = Connect-VIServer "vCenterServername" -NotDefault
  13.  
  14. #Import datacenters
  15. function Import-Datacenters {
  16.     $rootfolder = Get-Folder -Server $dst -NoRecursion
  17.     Import-Clixml "Datacenters.xml" | %{New-Datacenter -Location $rootfolder -Name $_.Name}
  18. }
  19.  
  20. #Import folder structure (no datacenter folders!)
  21. function Import-Folders {
  22.     $FolderStructure = Import-Clixml "Folders.xml"
  23.     filter New-FolderStructure {
  24.         param($parent)
  25.         if (-not($folder = Get-Folder $_.name -Location $parent -ErrorAction:SilentlyContinue)) {$folder = New-Folder $_.name -Location $parent}
  26.         $_.children | New-FolderStructure($folder)
  27.     }
  28.     $FolderStructure.GetEnumerator() | %{$dc = get-datacenter $_.name -server $dst; $_.value | New-FolderStructure($dc)}
  29. }
  30.  
  31. #Import clusters
  32. function Import-Clusters {
  33.     ForEach ($c in (Import-Clixml "Clusters.xml")) {
  34.         $nc = New-Cluster -Location (Get-Datacenter $c.datacenter -Server $dst) -Name $c.name
  35.         Set-Cluster -Cluster $nc -VMSwapfilePolicy $c.VMSwapfilePolicy -DrsEnabled:$true -DrsAutomationLevel $c.DrsAutomationLevel -DrsMode $c.DrsMode -HAAdmissionControlEnabled $c.HAAdmissionControlEnabled -HAFailoverLevel $c.HAFailoverLevel -HAIsolationResponse $c.HAIsolationResponse -HARestartPriority $c.HARestartPriority -Confirm:$false
  36.         Set-Cluster -Cluster $nc -DrsEnabled $c.DrsEnabled -HAEnabled $c.HAEnabled -Confirm:$false
  37.     }
  38. }
  39.  
  40. #Import custom attributes definitions
  41. function Import-CustomAttributes {
  42.     Import-Clixml "CustomAttributes.xml" | % {New-CustomAttribute -Server $dst -Name $_.name -TargetType $_.TargetType}
  43. }
  44.  
  45. #Import customization profiles
  46. function Import-CustomizationProfiles {
  47.     $view = Get-View -Server $dst CustomizationSpecManager
  48.     Import-Clixml "CustomizationProfiles.xml" | %{$view.CreateCustomizationSpec($view.XmlToCustomizationSpecItem($_))}
  49. }
  50.  
  51. #Import roles
  52. function Import-Roles {
  53.     #First remove existing roles on destination server
  54.     Get-VIRole -Server $dst | Where-Object {-not $_.IsSystem} | Remove-VIRole -Confirm:$false
  55.     foreach ($role in (Import-Clixml "Roles.xml")) {
  56.         $privileges = Get-VIPrivilege -PrivilegeItem -Server $dst
  57.         New-VIRole -Server $dst -Name $role.Name -Privilege ($privileges | ? {$role.PrivilegeList -contains $_.id})
  58.     }
  59. }
  60.  
  61. #Import vmhosts
  62. function Import-VMHosts {
  63.     #First get credentials for adding hosts
  64.     $HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials", "root", "")
  65.     filter Get-HostLocation {
  66.         param($parent)
  67.         if ($_.child) {
  68.             $_.child | Get-HostLocation(Get-Folder -Server $dst -Location $parent $_.Name)
  69.         } else {
  70.             Get-Folder -Server $dst -Location $parent $_.Name
  71.         }
  72.     }
  73.     foreach ($vmhost in (Import-Clixml "VMHosts.xml")) {
  74.         if ($vmhost.Cluster) {
  75.             Add-VMHost $vmhost.Name -Location (Get-Cluster $vmhost.Cluster -Location (Get-Datacenter $vmhost.Datacenter -Server $dst)) -Credential $HostCred -Force
  76.             #Add-VMHost $vmhost.Name -Location (Get-Cluster $vmhost.Cluster -Location (Get-Datacenter $vmhost.Datacenter -Server $dst)) -Credential $HostCred -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null
  77.         } else {
  78.             Add-VMHost $vmhost.Name -Location ($vmhost.Path | Get-HostLocation(Get-Datacenter $vmhost.Datacenter -Server $dst)) -Credential $HostCred -Force
  79.             #Add-VMHost $vmhost.Name -Location ($vmhost.Path | Get-HostLocation(Get-Datacenter $vmhost.Datacenter -Server $dst)) -Credential $HostCred -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null
  80.         }
  81.     }
  82. }
  83.  
  84. #Import vm locations (Move vm's to original location)
  85. function Import-VmLocations {
  86.     filter Get-VmLocation {
  87.         param($parent)
  88.         if ($_.child) {
  89.             $_.child | Get-VmLocation(Get-Folder -Server $dst -Location $parent $_.Name)
  90.         } else {
  91.             Get-Folder -Server $dst -Location $parent $_.Name
  92.         }
  93.     }
  94.     foreach ($vm in (Import-Clixml "VmLocations.xml")) {
  95.         $dc = Get-Datacenter -Server $dst $vm.datacenter
  96.         Move-VM ($dc | Get-VM $vm.Name) -Destination ($vm.path | Get-VmLocation($dc))
  97.     }
  98. }
  99.  
  100. #Import DRS rules
  101. function Import-DrsRules {
  102.     ForEach ($r in (Import-CliXml "DrsRules.xml")) {
  103.         New-DrsRule -Server $dst -Cluster (Get-Cluster -Server $dst -Name $r.Cluster) -Name $r.Name -Enabled $r.Enabled -KeepTogether $r.KeepTogether -VM (Get-VM -Server $dst $r.VMs)
  104.     }
  105. }
  106.  
  107. #Import permissions
  108. function Import-Permissions {
  109.     #Because we need the name of the entity in a regular expression (Get-view filters are regular expressions!!)
  110.     #we have to escape all metacharacters
  111.     filter Escape-MetaCharacters {
  112.         foreach($MetaChar in '^','$','{','}','[',']','(',')','.','*','+','?','|','<','>','-','&') {$_=$_.replace($MetaChar,"\$($Metachar)")}
  113.         $_
  114.     }
  115.     foreach ($p in (Import-Clixml "Permissions.xml")) {
  116.         $ev = Get-View -ViewType $p.EntityType -Filter @{"Name" = $($p.EntityName | Escape-MetaCharacters)} -Server $dst
  117.         $perm = New-Object VMware.Vim.Permission
  118.         $perm.principal = $p.Principal
  119.         $perm.group = $p.Group
  120.         $perm.propagate = $p.Propagate
  121.         $perm.roleId = $(Get-VIRole $p.Role -Server $dst).id
  122.         $authMgr = Get-View AuthorizationManager -Server $dst
  123.         $authMgr.SetEntityPermissions($ev.MoRef, $perm)
  124.     }
  125. }
  126.  
  127. #Main
  128. #Import-Datacenters
  129. #Import-Folders
  130. #Import-Clusters
  131. #Import-CustomAttributes
  132. #Import-CustomizationProfiles
  133. #Import-Roles
  134. #Stop code execution to give the opportunity to enable EVC mode on clusters if needed before adding ESX(i) hosts
  135. #[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  136. #$answer = [System.Windows.Forms.MessageBox]::show("Enable EVC mode on your clusters if needed Now!`nWhen finished, press OK to continue adding hosts","Enable EVC Mode", [System.Windows.Forms.MessageBoxButtons]::Ok, [System.Windows.Forms.MessageBoxIcon]::Asterisk)
  137. Import-VMHosts
  138. Import-DrsRules
  139. #Import-Permissions
  140. Import-VmLocations
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment