VMware Cloud Community
johncraig56
Contributor
Contributor
Jump to solution

Get-template -location clustername

I'm trying to find templates that exist in a cluster - but the -location param on the get-template cmdlet doesn't seem to work.  Any thoughts on a workaround?

FYI - using 4.1 U1 build 332441

Thanks in advance!

[vSphere PowerCLI] C:\scripts> get-template -location MYTEMPLATE
Get-Template : 1/28/2011 12:44:29 PM    Get-Template        Could not find VIContainer with name 'MYTEMPLATE'.
At line:1 char:13
+ get-template <<<<  -location MYTEMPLATE
  + CategoryInfo          : ObjectNotFound: (MYTEMPLATE:String) [Get-Tem
plate], VimException
  + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNo
tFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate
Get-Template : 1/28/2011 12:44:29 PM    Get-Template        VIContainer parameter: Could not find any of the objects specified by name.
At line:1 char:13
+ get-template <<<<  -location MYTEMPLATE
  + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...iner[] Locati
on:RuntimePropertyInfo) [Get-Template], ObnRecordProcessingFailedException
  + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotF
oundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The same principle as your solution but perhaps a bit shorter.

$clusName = "MyCluster"
$esx = Get-Cluster -Name $clusName | Get-VMHost | %{$_.Extensiondata.MoRef} $templates = Get-Template | where {$esx -contains $_.Extensiondata.Runtime.Host}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
FranckRookie
Leadership
Leadership
Jump to solution

Hi John,

"Location" is a vSphere container object such as a datacenter, a cluster or a host. Is it the case for your object "MYTEMPLATE"? Strange name for a cluster...

Hope it helps.

Regards

Franck

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There seems to be indeed a problem with a cluster as the location.

The -Location parameter works perfectly with a folder or a datacenter but apparently not with a cluster, although the documentation for Get-Template says it should.

It fails in both variations

Get-Template -Location ClusterName

Get-Template -Location (Get-Cluster ClusterName)

I wonder if this isn't a documentation error ?

Templates are visible in "blue" folders (the VMs & Templates view) and that view shows datacenters and folders but not clusters.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
johncraig56
Contributor
Contributor
Jump to solution

In my haste to obscure the "real" name of my cluster I did a silly and confusing substitution.  Sorry about that. 🙂

[vSphere PowerCLI] C:\scripts> get-template -location MYCLUSTER
Get-Template : 1/28/2011 12:44:29 PM    Get-Template        Could not find VIContainer with name 'MYCLUSTER'.
At line:1 char:13
+ get-template <<<<  -location MYCLUSTER
  + CategoryInfo          : ObjectNotFound: (MYCLUSTER:String) [Get-Tem
plate], VimException
  + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNo
tFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate
Get-Template : 1/28/2011 12:44:29 PM    Get-Template        VIContainer parameter: Could not find any of the objects specified by name.
At line:1 char:13
+ get-template <<<<  -location MYCLUSTER
  + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...iner[] Locati
on:RuntimePropertyInfo) [Get-Template], ObnRecordProcessingFailedException
  + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotF
oundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate
0 Kudos
johncraig56
Contributor
Contributor
Jump to solution

The following is very ugly but does the job.  Very open to better solutions 🙂

$all_templates = Get-Template
     | Select @{Name = "Template"; Expression = {$_.Name}}, (@{Name = "Name"; Expression = {(Get-View (Get-Template -Name $_.Name
     | Get-View).Summary.Runtime.Host).Name}})

$templates_in_cluster = $all_templates | % {$f = $_; Get-vmhost -location "MYCLUSTER"
     | ? {$f.Name -eq $_.Name} | Select @{n='Template';e={$f.Template}}}
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The same principle as your solution but perhaps a bit shorter.

$clusName = "MyCluster"
$esx = Get-Cluster -Name $clusName | Get-VMHost | %{$_.Extensiondata.MoRef} $templates = Get-Template | where {$esx -contains $_.Extensiondata.Runtime.Host}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
johncraig56
Contributor
Contributor
Jump to solution

Super duper cool!  I'm very new to powershell... awesome tip... Thanks!

0 Kudos