I need to be able to migrate a VM from 3.5 to 4.0.
1. Migrate the VM to a shared LUN - Done
Need help with
1. Import the VM into 4.0 from the shared LUN; the below script is not working.
$Datacenter = "Test"
$VMName = "Test-VM"
$ESXHost = get-vmhost | Sort-Object MemoryUsageMB | select name -first 1
$ResourcePool = Get-VMHost $ESXHost.Name | Get-ResourcePool | Get-View
$vmFolder = Get-View (Get-Datacenter -Name $Datacenter | Get-Folder -Name "vm").id
$vmFolder.RegisterVM_Task("[SHARED_LUN]" + $VMName + "/" + $VMName + ".vmx", $VMName, $false, $ResourcePool.MoRef, $null)
Thanks
That will work, but you will have to use the -Server parameter on the Get-Datacenter cmdlet to indicate from which vCenter you want to retrieve the datacenter.
Assume you have two vCenters, called VC1 and VC2, then you can get the datacenter under VC1 like this
$vc1 = Connect-VIServer -Server VC1
$vc2 = Connect-VIServer -Server VC2
Get-Datacenter -Name SameName -Server $vc1
If you would do
Get-Datacenter -Name SameName
you would get an array with 2 objects, one object for the datacenter in each vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Greetings, E4F-
It looks like there might just be a slight issue with the datastore path that you are providing to the RegisterVM_Task() method. You are bulding a string that has no space after the closing square-bracket of the datastore name, but valid datastore paths have a space there. So, the last line would become:
$vmFolder.RegisterVM_Task("[SHARED_LUN] " + $VMName + "/" + $VMName + ".vmx", $VMName, $false, $ResourcePool.MoRef, $null)
Otherwise, it looks good.
Another option would be to use the New-VM cmdlet to register the VM:
## test datacenter name
$strDatacenter = "Test"
$strVMName = "Test-VM"
## the datastore path for the .vmx file to register
$strVMXFilePath = "[SHARED_LUN] $strVMName/$strVMName.vmx"
## the VMhost to use for registering the VM
$hostToUse = Get-VMHost | Sort-Object MemoryUsageMB | Select -first 1
## the folder in which to put the VM
$oVmFolder = Get-Datacenter -Name $strDatacenter | Get-Folder -Name "vm"
## call New-VM to register the VM
New-VM -VMHost $hostToUse -Name $strVMName -Location $oVmFolder -VMFilePath $strVMXFilePath
I'll be interested to hear how these work for you.
I am getting the following error:
Register virtual machine
TestDC
A component of the virtual machine is not accessible on the host.
$Datacenter = "Test-DC"
$VMName = "Test-VM"
$ESXHost = get-vmhost | Sort-Object MemoryUsageMB | select name -first 1
$ResourcePool = Get-VMHost $ESXHost.Name | Get-ResourcePool | Get-View
$vmFolder = Get-View (Get-Datacenter -Name $Datacenter | Get-Folder -Name "vm").id
$vmFolder.RegisterVM_Task("[SHARED_LUN] " + $VMName + "/" + $VMName + ".vmx", $VMName, $false, $ResourcePool.MoRef, $null)
Could it be that the VM still had a CDROM ISO or a floppy image mounted ?
If any of those are on non-shared storage you can get this message.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you have more than one cluster in this datacentre? It might be that the line:
$ESXHost = get-vmhost | Sort-Object MemoryUsageMB | select name -first 1
returns a host in a different cluster that has no access to the datastore the vmx file is on.
You might change line into:
$ESXHost = Get-Cluster "myCluster" |get-vmhost | Sort-Object MemoryUsageMB | select name -first 1
Regards, Robert
Nothing mounted in CD or floppy.
I think that I have found the problem; both consoles have the same datacenter name. This works fine in another environment where the two consoles have different names but I don't know why the same datacenter name breaks it. Any suggestions or will this just not work?
That will work, but you will have to use the -Server parameter on the Get-Datacenter cmdlet to indicate from which vCenter you want to retrieve the datacenter.
Assume you have two vCenters, called VC1 and VC2, then you can get the datacenter under VC1 like this
$vc1 = Connect-VIServer -Server VC1
$vc2 = Connect-VIServer -Server VC2
Get-Datacenter -Name SameName -Server $vc1
If you would do
Get-Datacenter -Name SameName
you would get an array with 2 objects, one object for the datacenter in each vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference