I can't find the option in powershell to startup a vm after its been cloned. My script build mulitple VM's but they don't automatically power-up any clues?
This is a problem if a person creates a large number of VM's and they have post clone instructions to follow after boot
Below is a part of the script (hopfully there is an option that I am missing)
{New-VM -VMHost $DVMHost -Name $Name -Location $Folder -OSCustomizationSpec (Get-OSCustomizationSpec Win2k3x86)`
-Description $Description -Datastore $lun -Template (Get-Template $Template) -RunAsync -ErrorAction "SilentlyContinue" ;break}
Afaik there is no poweron option on the New-Vm cmdlet but you can do
New-VM ..... | Start-VM
The beauty of PowerShell, combining cmdlets through the pipeline. You create the new guest and pipe the virtual machine object to the next cmdlet which starts the guest.
It's that easy
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes thanks but I am not sure this works if you are using -RunAsync?; as I am creating mass amounts of vm's at a time and I use this to offload to the VC instead of waiting. I'll test to see if I can still use start-vm
No you're right, missed that -RunAsync parameter.
An alternative in that case is to use the Get-Task cmdlet and monitor till the task is finished.
You can then do
Get-Vm -Name $Name | Start-VM
to start the guest once the async task is finished.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes I've not figured out how to figure out when the buid is complete so I can execute the command. I could suggest they check the VC to see if the builds are done but this script was suppose to be a script to avoid doing that? Any suggestion on how to check if the builds are complete from powershell?
This is one way of doing this.
I use a hash table to store the task-Id and the name of the VM.
In a loop I check if the status of task has changed and if it says "Success" I start the VM.
$esxName = <ESX-name> $modelVm = "ModelVm" $newVmList = "NewVm1","NewVm2" $taskTab = @{} # Create all the VMs specified in $newVmList foreach($Name in $newVmList){ $taskTab[http://(New-VM -VM (Get-VM $modelVm) -Name $Name -VMHost (Get-VMHost -Name $esxName) -RunAsync).Id|http://(New-VM -VM (Get-VM $modelVm) -Name $Name -VMHost (Get-VMHost -Name $esxName) -RunAsync).Id] = $Name } # Start each VM that is completed $runningTasks = $taskTab.Count while($runningTasks -gt 0){ Get-Task | % { if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){ Get-VM $taskTab[http://$_.Id|http://$_.Id] | Start-VM -WhatIf $taskTab.Remove($_.Id) $runningTasks-- } elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){ $taskTab.Remove($_.Id) $runningTasks-- } } Start-Sleep -Seconds 15 }
This sample shows a clone of 2 new guests from a model VM.
But you could easily replace the New-VM cmdlet by your New-Vm cmdlet.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
getting an error on this line ? (Not sure how to sort it out)
$taskTab[http://(New-VM -VM (Get-VM $modelVm) -Name $Name -VMHost (Get-VMHost -Name $esxName) -RunAsync).Id|http://(New-VM -VM (Get-VM $modelVm) -Name $Name -VMHost (Get-VMHost -Name $esxName) -RunAsync).Id] = $Name
- Missing or invalid array index expression
That's the forum SW that has problems with square brackets.
I posted the same script on my blog as About Async tasks, the Get-Task cmdlet and a hash table.
Copy the source from there.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference