Get-Ryan

[PowerCLI] Move-VMDisk2

Feb 9th, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Move-VMDisk2 {
  2.  
  3. <#
  4. .DESCRIPTION
  5.  This script will storage vMotion a designated VM with 2 disks on different LUNs to 2 different LUNs in VMWare ESXi.
  6. .EXAMPLE USAGE
  7.  Move-VMDisk2 -VMName TestBox -OriginalLun1Name Lun1 -OriginalLun2Name Lun2 -DestinationLun1Name Lun4 -DestinationLun2Name Lun5
  8. .NOTES
  9.  Script created from reference https://psvmware.wordpress.com/2012/09/04/storage-vmotion-to-different-datastores-for-different-vm-hard-disks/
  10.  and from reference http://sites.utexas.edu/dbailey/2012/10/29/advanced-storage-vmotions-via-powershell/
  11.  Accessed on 2/9/2016
  12. .DISCLAIMER
  13.  This script does not take into account the scenario where a VM may have multiple disks on a single LUN. It is set to error out if it detects that scenario.
  14. ,REQUIRED MODULES
  15.  PowerCLI by VMWare
  16. #>
  17.  
  18.     param(
  19.         [parameter(Mandatory=$true,
  20.                    Position=0)]
  21.         [String]
  22.         $VMName,
  23.  
  24.         [parameter(Mandatory=$true,
  25.                    Position=1)]
  26.         [String]
  27.         $OriginalLun1Name,
  28.  
  29.         [parameter(Mandatory=$true,
  30.                    Position=2)]
  31.         [String]
  32.         $OriginalLun2Name,
  33.          
  34.         [parameter(Mandatory=$true,
  35.                    Position=3)]
  36.         [String]
  37.         $DestinationLun1Name,
  38.  
  39.         [parameter(Mandatory=$true,
  40.                    Position=4)]
  41.         [String]
  42.         $DestinationLun2Name
  43.          )
  44.  
  45.     ###--- IMPORTANT --- This assumes the VM does NOT have multiple disks on the same Lun - DO NOT USE if that is the case###
  46.  
  47.     $cred = Import-Clixml -Path <Path to XML credential file>
  48.  
  49.     Connect-VIServer -Server <ServerName> -Credential $cred | Out-Null
  50.  
  51.     Try{
  52.         $VM = Get-VM -Name $VMName
  53.     }
  54.     Catch{
  55.         Write-Error -Message "Unable to locate a VM with name $VMName."
  56.         Break
  57.     }
  58.     $Disks = $VM | Get-HardDisk
  59.     $Disk1 = $Disks | Where-Object{$_.Filename -match $OriginalLun1Name}
  60.     If(!$Disk1){Write-Error -Message "Unable to locate VM disk on Lun matching name: $OriginalLun1Name";Break}
  61.     $Disk2 = $Disks | Where-Object{$_.Filename -match $OriginalLun2Name}
  62.     If(!$Disk2){Write-Error -Message "Unable to locate VM disk on Lun matching name: $OriginalLun2Name";Break}
  63.  
  64.     If($Disk1.Count -gt 1 -or $Disk2.Count -gt 1){
  65.         Write-Error -Message "This script does not support moving VMs with multiple disks on a single LUN."
  66.         Break
  67.     }
  68.     $DiskIds = New-Object System.Collections.ArrayList
  69.     $DiskIds.Add($Disk1.Id.Split('/')[1]) | Out-Null
  70.     $DiskIds.Add($Disk2.Id.Split('/')[1]) | Out-Null
  71.  
  72.     Try{
  73.         $Lun1 = (Get-Datastore -Name $DestinationLun1Name -ErrorAction Stop | Get-View).MoRef.Value
  74.     }
  75.     Catch{
  76.         Write-Error -Message "Unable to find LUN with name: $DestinationLun1Name"
  77.         Break
  78.     }
  79.     Try{
  80.         $Lun2 = (Get-Datastore -Name $DestinationLun2Name -ErrorAction Stop | Get-View).MoRef.Value
  81.     }
  82.     Catch{
  83.         Write-Error -Message "Unable to find LUN with name: $DestinationLun2Name"
  84.         Break
  85.     }
  86.  
  87.     $Spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
  88.  
  89.     $Spec.Datastore = New-Object VMware.Vim.ManagedObjectReference
  90.     $Spec.Datastore.Type = 'Datastore'
  91.     $Spec.Datastore.Value = $Lun1
  92.  
  93.     $Spec.Disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator[] (2)
  94.     $Spec.Disk[0] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
  95.     $Spec.Disk[0].DiskId = $DiskIds[0]
  96.     $Spec.Disk[0].Datastore = New-Object VMware.Vim.ManagedObjectReference
  97.     $Spec.Disk[0].Datastore.Type = 'Datastore'
  98.     $Spec.Disk[0].Datastore.Value = $Lun1
  99.     $Spec.Disk[1] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
  100.     $Spec.Disk[1].DiskId = $DiskIds[1]
  101.     $Spec.Disk[1].Datastore = New-Object VMware.Vim.ManagedObjectReference
  102.     $Spec.Disk[1].Datastore.Type = 'Datastore'
  103.     $Spec.Disk[1].Datastore.Value = $Lun2
  104.  
  105.     If($VM.PowerState -eq "PoweredOn"){
  106.  
  107.     Shutdown-VMGuest -VM $VM.Name -Confirm:$false | Out-Null
  108.         Start-Sleep -Seconds 5
  109.     }
  110.  
  111.     While((Get-VM -Name $VM.Name).PowerState -eq "PoweredOn"){
  112.         Start-Sleep -Seconds 5
  113.     }
  114.    
  115.     $VM.ExtensionData.RelocateVM_Task($Spec, 'defaultPriority') | Out-Null
  116.  
  117.     While((Get-Task | Where-Object{$_.Name -eq 'RelocateVM_Task'}).State -eq "Running"){
  118.         Start-Sleep -Seconds 10
  119.     }
  120.  
  121.     Start-VM -VM $VM.Name -Confirm:$false -RunAsync | Out-Null
  122.  
  123.     Disconnect-VIServer -Confirm:$false | Out-Null
  124. }
Advertisement
Add Comment
Please, Sign In to add comment