SHARE
TWEET

Cross-SSO-vMotion-Between-vCenters

jmeg8r May 19th, 2018 (edited) 1,992 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Name: Cross-SSO_vMotion-between-vCenters.ps1
  2. # Description: PowerCLI script to vMotion a running VM between two vCenters which are
  3. #              not in the same SSO domain.
  4. #              The script will use the GetPortGroupObject() function to determine the
  5. #              port group type (standard or distributed) and return the correct object.          
  6. # Reference: http://cloudmaniac.net/using-powercli-to-vmotion-vm-between-different-sso-domains/
  7. # Version: 1.1
  8. # Author: Romain Decker <romain@cloudmaniac.net>
  9. # January 2017
  10. #
  11. # Changes by James Cruce
  12. # 04/19/18 Added AD Login Credentials for the source vCenter and the destination vCenter
  13. # 04/19/18 Added functions Get-SourcevCenter, Get-DestvCenter, Ask-VMNameForMigration, Ask-DCForMigration, Ask-ClusterForMigration, Choose-StorageForVMMigration
  14. # 04/19/18 Deleted function GetPortGroupObject - not needed in our environment
  15.  
  16.  
  17. # Login Credentials for vCenter
  18.  
  19. $Global:Cred = $null
  20.  
  21.  
  22. # Prompts for domain credentials
  23. Write-Verbose -Message 'Please enter domain credentials when prompted. These credentials will be used to connect to both vCenters.' -Verbose
  24. Start-Sleep -Seconds 5
  25. ' '
  26. ' '
  27. ' '
  28. ' '
  29. ' '
  30. ' '
  31. ' '
  32. $Global:Cred = $host.ui.PromptForCredential('Need Domain Credentials.','Please enter your user name and password.','','DomainUserName')
  33.  
  34.  
  35.  
  36.  
  37.  
  38. ####################################################################################
  39. # Variables
  40. ####################################################################################
  41.  
  42.  
  43.  
  44.  
  45. $Global:DC = $null
  46.  
  47. $Global:VMName = $null
  48.  
  49. $Global:SourcevCenters = $null
  50.  
  51. $Global:DestvCenters = $null
  52.  
  53. $Global:Clus = $null
  54.  
  55. $Global:Storage = $null
  56.  
  57. $SourceVC = $null
  58.  
  59. $DestVC = $null
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. function Get-SourcevCenter
  68. {
  69.    
  70.     Write-Verbose -Message 'Please specify which source vCenter you want to connect to' -Verbose
  71.    
  72.    
  73.  
  74.     $IvCenters = $null
  75.     $i = $null
  76.     $DvCenters = $null
  77.  
  78.  
  79.     $IvCenters = 'vcenter1.local', 'vcenter2.local', 'vcenter3.local', 'vcenter4.local', 'vcenter5.local', 'vcenter6.local', 'vcenter7.local'
  80.     $i = 1
  81.     $IvCenters | %{Write-Host $i":" $_; $i++}
  82.     $DvCenters = Read-host 'Enter the number next to the source vCenter'
  83.     $Global:SourcevCenters = $IvCenters[$DvCenters -1]
  84.     ' '
  85.     ' '
  86.     write-verbose -Message "The source vCenter you have selected is $Global:SourcevCenters" -Verbose
  87.     ' '
  88.     ' '
  89.     ' '
  90.     ' '
  91.     ' '
  92.    
  93.     $title = 'Source vCenter'
  94.     $message = "Is [$Global:SourcevCenters] the correct source vCenter?"
  95.  
  96.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  97.         'The source vCenter choice is correct. Proceed to next step'
  98.  
  99.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  100.         'The source vCenter choice is not correct. Repeat previous step'
  101.  
  102.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  103.  
  104.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  105.  
  106.     switch ($result)
  107.         {
  108.             0 {'You selected Yes.'}
  109.             1 {'You selected No.'}
  110.         }
  111.    
  112.     ' '
  113.  
  114.         If ($result -eq 0)
  115.     {
  116.         Write-Verbose -Message "You have chosen [$Global:SourcevCenters] as the source vCenter to connect to" -Verbose
  117.         ' '
  118.         ' '
  119.         ' '
  120.         ' '
  121.         ' '
  122.  
  123.     }
  124.     ElseIf ($result -eq 1)
  125.     {
  126.         Clear-Variable -Name SourcevCenters -Scope Global
  127.         Clear-Variable -Name result
  128.         Get-SourcevCenter
  129.     }
  130.  
  131.    
  132.  
  133.  
  134. }
  135.  
  136. Get-SourcevCenter
  137.  
  138. $SourceVC = $Global:SourcevCenters
  139.  
  140. function Get-DestvCenter
  141. {
  142.    
  143.     Write-Verbose -Message 'Please specify which destination vCenter you want to connect to' -Verbose
  144.    
  145.    
  146.  
  147.     $JvCenters = $null
  148.     $i = $null
  149.     $EvCenters = $null
  150.  
  151.  
  152.     $JvCenters = 'vcenter1.local', 'vcenter2.local', 'vcenter3.local', 'vcenter4.local', 'vcenter5.local', 'vcenter6.local', 'vcenter7.local'
  153.     $i = 1
  154.     $JvCenters | %{Write-Host $i":" $_; $i++}
  155.     $EvCenters = Read-host 'Enter the number next to the destination vCenter'
  156.     $Global:DestvCenters = $JvCenters[$EvCenters -1]
  157.     ' '
  158.     ' '
  159.     write-verbose -Message "The destination vCenter you have selected is $Global:DestvCenters" -Verbose
  160.     ' '
  161.     ' '
  162.     ' '
  163.     ' '
  164.     ' '
  165.    
  166.     $title = 'Destination vCenter'
  167.     $message = "Is [$Global:DestvCenters] the correct vCenter?"
  168.  
  169.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  170.         'The destination vCenter choice is correct. Proceed to next step'
  171.  
  172.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  173.         'The destination vCenter choice is not correct. Repeat previous step'
  174.  
  175.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  176.  
  177.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  178.  
  179.     switch ($result)
  180.         {
  181.             0 {'You selected Yes.'}
  182.             1 {'You selected No.'}
  183.         }
  184.    
  185.     ' '
  186.  
  187.         If ($result -eq 0)
  188.     {
  189.         Write-Verbose -Message "You have chosen [$Global:DestvCenters] as the destination vCenter" -Verbose
  190.         ' '
  191.         ' '
  192.         ' '
  193.         ' '
  194.         ' '
  195.  
  196.     }
  197.     ElseIf ($result -eq 1)
  198.     {
  199.         Clear-Variable -Name DestvCenters -Scope Global
  200.         Clear-Variable -Name result
  201.         Get-DestvCenter
  202.     }
  203.  
  204.    
  205.  
  206.  
  207. }
  208.  
  209. Get-DestvCenter
  210.  
  211. $DestVC = $Global:DestvCenters
  212.  
  213. Connect-VIServer -Server $SourceVC, $DestVC -Credential $Global:Cred -Force
  214.  
  215. Function Ask-VMnameForMigration
  216. {
  217. ' '
  218. ' '
  219. ' '
  220. ' '
  221.     Write-Verbose -Message "Virtual Machine Name" -Verbose
  222.  
  223.  
  224.    
  225.     $Global:VMName = $NULL
  226.  
  227.     $Global:VMName = Read-Host -Prompt "Enter the VM name that you want to migrate to the [$DestVC] vCenter" -Verbose
  228.  
  229.     Write-Verbose -Message "The VM name you have entered is [$Global:VMName]" -Verbose
  230.     ' '
  231.  
  232.     $title = 'VMName'
  233.     $message = "Is the entered name [$Global:VMName] correct?"
  234.  
  235.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  236.         'The name is correct. Proceed to next step'
  237.  
  238.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  239.         'The name is not correct. Repeat previous step'
  240.  
  241.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  242.  
  243.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  244.  
  245.     switch ($result)
  246.         {
  247.             0 {'You selected Yes.'}
  248.             1 {'You selected No.'}
  249.         }
  250.  
  251.  
  252.     If ($result -eq 0)
  253.     {
  254.         Write-Verbose -Message "You have chosen to migrate the vm named [$Global:VMName] " -Verbose
  255.         ' '
  256.         ' '
  257.         ' '
  258.         ' '
  259.         ' '
  260.  
  261.     }
  262.     ElseIf ($result -eq 1)
  263.     {
  264.         Clear-Variable -Name VMName -Scope Global
  265.         Clear-Variable -Name result
  266.         Ask-VMnameForMigration
  267.     }
  268.    
  269.  
  270. }
  271.  
  272. Ask-VMnameForMigration
  273.  
  274. function Ask-DCForVMMigration
  275. {
  276.  
  277.     ' '
  278.     ' '
  279.     ' '
  280.     ' '
  281.     Write-Verbose -Message "Choose Destination Datacenter" -Verbose
  282.  
  283.  
  284.     $IDC = $null
  285.     $i = $null
  286.     $DSDC = $null
  287.  
  288.  
  289.     $IDC = Get-Datacenter -Server $DestVC | Select-Object Name | Sort-object Name
  290.     $i = 1
  291.     $IDC | %{Write-Host $i":" $_.Name; $i++}
  292.     $DSDC = Read-host 'Enter the number next to the datacenter for this vm migration'
  293.     $Global:DC = $IDC[$DSDC -1].Name
  294.     ' '
  295.     ' '
  296.     Write-Verbose -Message "You have selected the [$Global:DC] datacenter." -Verbose
  297.     ' '
  298.     ' '
  299.     ' '
  300.     ' '
  301.     ' '
  302.    
  303.     $title = 'DataCenter'
  304.     $message = "Is the Datacenter choice [$Global:DC] correct?"
  305.  
  306.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  307.         'The datacenter choice is correct. Proceed to next step'
  308.  
  309.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  310.         'The datacenter choice is not correct. Repeat previous step'
  311.  
  312.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  313.  
  314.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  315.  
  316.     switch ($result)
  317.         {
  318.             0 {'You selected Yes.'}
  319.             1 {'You selected No.'}
  320.         }
  321.    
  322.     ' '
  323.  
  324.         If ($result -eq 0)
  325.     {
  326.         Write-Verbose -Message "You have chosen [$Global:DC] as the Datacenter for this vm migration" -Verbose
  327.         ' '
  328.         ' '
  329.         ' '
  330.         ' '
  331.         ' '
  332.  
  333.     }
  334.     ElseIf ($result -eq 1)
  335.     {
  336.         Clear-Variable -Name DC -Scope Global
  337.         Clear-Variable -Name result
  338.         Ask-DC
  339.     }
  340.    
  341.    
  342.  
  343. }
  344.  
  345. Ask-DCForVMMigration
  346.  
  347. function Ask-ClusterForVMMigration
  348. {
  349.  
  350.     ' '
  351.     ' '
  352.     ' '
  353.     ' '
  354.     Write-Verbose -Message "Choose Destination Cluster" -Verbose
  355.  
  356.     $IClus = $null
  357.     $i = $null
  358.     $DSClus = $null
  359.  
  360.  
  361.     $IClus = Get-Cluster -Location $Global:DC.Name -Server $DestVC| Select-Object Name | Sort-object Name
  362.     $i = 1
  363.     $IClus | %{Write-Host $i":" $_.Name; $i++}
  364.     ' '
  365.     ' '
  366.     $DSClus = Read-host 'Enter the number next to the Cluster for this vm migration'
  367.     $Global:Clus = $IClus[$DSClus -1].Name
  368.     ' '
  369.     ' '
  370.     Write-Verbose -Message "You have selected the [$Global:Clus] cluster." -Verbose
  371.     ' '
  372.     ' '
  373.     ' '
  374.     ' '
  375.     ' '
  376.     $title = 'Cluster'
  377.     $message = "Is the cluster choice [$Global:Clus] correct?"
  378.  
  379.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  380.         'The cluster choice is correct. Proceed to next step'
  381.  
  382.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  383.         'The cluster choice is not correct. Repeat previous step'
  384.  
  385.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  386.  
  387.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  388.  
  389.     switch ($result)
  390.         {
  391.             0 {'You selected Yes.'}
  392.             1 {'You selected No.'}
  393.         }
  394.    
  395.     ' '
  396.  
  397.         If ($result -eq 0)
  398.     {
  399.         Write-Verbose -Message "You have chosen [$Global:Clus] as the cluster for vm migration" -Verbose
  400.         ' '
  401.         ' '
  402.         ' '
  403.         ' '
  404.         ' '
  405.  
  406.     }
  407.     ElseIf ($result -eq 1)
  408.     {
  409.         Clear-Variable -Name Clus -Scope Global
  410.         Clear-Variable -Name result
  411.         Ask-ClusterForVMMigration
  412.     }
  413.  
  414.  
  415.        
  416.  
  417. }
  418.  
  419. Ask-ClusterForVMMigration
  420.  
  421.  
  422. $SourcePortGroup = Get-NetworkAdapter -VM $Global:VMName | Select-Object {$_.NetworkName}
  423.  
  424. Function Choose-StorageForVMMigration
  425. {
  426.  
  427.  If (($Global:DC -eq 'DC1') -and ($Global:Clus -eq 'Clus1'))
  428.     {
  429.         $Global:Storage = 'sDRS1'
  430.     }
  431.     If (($Global:DC -eq 'DC2') -and ($Global:Clus -eq 'Clus2'))
  432.     {
  433.         $Global:Storage = 'sDRS2'
  434.     }
  435.     If (($Global:DC -eq 'DC3') -and ($Global:Clus -eq 'Clus3'))
  436.     {
  437.         $Global:Storage = 'sDRS3'
  438.     }
  439.     If (($Global:DC -eq 'DC4') -and ($Global:Clus -eq 'Clus4'))
  440.     {
  441.         $Global:Storage = 'sDRS4'
  442.     }
  443.     If (($Global:DC -eq 'DC5') -and ($Global:Clus -eq 'Clus5'))
  444.     {
  445.         $Global:Storage = 'sDRS5'
  446.     }
  447.     If (($Global:DC -eq 'DC6') -and ($Global:Clus -eq 'Clus6'))
  448.     {
  449.         $Global:Storage = 'sDRS6'
  450.     }
  451.     If (($Global:DC -eq 'DC7') -and ($Global:Clus -eq 'Clus7'))
  452.     {
  453.         $Global:Storage = 'sDRS7'
  454.     }
  455.  
  456.    
  457. }
  458.  
  459. Choose-StorageForVMMigration
  460.  
  461.  
  462.  
  463.  
  464. $DstPortGroup = $SourcePortGroup # The port group or distributed port group.
  465.  
  466.  
  467.  
  468.  
  469. function Drawline {
  470.     for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan "-"}
  471. }
  472.  
  473.  
  474. # vMotion :)
  475. $vm = Get-VM -name $Global:VMName
  476.  
  477. $networkAdapter = Get-NetworkAdapter -VM $vm
  478.  
  479.  
  480. $destination = Get-VMHost -Location $Global:Clus | Select-Object -First 1
  481.  
  482. $DestVDSwitch = Get-VMHost -Location $Global:Clus | Get-VDSwitch
  483.  
  484.  
  485. $destinationPortGroup = Get-VDPortGroup -VDSwitch $DestVDSwitch -Name $DstPortGroup.'$_.NetworkName'
  486.  
  487.  
  488.  
  489.  
  490. $destinationDatastore = Get-DatastoreCluster ?Name $Global:Storage  | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select-Object -First 1
  491.  
  492. Write-Verbose -Message "Starting move of virtual machine [$Global:VMName] now" -Verbose
  493.  
  494.  
  495. $vm | Move-VM -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore  | out-null
  496.  
  497. ####################################################################################
  498. # Display VM information after vMotion
  499. write-host -foregroundcolor Cyan "`nVM is now running on:"
  500. Drawline
  501. 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
  502. Drawline
  503.  
  504.  
  505. ####################################################################################
  506.  
  507.  
  508. # Disconnect
  509. Disconnect-VIServer -Server * -Force -Confirm:$false
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top