function Move-VMs{
param($vm)
Write-Host $vm
$dsNameHDD1 = 0
$HDDs = Get-HardDisk -VM $vm
# a foreach loop to move vmdk
$HDDs | %{
# Get the datastore name of the old
$oldDS = $_.Filename.Split(']')[0].TrimStart('[')
$sourceDS = "datastore1"
# as @pfuhli said the new lun has a preceding letter that differs from the old.
$newDS = "datastore2"
# Here i check which is the first hdd to later move the config there
if ($_.Name -eq "Festplatte 1" -and $oldDS -eq $sourceDS){
$dsNameHDD1 = $newDS
}
$newDS = Get-Datastore $newDS
if ($SourceDS -eq $oldDS)
{
Set-HardDisk -HardDisk $_ -Datastore $newDS -Confirm:$false -StorageFormat Thin
}
}
# This part is for moving the config file
$HDDs = Get-HardDisk -VM $vm
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.datastore = (Get-Datastore -Name $dsNameHDD1).Extensiondata.MoRef
$HDDs | %{
$disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$disk.diskId = $_.Extensiondata.Key
$disk.datastore = $_.Extensiondata.Backing.Datastore
$spec.disk += $disk
}
$vm.Extensiondata.RelocateVM_Task($spec, "defaultPriority")
}
Get-VM -Datastore "datastore1" | %{
Move-VMs $_
Just had a look how I moved VMs the last time.