Friday, October 30, 2015

Note to Self: Learn Get-View

Queue Deflation

I’m rather red-faced right now.  I chimed in with a reply in the VMTN VMware vSphere PowerCLI forum and promptly got schooled.

Which is great; these things should happen.

Where’s the Template?

In this thread, VMTN Communities member denjoh44 asked how one would obtain a list of templates along with the name of the vSphere hosts on which they reside.

I haven’t had the need to do this myself, but I wanted to give it a shot. I fired up PowerShell ISE and plugged away:

$Templates = @()
$Hosts = Get-VMHost
foreach ($h in $hosts) {
$HostTemplates = Get-Template -Location $h
foreach ($t in $HostTemplates) {
$TemplateInfo = New-Object PSObject
$TemplateInfo | Add-Member -MemberType NoteProperty Name -Value $t.Name
$TemplateInfo | Add-Member -MemberType NoteProperty Host -Value $h.Name
$Templates += $TemplateInfo
}
}
$Templates
view raw gistfile1.txt hosted with ❤ by GitHub
And it works, which is nice.

I See Your 12 Lines and…

It wasn’t long after that I revisited the thread only to find a much more elegant solution posted by Christophe Calvet:

Get-Template |
Select-Object Name, @{N="Host"; E={(Get-View $_.HostId).Name}} |
Format-Table -AutoSize
view raw gistfile1.txt hosted with ❤ by GitHub
My personality dictates that I should feel silly about my reply.  Christophe nailed it with a one-liner.  Instead, I’m going to focus on my inexperience with Get-View and do some reading.  I'll endeavor to always keep calculated expressions in mind as well.

Time to Learn

Brian Graf has a 3-part blog series on Get-View which I’ve seen before and marked for reading in more detail “later.”  If you are interested in learning more about Get-View, start here:
While you are at it, Christophe has a blog worth checking out at TheCrazyConsultant.com

1 comment:

Brian said...

Todd,
It's great to see you getting interested in Get-View. While it may seem a little overwhelming for most people at first, taking a little time to learn it is definitely super helpful and beneficial to you going forward. I look forward to hearing more about your Get-View experiences!

All the best

Post a Comment