vSphere 4.0 U2/vCenter 4.0 U2/PowerCLI 4.0 U1
I'm not sure if the issue is that the script I've modded from here is out-of-date for the version of CLI or not. If it is, I have no idea how to update for this version of PowerCLI. I have some 200 VMs to convert from thick to thin really isn't an option.
Here's the script:
# Convert Machines To Thin Provisioned # Ben Neise 16/03/10 # Create an empty array $arrMachinesToBeConverted = @() Write-Host "Getting virtual machine objects" -ForegroundColor Blue Write-Host "" # Select the broad category of VMs we're looking at, for example all the machines in a specific blue folder $objVMs = Get-Folder "vmlab04" | Get-VM | Sort-Object Write-Host "Generating list of candidates" -ForegroundColor Blue Write-Host "" # Loop through all the virtual machines selected ForEach ($objVM in $objVMs){ Write-Host "Investigating " -NoNewline Write-Host $objVM -ForegroundColor Blue -NoNewline # Skip the rest of the loop if the machine is thin provisioned If ($objVM | Get-HardDisk | Where-Object {$_.StorageFormat -like "Thin"}){ Write-Host " - disks are already thin provisioned" -ForegroundColor DarkGray continue } # Skip the rest of the loop is the machine is switched on and non-persistent If ($objVM | Where-Object {$_.PowerState -ne "PoweredOff"} | Get-HardDisk | Where-Object {$_.Persistence -notlike "IndependentPersistent"}){ Write-Host " - switched on and non-persistent" -ForegroundColor DarkGray continue } # Skip the rest of the loop if the machine is powered-on, and has snapshots If ($objVM | Where-Object {$_.PowerState -ne "PoweredOff" -and ($objVM | Get-Snapshot)}) { Write-Host " - switched on and with snapshots" -ForegroundColor DarkGray continue } # Skips the rest of the loop if the machine has a shared drive and is not set up as fault tolerant (indicating that it's a Linked Clone) # Thanks to Keshav Attrey for this method - http://www.vmdev.info/?p=546) $viewVM = $objVM | Get-View -Property Name,Summary,Config.Hardware.Device $unshared = $viewVM.Summary.Storage.Unshared $committed = $viewVM.Summary.Storage.Committed $ftInfo = $viewVM.Summary.Config.FtInfo If (($unshared -ne $committed) -and (($ftInfo -eq $null) -or ($ftInfo.InstanceUuids.Length -le 1))){ Write-Host "The machine is a linked clone" -ForegroundColor DarkGray continue } Write-Host "Added to the list of machines to be converted" $arrMachinesToBeConverted += $objVM } Write-Host "Starting Storage vMotions" -ForegroundColor Blue Write-Host $arrMachinesToBeConverted.Count -ForegroundColor Blue -NoNewline Write-Host " machines to be converted" -ForegroundColor DarkGray Write-Host "" ForEach ($objVM in $arrMachinesToBeConverted | Sort-Object){ # Get the biggest datastore $objBiggestDatastore = Get-Datastore | Sort-Object -Property FreeSpaceMB -Descending # Select the datastore from the top of the previously generated list (index 0) and remove the preceeding "Datastore-" from it's ID to give us the MOID $strTargetDatastore = ($objBiggestDatastore[0].Id).replace('Datastore-','') # Let the user know what's going on Write-Host "Migrating machine " -NoNewline Write-Host $objVM -ForegroundColor Blue -NoNewline Write-Host ", to Thin Provisioned format on datastore " -NoNewline Write-Host $objBiggestDatastore[0] -ForegroundColor Blue -NoNewline Write-Host "" # Get the view of the VM we're moving $viewVM = Get-View -VIObject $objVM # Remove the preceding "HostSystem-" from the VM's Host's ID, giving us the Host's MOID $strTargetHost = ($objVM.host.id).replace('HostSystem-','') # Create a task specification for the relocation $specRelocate = New-Object Vim.VirtualMachineRelocateSpec # Add the target datastore to the specification using the MOID $specRelocate.datastore = New-Object Vim.ManagedObjectReference $specRelocate.datastore.type = "Datastore" $specRelocate.datastore.value = $strTargetDatastore # Add the host to the specification using the MOID $specRelocate.host = New-Object Vim.ManagedObjectReference $specRelocate.host.type = "HostSystem" $specRelocate.host.value = $strTargetHost # This is the specification property that makes the disk Thin Provisioned $specRelocate.transform = "sparse" # Create the task where the previously specified task is applied to the view of the target VM $task = $viewVM.RelocateVM_Task($specRelocate, $priority) # Start the task, and wait for it to complete before continuing Get-VIObjectByVIView $task | Wait-Task | Out-Null }
The script correctly identifies the VMs to be moved and the datastore to move it to, but he execution fails as follows:
Migrating machine 000019-WinXPPro, to Thin Provisioned format on datastore VMLAB02 New-Object : Cannot find type [http://Vim.VirtualMachineRelocateSpec|http://Vim.VirtualMachineRelocateSpec]: make sure the assembly containing this type is loaded. At D:\thick2thin.ps1:80 char:31 + $specRelocate = New-Object <<<< Vim.VirtualMachineRelocateSpec New-Object : Cannot find type [http://Vim.ManagedObjectReference|http://Vim.ManagedObjectReference]: make sure the assembly containing this type is loaded. At D:\thick2thin.ps1:82 char:41 + $specRelocate.datastore = New-Object <<<< Vim.ManagedObjectReference Property 'type' cannot be found on this object; make sure it exists and is settable. At D:\thick2thin.ps1:83 char:29 + $specRelocate.datastore.t <<<< ype = "Datastore" Property 'value' cannot be found on this object; make sure it exists and is settable. At D:\thick2thin.ps1:84 char:29 + $specRelocate.datastore.v <<<< alue = $strTargetDatastore New-Object : Cannot find type [http://Vim.ManagedObjectReference|http://Vim.ManagedObjectReference]: make sure the assembly containing this type is loaded. At D:\thick2thin.ps1:86 char:36 + $specRelocate.host = New-Object <<<< Vim.ManagedObjectReference Property 'type' cannot be found on this object; make sure it exists and is settable. At D:\thick2thin.ps1:87 char:24 + $specRelocate.host.t <<<< ype = "HostSystem" Property 'value' cannot be found on this object; make sure it exists and is settable. At D:\thick2thin.ps1:88 char:24 + $specRelocate.host.v <<<< alue = $strTargetHost Property 'transform' cannot be found on this object; make sure it exists and is settable. At D:\thick2thin.ps1:90 char:19 + $specRelocate.t <<<< ransform = "sparse" Exception calling "RelocateVM_Task" with "2" argument(s): "spec" At D:\thick2thin.ps1:92 char:36 + $task = $viewVM.RelocateVM_Task( <<<< $specRelocate, $priority) Get-VIObjectByVIView : The argument cannot be null or empty. At D:\thick2thin.ps1:94 char:25 + Get-VIObjectByVIView <<<< $task | Wait-Task | Out-Null
I've already run the
[http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Vim")
before running the script. If someone can help me get this thing running, it would be greatly appreciated.
try replacing
New-Object Vim.VirtualMachineRelocateSpec
with
New-Object VMware.Vim.VirtualMachineRelocateSpec
Well, got rid of one of bulk of the execution errors...I prefaced with VMware.vim and got past those...now I think I have a totally different issue.
Exception calling "RelocateVM_Task" with "2" argument(s): "Invalid / explicitly disabled state operation is invoked." At D:\thick2thin.ps1:92 char:36 + $task = $viewVM.RelocateVM_Task( <<<< $specRelocate, $priority) Get-VIObjectByVIView : The argument cannot be null or empty. At D:\thick2thin.ps1:94 char:25 + Get-VIObjectByVIView <<<< $task | Wait-Task | Out-Null
try replacing
New-Object Vim.VirtualMachineRelocateSpec
with
New-Object VMware.Vim.VirtualMachineRelocateSpec
try replacing
New-Object Vim.VirtualMachineRelocateSpec
with
New-Object VMware.Vim.VirtualMachineRelocateSpec
[http://www.vmwareadmins.com | http://www.vmwareadmins.com]
I just went through the execution portion and replaced vim.blahblah with VMware.vim and it eliminated the bulk of the errors.
Now I think I have an entirely different problem:
Exception calling "RelocateVM_Task" with "2" argument(s): "Invalid / explicitly disabled state operation is invoked." At D:\thick2thin.ps1:92 char:36 + $task = $viewVM.RelocateVM_Task( <<<< $specRelocate, $priority) Get-VIObjectByVIView : The argument cannot be null or empty. At D:\thick2thin.ps1:94 char:25 + Get-VIObjectByVIView <<<< $task | Wait-Task | Out-Null
yes, you replaced to many of them 🙂
[http://www.vmwareadmins.com | http://www.vmwareadmins.com]
Here's what I have now for that section:
# Get the view of the VM we're moving $viewVM = Get-View -VIObject $objVM # Remove the preceding "HostSystem-" from the VM's Host's ID, giving us the Host's MOID $strTargetHost = ($objVM.host.id).replace('HostSystem-','') # Create a task specification for the relocation $specRelocate = New-Object VMware.Vim.VirtualMachineRelocateSpec # Add the target datastore to the specification using the MOID $specRelocate.datastore = New-Object VMware.Vim.ManagedObjectReference $specRelocate.datastore.type = "Datastore" $specRelocate.datastore.value = $strTargetDatastore # Add the host to the specification using the MOID $specRelocate.host = New-Object VMware.Vim.ManagedObjectReference $specRelocate.host.type = "HostSystem" $specRelocate.host.value = $strTargetHost # This is the specification property that makes the disk Thin Provisioned $specRelocate.transform = "sparse" # Create the task where the previously specified task is applied to the view of the target VM $task = $viewVM.RelocateVM_Task($specRelocate, $priority) # Start the task, and wait for it to complete before continuing Get-VIObjectByVIView $task | Wait-Task | Out-Null }
Which has eliminated most of the errors except for this:
Migrating machine 000019-WinXPPro, to Thin Provisioned format on datastore VMLAB 02 Exception calling "RelocateVM_Task" with "2" argument(s): "Invalid / explicitly disabled state operation is invoked." At D:\thick2thin.ps1:92 char:36 + $task = $viewVM.RelocateVM_Task( <<<< $specRelocate, $priority) Get-VIObjectByVIView : The argument cannot be null or empty. At D:\thick2thin.ps1:94 char:25 + Get-VIObjectByVIView <<<< $task | Wait-Task | Out-Null
Hi slayer199,
Do you see this error with any VM or just with particular one?
For some reason the SVMotion operation is temporary disabled for that VM. Maybe there is something other that happens with that VM in the same time ( VUM operation for example) and it blocks the SVMotion task. Can you try your script with other VM?
Vitali,
PowerCLI team
That's what I was afraid of. It's a Lab Manager 4.01 implementation but the Lab Manager agents are disabled on both hosts (testing in a lab) so it's affecting all of the VMs in our test lab.
Background: We have over 200 VMs in our prod Lab Manager 3.0 environment (planning the upgrade now). They are using 13 TB of allocated disk space (thick) though they are using less than 7TB actual. We could save a ton of $$$ by thin-provisioning the VMs (which is supposed to be supported by 4.01) and recovering 5TB of disk. I know I'm not the only person that has a need or desire to do this, I was just trying to find a way around undeploying the VMs, updating the configurations to thin, and then redeploying.
I'm just trying to find a creative way around the limitations of the Lab Manager product especially in regards to sprawl (don't even get me started on reporting). Running sVmotion in the back end would be one way. Since it's a test lab, I don't really care if I blow it up...hence the testing. I just need to figure out a way around whatever is blocking the storage vMotion.
Any other hints?
Hi again,
I've just found that Lab Manager as default uses linked clones, e.g. all VMs cloned from one VM template share the template's disk and have their own delta disk which should be relatively small. Of course it is possible to choose "full clone". Do you use the full clone operation?
Vitali
I didn't think about that aspect. Good call. I'd have to check production, but that's probably correct. I'm guessing the only thing we can do (other than changing the templates to thin-provisioned which is planned) is to go through all of the configurations to make them thin-provisioned.
So to sum up, there's no easy way to convert them to thin-provisioned, correct?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi slayer,
I think that LM has really good reason to disable relocate operation...
Did you try to look into Lab manager community? There are several posts that concern thin provisioning.
Vitali
I contacted VMware support and discovered there's no easy way to do this. To change the templates to thin-provisioning would break the configurations. The only solution is to clone the templates, upgrade the cloned templates VMTools and virtual hardware, and convert the disk to thin-provisioned. This is not a good solution and another weakness in the Lab Manager product.
I guess we'll have to wait until the new product is released in the fall and convert to the new one.