srajsenthil
Contributor
Contributor

Clone using Templates on a host

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
ScottDriver42
Enthusiast
Enthusiast

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.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42

View solution in original post

3 Replies
ScottDriver42
Enthusiast
Enthusiast

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.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
LucD
Leadership
Leadership

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

0 Kudos
srajsenthil
Contributor
Contributor

Thanks for the reply. Your solution did worked and marking it answered.

0 Kudos