VMware Cloud Community
andrewrichardso
Contributor
Contributor
Jump to solution

Beginner's question - get host version and sort by hostname and version

Hi,

I'm trying to get the host version of all my hosts in vCenter and then sort the output into a table by hostname and version, but I'm pretty new to powershell and have zero programming background so i can't figure it out.

What I have got thus far is this;

get-vmhost | % { (Get-View $_.ID).Config.Product } | sort-object fullname | format-table FullName

This will get the software version of all my hosts and sort it into a list by software version (fullname). However because get-vmhost | % { (Get-View $_.ID).Config.Product } doesn't include the hostname in its output, I can't figure out how to include the host name into my output.

Can anyone help?

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can do something like this

get-vmhost | Get-View | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Build | format-table

On the Select-Object cmdlet you can use hash literals which will allow you to execute complex expressions.

If you want the ouput ordered by hostname, you can do

get-vmhost | Get-View | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Name | format-table

Or like this (which is a bit faster)

Get-View -ViewType HostSystem | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Build | format-table

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

You can do something like this

get-vmhost | Get-View | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Build | format-table

On the Select-Object cmdlet you can use hash literals which will allow you to execute complex expressions.

If you want the ouput ordered by hostname, you can do

get-vmhost | Get-View | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Name | format-table

Or like this (which is a bit faster)

Get-View -ViewType HostSystem | select Name,@{N="Build";E={$_.Config.Product.FullName}} | `
sort-object -Property Build | format-table

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
andrewrichardso
Contributor
Contributor
Jump to solution

Hi Luc,

Thanks a lot. I have no idea how it works, but it does and that's the main thing!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at the Calculated properties post.

It explains much better then I could do, the concept of the calculated property.

____________

Blog: LucD notes

Twitter: lucd22


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

andrewrichardso
Contributor
Contributor
Jump to solution

Hi Luc,

Right I think I understand that a little better now. One question I still have is how you knew the options to put in the calculated property.

Using the technet article you linked as an example, how would you know that "creationtime" was valid property to use?

Or using your script as the example, how did you find "Config.Product.FullName"?

Is there a way you can automatically output all the valid properties for something? Or is there a document that lists them all?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are 2 replies to your question.

1) You could have found out about the "creationtime" by piping the object toGet-Member. This cmdlet shows you all the available properties (and methods) for a specific object.

Get-ChildItem "C:\" | Get-Member

2) The "issue" with Get-Member is that it doesn't show you the nested properties.

In the case of build information, you could have followed the properties of the HostSystem object in the VMware vSphere API Reference Documentation

But this requires at least some experience with these SDK objects.

Another option here is to use a script that dumps the nested properties and their values.

See for example Carter's excellent Get-ViewProperties function in his Exploring your objects post.

____________

Blog: LucD notes

Twitter: lucd22


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

andrewrichardso
Contributor
Contributor
Jump to solution

Thanks for those links, it's helpful for trying to learn this stuff when you are starting from square one.

0 Kudos
nikol
VMware Employee
VMware Employee
Jump to solution

Hi there,

note that VMHost objects in PowerCLI have "Build", "Version", "ApiVersion" etc. properties so you can use them instead of getting the view of the VM hosts.

Regards,

Irina

0 Kudos
yashatrevm
Enthusiast
Enthusiast
Jump to solution

@LucD  When I try to change the property name or label of ( N= or L=) anything apart from "Build" for e.g "Build Number", it fails to sort the list based on build number. I am not able to understand the reason, could you please explain why? 

Thank you.  

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try the new propery name with single quotes, i.e. 'Build Number'?


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

0 Kudos
yashatrevm
Enthusiast
Enthusiast
Jump to solution

Yes, I did try that, but still fails to sort the list based on build number. Only works when the name or label is "Build"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

How did you specify the Sort-Object?

Sort-Object -Property 'Build Number'


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

0 Kudos
yashatrevm
Enthusiast
Enthusiast
Jump to solution

My bad! was trying to do it the wrong way.  Thanks a TON for this help! 

0 Kudos