Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .LABEL
- Perform a xVCVmotion of a virtual machine
- .DESCRIPTION
- VMotions machines between non sso VC Servers
- #>
- param
- (
- [Parameter(Mandatory=$true)]
- #[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]
- [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]
- $vParam
- );
- Function xMove-VM {
- param(
- [Parameter(
- Position=0,
- Mandatory=$true,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)
- ]
- [VMware.VimAutomation.ViCore.Util10.VersionedObjectImpl]$sourcevc,
- [VMware.VimAutomation.ViCore.Util10.VersionedObjectImpl]$destvc,
- [String]$vm,
- [Int]$xvctype
- )
- # Retrieve Source VC SSL Thumbprint
- $vcurl = "https://" + $destVC
- add-type @"
- using System.Net;
- using System.Security.Cryptography.X509Certificates;
- public class IDontCarePolicy : ICertificatePolicy {
- public IDontCarePolicy() {}
- public bool CheckValidationResult(
- ServicePoint sPoint, X509Certificate cert,
- WebRequest wRequest, int certProb) {
- return true;
- }
- }
- "@
- [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy
- # Need to do simple GET connection for this method to work
- Invoke-RestMethod -Uri $VCURL -Method Get | Out-Null
- $endpoint_request = [System.Net.Webrequest]::Create("$vcurl")
- # Get Thumbprint + add colons for a valid Thumbprint
- $destVCThumbprint = ($endpoint_request.ServicePoint.Certificate.GetCertHashString()) -replace '(..(?!$))','$1:'
- # Source VM to migrate
- $vm_view = Get-View (Get-VM -Server $sourcevc -Name $vm) -Property Config.Hardware.Device
- $datastore = Get-Datastore -server $sourceVCConn -VM $vm
- echo Source Datastore: $datastore.Name $datastore_view.Id
- $datastore_view = Get-Datastore -server $destVCConn -Name $datastore.name
- echo Destination Datastore: $datastore_view.Name $datastore_view.Id
- $cluster = Get-Cluster -Server $sourceVCConn -VM $vm
- $cluster_view = Get-Cluster -Server $destVCConn -Name $cluster.name
- echo "Cluster: $cluster_view"
- $tagStore = Get-TagAssignment -Server $sourceVCConn $vm
- # Dest ESXi host to migrate VM to
- $vmhost_view = (Get-VMHost -Server $destVCConn -name "ps008esxpr1512.cs.athabascau.ca")
- # Find all Etherenet Devices for given VM which
- # we will need to change its network at the destination
- $vmNetworkAdapters = @()
- $devices = $vm_view.Config.Hardware.Device
- foreach ($device in $devices) {
- if($device -is [VMware.Vim.VirtualEthernetCard]) {
- $vmNetworkAdapters += $device
- echo "Found a NIC"
- }
- }
- # Relocate Spec for Migration
- $spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
- $spec.datastore = $datastore_view.Id
- $spec.host = $vmhost_view.Id
- $spec.pool = $cluster_view.ExtensionData.ResourcePool
- # Relocate Spec Disk Locator
- if($xvctype -eq 1){
- $HDs = Get-VM -Server $sourcevc -Name $vm | Get-HardDisk
- $HDs | %{
- $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
- $disk.diskId = $_.Extensiondata.Key
- $SourceDS = $_.FileName.Split("]")[0].TrimStart("[")
- $DestDS = Get-Datastore -Server $destvc -name $sourceDS
- $disk.Datastore = $DestDS.ID
- $spec.disk += $disk
- }
- }
- # Service Locator for the destination vCenter Server
- # regardless if its within same SSO Domain or not
- $service = New-Object VMware.Vim.ServiceLocator
- $credential = New-Object VMware.Vim.ServiceLocatorNamePassword
- $credential.username = $destVCusername
- $credential.password = $destVCpassword
- $service.credential = $credential
- $service.instanceUuid = $destVCConn.InstanceUuid
- $service.sslThumbprint = $destVCThumbprint
- $service.url = "https://$destVC"
- $spec.service = $service
- # Create VM spec depending if destination networking
- # is using Distributed Virtual Switch (VDS) or
- # is using Virtual Standard Switch (VSS)
- $count = 0
- echo "Switch: $switchtype"
- $vmnetworkname = ""
- $networks = get-vm -server $sourceVCConn -name $vm | Select Name, @{N="Network"; e={ $_ | get-networkadapter|Select Name, NetworkName} }
- $vmnetworks = ""
- foreach ($network in $networks.Network) {
- if ($vmnetworks -ne "") {
- echo "Found Additional Network"
- $vmnetworks = $vmnetworks +", " + $network.NetworkName
- } else {
- echo "Found First Network"
- $vmnetworks = $network.NetworkName
- }
- }
- echo "Collected network names are: $vmnetworks"
- $vmnetworkname = ($vmnetworks -split ", ")
- if($switchtype -eq "vds") {
- foreach ($vmNetworkAdapter in $vmNetworkAdapters) {
- $vmNetName = $vmnetworkname[$count]
- echo "Associating: " + $vmNetName
- # New VM Network to assign vNIC
- # Extract Distributed Portgroup required info
- $dvpg = Get-VirtualPortGroup -Server $destvc -Name $vmNetName
- $vds_uuid = (Get-View $dvpg.ExtensionData.Config.DistributedVirtualSwitch).Uuid
- $dvpg_key = $dvpg.ExtensionData.Config.key
- # Device Change spec for VSS portgroup
- $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
- $dev.Operation = "edit"
- $dev.Device = $vmNetworkAdapter
- $dev.device.Backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
- $dev.device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
- $dev.device.backing.port.switchUuid = $vds_uuid
- $dev.device.backing.port.portgroupKey = $dvpg_key
- $spec.DeviceChange += $dev
- $count++
- }
- } else {
- foreach ($vmNetworkAdapter in $vmNetworkAdapters) {
- # New VM Network to assign vNIC
- $vmnetworkname = ($vmnetworks -split ",")[$count]
- # Device Change spec for VSS portgroup
- $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
- $dev.Operation = "edit"
- $dev.Device = $vmNetworkAdapter
- $dev.device.backing = New-Object VMware.Vim.VirtualEthernetCardNetworkBackingInfo
- $dev.device.backing.deviceName = $vmnetworkname
- $spec.DeviceChange += $dev
- $count++
- }
- }
- Write-Host "`nMigrating $vmname from $sourceVC to $destVC ...`n"
- # Issue Cross VC-vMotion
- $task = $vm_view.RelocateVM_Task($spec,"defaultPriority")
- $task1 = Get-Task -Id ("Task-$($task.value)")
- $task1 | Wait-Task -Verbose
- $destVm = get-vm $vm
- echo "Associating Tags to guest"
- foreach ($tag in $tagStore){
- echo "Assigning tag:" $tag.Tag.Name
- $dTag = get-tag -server $destVCConn $tag.Tag.Name
- New-TagAssignment -Tag $dTag -Entity $destVM
- }
- }
- # Variables that must be defined
- $sourceVC = "192.168.35.10"
- $sourceVCUsername = "administrator@vsphere.local"
- $sourceVCPassword = "password"
- $destVC = "prd-vcsa-01.cs.athabascau.ca"
- $destVCUsername = "administrator@vsphere.local"
- $destVCpassword = "passwprd!"
- $switchtype = "vds"
- $ComputeXVC = 1
- $title = "xVCVmotion"
- $message = "Are you sure you want to VMotion this virtual machine to " + $destVC
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
- "Migrates the VM to another VCenter."
- $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
- "Do Nothing."
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {
- # Connect to Source/Destination vCenter Server
- $sourceVCConn = Connect-VIServer -Server $sourceVC -user $sourceVCUsername -password $sourceVCPassword
- $destVCConn = Connect-VIServer -Server $destVC -user $destVCUsername -password $destVCpassword
- foreach ($vm in $vParam) {
- write-host "Migrating $vm"
- xMove-VM -sourcevc $sourceVCConn -destvc $destVCConn -VM $vm -xvcType $computeXVC
- }
- # Disconnect from Source/Destination VC
- Disconnect-VIServer -Server $sourceVCConn -Confirm:$false
- Disconnect-VIServer -Server $destVCConn -Confirm:$false
- write-host "Migration Complete"
- }
- 1 {
- Write-Host "Aborting Migration"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement