Sunday, December 2, 2012

PowerCLI - Deploying VM from template with customization spec

Reproducing an issue recently required me to deploy 20 VM's across 4 hosts that each had 5 datastores (4 VM's on each datastore, one from each host). The datastores were named nfs-[1-5]. Once I had the template VM created with the tools I needed and the customization spec, I was able to deploy the VM's across the hosts/datastores with the following PowerCLI code:
1..5 | Foreach { New-VM -vmhost 192.168.10.244 -Name Win2k8-1-$_ -Template Win2k8Template -Datastore nfs-$_ -OSCustomizationSpec TM-WIN2k8 }
1..5 | Foreach { New-VM -vmhost 192.168.10.245 -Name Win2k8-2-$_ -Template Win2k8Template -Datastore nfs-$_ -OSCustomizationSpec TM-WIN2k8 }
1..5 | Foreach { New-VM -vmhost 192.168.10.246 -Name Win2k8-3-$_ -Template Win2k8Template -Datastore nfs-$_ -OSCustomizationSpec TM-WIN2k8 }
1..5 | Foreach { New-VM -vmhost 192.168.10.247 -Name Win2k8-4-$_ -Template Win2k8Template -Datastore nfs-$_ -OSCustomizationSpec TM-WIN2k8 }
Each VM was using ~20GB, and the whole deployment took approximately an hour. I found a variation of the above on another blog (http://virtuallymikebrown.com/2012/02/17/deploy-a-vm-from-template-with-powercli/) where he was able to pipe the New-VM command to Start-VM but for whatever reason this didn't work for me, I was getting the error: "New-VM : 12/2/2012 5:06:34 PM New-VM Operation is not valid due to the current state of the object." I didn't look into this too much and instead just ran the following loop after the above was done:
$VMs = Get-VM -Name "Win2k8-*"
foreach ($vm in $VMs)  {
  Write-Host "Starting " -NoNewLine
  Write-Host $vm.Name 
  Start-VM $vm.Name
}
The final outcome in vCenter looked like:

4 comments:

  1. Nice Script, thanks for sharing

    ReplyDelete
  2. Hi, regarding the most annoying error in the world "New-VM Operation is not valid due to the current state of the object".

    I made a post to the vmware community regarding this error when I were building a script to deploy multiple VM's with multiple different variables.

    http://communities.vmware.com/thread/443716

    I didn't get to the bottom of why this error occurs, but for me it works to restart the powershell/powercli window.

    Hope this help.

    PS. Let me know if you are interested in the script I made.

    --LittleNickey

    ReplyDelete
  3. HI,

    Nice script...
    I just ran into the most annoying error in the world you were talking about. All
    i want to do is create a schedule clone of all vms from one datastore to another datastore.
    All i get is this error tried everything even re-installing powercli, and rebooting host.

    http://communities.vmware.com/thread/452768

    ReplyDelete
  4. Restarting my powershell editor - also worked around this problem for me too. I think that, as I was running and debugging a similar script - I cancelled one of the New-VM jobs ( I may have cancelled in VCenter rather than from the PowerShell gui/editor). Anyhow - since the Editor caches the objects, it has some stale view of the state of Vcenter.

    ReplyDelete