Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Move-VMDisk2 {
- <#
- .DESCRIPTION
- This script will storage vMotion a designated VM with 2 disks on different LUNs to 2 different LUNs in VMWare ESXi.
- .EXAMPLE USAGE
- Move-VMDisk2 -VMName TestBox -OriginalLun1Name Lun1 -OriginalLun2Name Lun2 -DestinationLun1Name Lun4 -DestinationLun2Name Lun5
- .NOTES
- Script created from reference https://psvmware.wordpress.com/2012/09/04/storage-vmotion-to-different-datastores-for-different-vm-hard-disks/
- and from reference http://sites.utexas.edu/dbailey/2012/10/29/advanced-storage-vmotions-via-powershell/
- Accessed on 2/9/2016
- .DISCLAIMER
- 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.
- ,REQUIRED MODULES
- PowerCLI by VMWare
- #>
- param(
- [parameter(Mandatory=$true,
- Position=0)]
- [String]
- $VMName,
- [parameter(Mandatory=$true,
- Position=1)]
- [String]
- $OriginalLun1Name,
- [parameter(Mandatory=$true,
- Position=2)]
- [String]
- $OriginalLun2Name,
- [parameter(Mandatory=$true,
- Position=3)]
- [String]
- $DestinationLun1Name,
- [parameter(Mandatory=$true,
- Position=4)]
- [String]
- $DestinationLun2Name
- )
- ###--- IMPORTANT --- This assumes the VM does NOT have multiple disks on the same Lun - DO NOT USE if that is the case###
- $cred = Import-Clixml -Path <Path to XML credential file>
- Connect-VIServer -Server <ServerName> -Credential $cred | Out-Null
- Try{
- $VM = Get-VM -Name $VMName
- }
- Catch{
- Write-Error -Message "Unable to locate a VM with name $VMName."
- Break
- }
- $Disks = $VM | Get-HardDisk
- $Disk1 = $Disks | Where-Object{$_.Filename -match $OriginalLun1Name}
- If(!$Disk1){Write-Error -Message "Unable to locate VM disk on Lun matching name: $OriginalLun1Name";Break}
- $Disk2 = $Disks | Where-Object{$_.Filename -match $OriginalLun2Name}
- If(!$Disk2){Write-Error -Message "Unable to locate VM disk on Lun matching name: $OriginalLun2Name";Break}
- If($Disk1.Count -gt 1 -or $Disk2.Count -gt 1){
- Write-Error -Message "This script does not support moving VMs with multiple disks on a single LUN."
- Break
- }
- $DiskIds = New-Object System.Collections.ArrayList
- $DiskIds.Add($Disk1.Id.Split('/')[1]) | Out-Null
- $DiskIds.Add($Disk2.Id.Split('/')[1]) | Out-Null
- Try{
- $Lun1 = (Get-Datastore -Name $DestinationLun1Name -ErrorAction Stop | Get-View).MoRef.Value
- }
- Catch{
- Write-Error -Message "Unable to find LUN with name: $DestinationLun1Name"
- Break
- }
- Try{
- $Lun2 = (Get-Datastore -Name $DestinationLun2Name -ErrorAction Stop | Get-View).MoRef.Value
- }
- Catch{
- Write-Error -Message "Unable to find LUN with name: $DestinationLun2Name"
- Break
- }
- $Spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
- $Spec.Datastore = New-Object VMware.Vim.ManagedObjectReference
- $Spec.Datastore.Type = 'Datastore'
- $Spec.Datastore.Value = $Lun1
- $Spec.Disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator[] (2)
- $Spec.Disk[0] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
- $Spec.Disk[0].DiskId = $DiskIds[0]
- $Spec.Disk[0].Datastore = New-Object VMware.Vim.ManagedObjectReference
- $Spec.Disk[0].Datastore.Type = 'Datastore'
- $Spec.Disk[0].Datastore.Value = $Lun1
- $Spec.Disk[1] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
- $Spec.Disk[1].DiskId = $DiskIds[1]
- $Spec.Disk[1].Datastore = New-Object VMware.Vim.ManagedObjectReference
- $Spec.Disk[1].Datastore.Type = 'Datastore'
- $Spec.Disk[1].Datastore.Value = $Lun2
- If($VM.PowerState -eq "PoweredOn"){
- Shutdown-VMGuest -VM $VM.Name -Confirm:$false | Out-Null
- Start-Sleep -Seconds 5
- }
- While((Get-VM -Name $VM.Name).PowerState -eq "PoweredOn"){
- Start-Sleep -Seconds 5
- }
- $VM.ExtensionData.RelocateVM_Task($Spec, 'defaultPriority') | Out-Null
- While((Get-Task | Where-Object{$_.Name -eq 'RelocateVM_Task'}).State -eq "Running"){
- Start-Sleep -Seconds 10
- }
- Start-VM -VM $VM.Name -Confirm:$false -RunAsync | Out-Null
- Disconnect-VIServer -Confirm:$false | Out-Null
- }
Advertisement
Add Comment
Please, Sign In to add comment