I have a host where the vm needs to be provisioned using a template. The issues are some that chosen templates may not always exist for that host. Is there a way to check before cloning, whether the chosen host has ability to use chosen template before starting cloning?
Thanks
Easy to check with a one-liner
if(Get-VMHost HOSTNAME|Get-Template -Name TEMPLATENAME){
new-vm -template #add your options for fun stuff
}else{
write-host "sorry, that template doesn't exist here"
}
Good luck!
I'm new to this forum, so if you've found this helpful, please click the like button.
Easy to check with a one-liner
if(Get-VMHost HOSTNAME|Get-Template -Name TEMPLATENAME){
new-vm -template #add your options for fun stuff
}else{
write-host "sorry, that template doesn't exist here"
}
Good luck!
I'm new to this forum, so if you've found this helpful, please click the like button.
You could do something like this.
The New-VM will only be executed when the Get-Template is successful.
Try{
$template = Get-Template -Name $templateName -ErrorAction Stop
New-VM -Name $vmName -Template $template # more parameters
}
Catch{
Write-Error "Template $($templateName) not found"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the reply. Your solution did worked and marking it answered.