Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Name: Cross-SSO_vMotion-between-vCenters.ps1
- # Description: PowerCLI script to vMotion a running VM between two vCenters which are
- # not in the same SSO domain.
- # The script will use the GetPortGroupObject() function to determine the
- # port group type (standard or distributed) and return the correct object.
- # Reference: http://cloudmaniac.net/using-powercli-to-vmotion-vm-between-different-sso-domains/
- # Version: 1.1
- # Author: Romain Decker <romain@cloudmaniac.net>
- # January 2017
- #
- # Changes by James Cruce
- # 04/19/18 Added AD Login Credentials for the source vCenter and the destination vCenter
- # 04/19/18 Added functions Get-SourcevCenter, Get-DestvCenter, Ask-VMNameForMigration, Ask-DCForMigration, Ask-ClusterForMigration, Choose-StorageForVMMigration
- # 04/19/18 Deleted function GetPortGroupObject - not needed in our environment
- # Login Credentials for vCenter
- $Global:Cred = $null
- # Prompts for domain credentials
- Write-Verbose -Message 'Please enter domain credentials when prompted. These credentials will be used to connect to both vCenters.' -Verbose
- Start-Sleep -Seconds 5
- ' '
- ' '
- ' '
- ' '
- ' '
- ' '
- ' '
- $Global:Cred = $host.ui.PromptForCredential('Need Domain Credentials.','Please enter your user name and password.','','DomainUserName')
- ####################################################################################
- # Variables
- ####################################################################################
- $Global:DC = $null
- $Global:VMName = $null
- $Global:SourcevCenters = $null
- $Global:DestvCenters = $null
- $Global:Clus = $null
- $Global:Storage = $null
- $SourceVC = $null
- $DestVC = $null
- function Get-SourcevCenter
- {
- Write-Verbose -Message 'Please specify which source vCenter you want to connect to' -Verbose
- $IvCenters = $null
- $i = $null
- $DvCenters = $null
- $IvCenters = 'vcenter1.local', 'vcenter2.local', 'vcenter3.local', 'vcenter4.local', 'vcenter5.local', 'vcenter6.local', 'vcenter7.local'
- $i = 1
- $IvCenters | %{Write-Host $i":" $_; $i++}
- $DvCenters = Read-host 'Enter the number next to the source vCenter'
- $Global:SourcevCenters = $IvCenters[$DvCenters -1]
- ' '
- ' '
- write-verbose -Message "The source vCenter you have selected is $Global:SourcevCenters" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- $title = 'Source vCenter'
- $message = "Is [$Global:SourcevCenters] the correct source vCenter?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The source vCenter choice is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The source vCenter choice is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- ' '
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen [$Global:SourcevCenters] as the source vCenter to connect to" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name SourcevCenters -Scope Global
- Clear-Variable -Name result
- Get-SourcevCenter
- }
- }
- Get-SourcevCenter
- $SourceVC = $Global:SourcevCenters
- function Get-DestvCenter
- {
- Write-Verbose -Message 'Please specify which destination vCenter you want to connect to' -Verbose
- $JvCenters = $null
- $i = $null
- $EvCenters = $null
- $JvCenters = 'vcenter1.local', 'vcenter2.local', 'vcenter3.local', 'vcenter4.local', 'vcenter5.local', 'vcenter6.local', 'vcenter7.local'
- $i = 1
- $JvCenters | %{Write-Host $i":" $_; $i++}
- $EvCenters = Read-host 'Enter the number next to the destination vCenter'
- $Global:DestvCenters = $JvCenters[$EvCenters -1]
- ' '
- ' '
- write-verbose -Message "The destination vCenter you have selected is $Global:DestvCenters" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- $title = 'Destination vCenter'
- $message = "Is [$Global:DestvCenters] the correct vCenter?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The destination vCenter choice is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The destination vCenter choice is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- ' '
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen [$Global:DestvCenters] as the destination vCenter" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name DestvCenters -Scope Global
- Clear-Variable -Name result
- Get-DestvCenter
- }
- }
- Get-DestvCenter
- $DestVC = $Global:DestvCenters
- Connect-VIServer -Server $SourceVC, $DestVC -Credential $Global:Cred -Force
- Function Ask-VMnameForMigration
- {
- ' '
- ' '
- ' '
- ' '
- Write-Verbose -Message "Virtual Machine Name" -Verbose
- $Global:VMName = $NULL
- $Global:VMName = Read-Host -Prompt "Enter the VM name that you want to migrate to the [$DestVC] vCenter" -Verbose
- Write-Verbose -Message "The VM name you have entered is [$Global:VMName]" -Verbose
- ' '
- $title = 'VMName'
- $message = "Is the entered name [$Global:VMName] correct?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The name is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The name is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen to migrate the vm named [$Global:VMName] " -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name VMName -Scope Global
- Clear-Variable -Name result
- Ask-VMnameForMigration
- }
- }
- Ask-VMnameForMigration
- function Ask-DCForVMMigration
- {
- ' '
- ' '
- ' '
- ' '
- Write-Verbose -Message "Choose Destination Datacenter" -Verbose
- $IDC = $null
- $i = $null
- $DSDC = $null
- $IDC = Get-Datacenter -Server $DestVC | Select-Object Name | Sort-object Name
- $i = 1
- $IDC | %{Write-Host $i":" $_.Name; $i++}
- $DSDC = Read-host 'Enter the number next to the datacenter for this vm migration'
- $Global:DC = $IDC[$DSDC -1].Name
- ' '
- ' '
- Write-Verbose -Message "You have selected the [$Global:DC] datacenter." -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- $title = 'DataCenter'
- $message = "Is the Datacenter choice [$Global:DC] correct?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The datacenter choice is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The datacenter choice is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- ' '
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen [$Global:DC] as the Datacenter for this vm migration" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name DC -Scope Global
- Clear-Variable -Name result
- Ask-DC
- }
- }
- Ask-DCForVMMigration
- function Ask-ClusterForVMMigration
- {
- ' '
- ' '
- ' '
- ' '
- Write-Verbose -Message "Choose Destination Cluster" -Verbose
- $IClus = $null
- $i = $null
- $DSClus = $null
- $IClus = Get-Cluster -Location $Global:DC.Name -Server $DestVC| Select-Object Name | Sort-object Name
- $i = 1
- $IClus | %{Write-Host $i":" $_.Name; $i++}
- ' '
- ' '
- $DSClus = Read-host 'Enter the number next to the Cluster for this vm migration'
- $Global:Clus = $IClus[$DSClus -1].Name
- ' '
- ' '
- Write-Verbose -Message "You have selected the [$Global:Clus] cluster." -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- $title = 'Cluster'
- $message = "Is the cluster choice [$Global:Clus] correct?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The cluster choice is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The cluster choice is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- ' '
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen [$Global:Clus] as the cluster for vm migration" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name Clus -Scope Global
- Clear-Variable -Name result
- Ask-ClusterForVMMigration
- }
- }
- Ask-ClusterForVMMigration
- $SourcePortGroup = Get-NetworkAdapter -VM $Global:VMName | Select-Object {$_.NetworkName}
- Function Choose-StorageForVMMigration
- {
- If (($Global:DC -eq 'DC1') -and ($Global:Clus -eq 'Clus1'))
- {
- $Global:Storage = 'sDRS1'
- }
- If (($Global:DC -eq 'DC2') -and ($Global:Clus -eq 'Clus2'))
- {
- $Global:Storage = 'sDRS2'
- }
- If (($Global:DC -eq 'DC3') -and ($Global:Clus -eq 'Clus3'))
- {
- $Global:Storage = 'sDRS3'
- }
- If (($Global:DC -eq 'DC4') -and ($Global:Clus -eq 'Clus4'))
- {
- $Global:Storage = 'sDRS4'
- }
- If (($Global:DC -eq 'DC5') -and ($Global:Clus -eq 'Clus5'))
- {
- $Global:Storage = 'sDRS5'
- }
- If (($Global:DC -eq 'DC6') -and ($Global:Clus -eq 'Clus6'))
- {
- $Global:Storage = 'sDRS6'
- }
- If (($Global:DC -eq 'DC7') -and ($Global:Clus -eq 'Clus7'))
- {
- $Global:Storage = 'sDRS7'
- }
- }
- Choose-StorageForVMMigration
- $DstPortGroup = $SourcePortGroup # The port group or distributed port group.
- function Drawline {
- for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan "-"}
- }
- # vMotion :)
- $vm = Get-VM -name $Global:VMName
- $networkAdapter = Get-NetworkAdapter -VM $vm
- $destination = Get-VMHost -Location $Global:Clus | Select-Object -First 1
- $DestVDSwitch = Get-VMHost -Location $Global:Clus | Get-VDSwitch
- $destinationPortGroup = Get-VDPortGroup -VDSwitch $DestVDSwitch -Name $DstPortGroup.'$_.NetworkName'
- $destinationDatastore = Get-DatastoreCluster ?Name $Global:Storage | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select-Object -First 1
- Write-Verbose -Message "Starting move of virtual machine [$Global:VMName] now" -Verbose
- $vm | Move-VM -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore | out-null
- ####################################################################################
- # Display VM information after vMotion
- write-host -foregroundcolor Cyan "`nVM is now running on:"
- Drawline
- Get-VM $Global:VMName | Get-NetworkAdapter | Select-Object @{N="VM Name";E={$_.Parent.Name}},@{N="Cluster";E={Get-Cluster -VM $_.Parent}},@{N="ESXi Host";E={Get-VMHost -VM $_.Parent}},@{N="Datastore";E={Get-Datastore -VM $_.Parent}},@{N="Network";E={$_.NetworkName}} | Format-List
- Drawline
- ####################################################################################
- # Disconnect
- Disconnect-VIServer -Server * -Force -Confirm:$false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement