Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .LABEL
  3. Perform a xVCVmotion of a virtual machine
  4. .DESCRIPTION
  5. VMotions machines between non sso VC Servers
  6. #>
  7.  
  8. param
  9. (
  10.  [Parameter(Mandatory=$true)]
  11.  #[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]
  12.  [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]
  13.  $vParam
  14. );
  15.  
  16. Function xMove-VM {
  17.     param(
  18.     [Parameter(
  19.         Position=0,
  20.         Mandatory=$true,
  21.         ValueFromPipeline=$true,
  22.         ValueFromPipelineByPropertyName=$true)
  23.     ]
  24.     [VMware.VimAutomation.ViCore.Util10.VersionedObjectImpl]$sourcevc,
  25.     [VMware.VimAutomation.ViCore.Util10.VersionedObjectImpl]$destvc,
  26.     [String]$vm,
  27.     [Int]$xvctype
  28.     )
  29.  
  30.     # Retrieve Source VC SSL Thumbprint
  31.     $vcurl = "https://" + $destVC
  32. add-type @"
  33.        using System.Net;
  34.        using System.Security.Cryptography.X509Certificates;
  35.            public class IDontCarePolicy : ICertificatePolicy {
  36.            public IDontCarePolicy() {}
  37.            public bool CheckValidationResult(
  38.                ServicePoint sPoint, X509Certificate cert,
  39.                WebRequest wRequest, int certProb) {
  40.                return true;
  41.            }
  42.        }
  43. "@
  44.    
  45.     [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy
  46.     # Need to do simple GET connection for this method to work
  47.     Invoke-RestMethod -Uri $VCURL -Method Get | Out-Null
  48.  
  49.     $endpoint_request = [System.Net.Webrequest]::Create("$vcurl")
  50.     # Get Thumbprint + add colons for a valid Thumbprint
  51.     $destVCThumbprint = ($endpoint_request.ServicePoint.Certificate.GetCertHashString()) -replace '(..(?!$))','$1:'
  52.  
  53.     # Source VM to migrate
  54.     $vm_view = Get-View (Get-VM -Server $sourcevc -Name $vm) -Property Config.Hardware.Device
  55.  
  56.     $datastore = Get-Datastore -server $sourceVCConn -VM $vm
  57.     echo Source Datastore: $datastore.Name $datastore_view.Id
  58.     $datastore_view = Get-Datastore -server $destVCConn -Name $datastore.name
  59.     echo Destination Datastore: $datastore_view.Name $datastore_view.Id
  60.     $cluster = Get-Cluster -Server $sourceVCConn -VM $vm
  61.     $cluster_view = Get-Cluster -Server $destVCConn -Name $cluster.name
  62.     echo "Cluster: $cluster_view"
  63.     $tagStore = Get-TagAssignment -Server $sourceVCConn $vm
  64.     # Dest ESXi host to migrate VM to
  65.     $vmhost_view = (Get-VMHost -Server $destVCConn -name "ps008esxpr1512.cs.athabascau.ca")
  66.  
  67.     # Find all Etherenet Devices for given VM which
  68.     # we will need to change its network at the destination
  69.     $vmNetworkAdapters = @()
  70.     $devices = $vm_view.Config.Hardware.Device
  71.     foreach ($device in $devices) {
  72.         if($device -is [VMware.Vim.VirtualEthernetCard]) {
  73.             $vmNetworkAdapters += $device
  74.             echo "Found a NIC"
  75.         }
  76.     }
  77.  
  78.     # Relocate Spec for Migration
  79.     $spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
  80.     $spec.datastore = $datastore_view.Id
  81.     $spec.host = $vmhost_view.Id
  82.     $spec.pool = $cluster_view.ExtensionData.ResourcePool
  83.  
  84.     # Relocate Spec Disk Locator
  85.     if($xvctype -eq 1){
  86.         $HDs = Get-VM -Server $sourcevc -Name $vm | Get-HardDisk
  87.         $HDs | %{
  88.             $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
  89.             $disk.diskId = $_.Extensiondata.Key
  90.             $SourceDS = $_.FileName.Split("]")[0].TrimStart("[")
  91.             $DestDS = Get-Datastore -Server $destvc -name $sourceDS
  92.             $disk.Datastore = $DestDS.ID
  93.             $spec.disk += $disk
  94.         }
  95.     }
  96.  
  97.     # Service Locator for the destination vCenter Server
  98.     # regardless if its within same SSO Domain or not
  99.     $service = New-Object VMware.Vim.ServiceLocator
  100.     $credential = New-Object VMware.Vim.ServiceLocatorNamePassword
  101.     $credential.username = $destVCusername
  102.     $credential.password = $destVCpassword
  103.     $service.credential = $credential
  104.     $service.instanceUuid = $destVCConn.InstanceUuid
  105.     $service.sslThumbprint = $destVCThumbprint
  106.     $service.url = "https://$destVC"
  107.     $spec.service = $service
  108.  
  109.     # Create VM spec depending if destination networking
  110.     # is using Distributed Virtual Switch (VDS) or
  111.     # is using Virtual Standard Switch (VSS)
  112.     $count = 0
  113.     echo "Switch: $switchtype"
  114.     $vmnetworkname = ""
  115.     $networks = get-vm -server $sourceVCConn -name $vm | Select Name, @{N="Network"; e={ $_ | get-networkadapter|Select Name, NetworkName} }
  116.     $vmnetworks = ""
  117.     foreach ($network in $networks.Network) {
  118.         if ($vmnetworks -ne "") {
  119.             echo "Found Additional Network"
  120.             $vmnetworks = $vmnetworks +", " + $network.NetworkName
  121.         } else {
  122.             echo "Found First Network"
  123.             $vmnetworks = $network.NetworkName
  124.         }
  125.     }
  126.     echo "Collected network names are: $vmnetworks"
  127.     $vmnetworkname = ($vmnetworks -split ", ")
  128.  
  129.     if($switchtype -eq "vds") {
  130.         foreach ($vmNetworkAdapter in $vmNetworkAdapters) {
  131.             $vmNetName = $vmnetworkname[$count]
  132.             echo "Associating: " + $vmNetName
  133.             # New VM Network to assign vNIC
  134.             # Extract Distributed Portgroup required info
  135.             $dvpg = Get-VirtualPortGroup -Server $destvc -Name $vmNetName
  136.             $vds_uuid = (Get-View $dvpg.ExtensionData.Config.DistributedVirtualSwitch).Uuid
  137.             $dvpg_key = $dvpg.ExtensionData.Config.key
  138.  
  139.             # Device Change spec for VSS portgroup
  140.             $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
  141.             $dev.Operation = "edit"
  142.             $dev.Device = $vmNetworkAdapter
  143.             $dev.device.Backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
  144.             $dev.device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
  145.             $dev.device.backing.port.switchUuid = $vds_uuid
  146.             $dev.device.backing.port.portgroupKey = $dvpg_key
  147.             $spec.DeviceChange += $dev
  148.             $count++
  149.         }
  150.     } else {
  151.         foreach ($vmNetworkAdapter in $vmNetworkAdapters) {
  152.             # New VM Network to assign vNIC
  153.             $vmnetworkname = ($vmnetworks -split ",")[$count]
  154.  
  155.             # Device Change spec for VSS portgroup
  156.             $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
  157.             $dev.Operation = "edit"
  158.             $dev.Device = $vmNetworkAdapter
  159.             $dev.device.backing = New-Object VMware.Vim.VirtualEthernetCardNetworkBackingInfo
  160.             $dev.device.backing.deviceName = $vmnetworkname
  161.             $spec.DeviceChange += $dev
  162.             $count++
  163.         }
  164.     }
  165.  
  166.     Write-Host "`nMigrating $vmname from $sourceVC to $destVC ...`n"
  167.  
  168.     # Issue Cross VC-vMotion
  169.     $task = $vm_view.RelocateVM_Task($spec,"defaultPriority")
  170.     $task1 = Get-Task -Id ("Task-$($task.value)")
  171.     $task1 | Wait-Task -Verbose
  172.     $destVm = get-vm $vm
  173.     echo "Associating Tags to guest"
  174.     foreach ($tag in $tagStore){
  175.         echo "Assigning tag:" $tag.Tag.Name
  176.         $dTag = get-tag -server $destVCConn $tag.Tag.Name
  177.         New-TagAssignment -Tag $dTag -Entity $destVM
  178.     }
  179.  }
  180.      
  181.  
  182. # Variables that must be defined
  183.  
  184. $sourceVC = "192.168.35.10"
  185. $sourceVCUsername = "administrator@vsphere.local"
  186. $sourceVCPassword = "password"
  187. $destVC = "prd-vcsa-01.cs.athabascau.ca"
  188. $destVCUsername = "administrator@vsphere.local"
  189. $destVCpassword = "passwprd!"
  190. $switchtype = "vds"
  191. $ComputeXVC = 1
  192.  
  193. $title = "xVCVmotion"
  194. $message = "Are you sure you want to VMotion this virtual machine to " + $destVC
  195.  
  196. $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
  197.     "Migrates the VM to another VCenter."
  198.  
  199. $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
  200.     "Do Nothing."
  201.  
  202. $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  203.  
  204. $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  205.  
  206. switch ($result)
  207.     {
  208.         0 {
  209.            
  210.             # Connect to Source/Destination vCenter Server
  211.             $sourceVCConn = Connect-VIServer -Server $sourceVC -user $sourceVCUsername -password $sourceVCPassword
  212.             $destVCConn = Connect-VIServer -Server $destVC -user $destVCUsername -password $destVCpassword
  213.            
  214.             foreach ($vm in $vParam) {
  215.                 write-host "Migrating $vm"
  216.                 xMove-VM -sourcevc $sourceVCConn -destvc $destVCConn -VM $vm -xvcType $computeXVC
  217.             }
  218.  
  219.             # Disconnect from Source/Destination VC
  220.             Disconnect-VIServer -Server $sourceVCConn -Confirm:$false
  221.             Disconnect-VIServer -Server $destVCConn -Confirm:$false
  222.            
  223.             write-host "Migration Complete"
  224.         }
  225.         1 {
  226.             Write-Host "Aborting Migration"
  227.         }
  228.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement