VMware Cloud Community
snteran
Contributor
Contributor
Jump to solution

Learn proper commands, script to get a list of all VMHost and their assocated vm's.

I am new to powercli and have been using the discussion forum to help find information on learning more about our VM Datacenter and clusters.

currently I'm working on getting a report that will list Each VMHost and their associated VM's utilizing get-view:

$clusterName = “Prod_Cluster”

$vmHost = foreach($cluster in Get-Cluster -Name $clusterName){

(Get-VMhost $vmHost | Get-View).VM | Get-VIObjectByVIView

} | export-csv c:\reports\prodCluster.csv

Now this errors out and i'm not sure how best to troubleshoot to get my needed results.

Error states: Get-VMHost : Cannot validate argument on parameter 'Name'.

At the end of the day, i'd like to get a report that would show me a total CPU / Memory per cluster and then the breakout by Host of CPU / Memory.

Name             PowerState Num CPUs MemoryGB
----             ---------- -------- --------
Server1   PoweredOn  4    8.000
Server2PoweredOn  4    12.000
Server3PoweredOn  2   

8.000

If possible show subtotals per VMHost and then a grand total at the bottom.

Hoping to pull a report that will help us prepare a benchmark to determine an acceptable level of available resources before we look at adding some more host.

Thank you for any assistance and patience regarding my beginner questions as I learn.

1 Solution

Accepted Solutions
sneddo
Hot Shot
Hot Shot
Jump to solution

OK, so one thing to avoid here are reserved variables. The $host variable relates to your Powershell console host, so avoid it Smiley Happy

Secondly, you are not using your host variable in your loop. At the very least you should be doing (Get-View $vhost).VM | Get-VIObjectByVIView (note the replaced variable to avoid $host)

Next, doing (Get-View $vhost).VM | Get-VIObjectByVIView is actually a bit redundant. Retrieving a host view (which contains the "VM" attribute containing references to the VMs), and then getting the VM object via Get-VIObjectByVIView is a few steps more than you need. You can simply do $vhost | Get-VM as Get-VM will take a host object as a pipeline variable.

So putting that all together, we get:

$clusterName = “Intel_Prod_Cluster”

$vmHosts = Get-Cluster -Name $clusterName | Get-VMHost

foreach ($vmhost in $vmHosts)

{

    $vmhost | Get-VM

}

And finally... the "power" in Powershell really comes from the pipeline, I'd suggest doing a bit of searching and see how you can start to string cmdlets together to save you some work.

Other usefull cmdlets:

Get-Member - pipe a variable to this to see what methods and properties are valid (e.g. $vmhost | Get-Member)

Get-Help <cmdlet> - to get help for a particular command

Get-command - will return a list of cmdlets for when you can't quite remember e.g. get cluster commands: Get-command *-Cluster

View solution in original post

0 Kudos
4 Replies
sneddo
Hot Shot
Hot Shot
Jump to solution

OK, let's break this up a bit. I'm not going to give you the answer just yet because I think you will get more out of solving it yourself Smiley Happy

To get the hosts in a cluster:

Get-Cluster -Name $clusterName | Get-VMHost

To get VMs per host, you can pipe the host object (like the ones returned above) to the Get-VM cmdlet

$VMHost | Get-VM

You can also get all VMs in a cluster in a similar way:

Get-Cluster -Name $clusterName | Get-VM

You can also do some funky things with the Measure-Object cmdlet, like finding a total of some attributes:

$VMHost | Get-VM | Measure-Object -sum MemoryGB, NumCPU

The syntax of a foreach loop is as follows:

foreach ($item in $array)

{

   // Do stuff

}

$item is set to an instance of each item in the array as it loops.

See how you go and we can help out if you need.

snteran
Contributor
Contributor
Jump to solution

First I'd like to say thank you very much for your method.  This is exactly what I was hoping, a way to learn and try to develop the right procedural thinking to get my desired results.  There are plenty of examples but the problem is I don't really understand some of the commands.  I have to break things down to their lowest common denominator and build them up.

So my thoughts on this initial task:

  • declare a variable that holds the clusterName - $clusterName
  • $vmhost will hold an array of all vmhost names in $clusterName
  • the foreach loop will create an instance called $host for each vmhost in $vmhost as it goes through the array
  • my Do Stuff statement - (get-view).VM | Get-VIObjectByVIView

In truth, I found the comment "(get-view).VM | Get-VIObjectByVIView" and was curious about the (get-view).VM part, is "VM" a property of get-view?  I looked at the reference guide for get-view and didn't see VM.

So my revised statement:

$clusterName = “Intel_Prod_Cluster”

$vmHost = Get-Cluster -Name $clusterName | Get-VMHost

foreach ($host in $vmHost)

{

   (Get-View).VM | Get-VIObjectByVIView

}

This did not work, as you are probably aware:

Cannot overwrite variable Host because it is read-only or constant.

I'll keep after it and would appreciate any other awesome notes.

Cheers,

0 Kudos
sneddo
Hot Shot
Hot Shot
Jump to solution

OK, so one thing to avoid here are reserved variables. The $host variable relates to your Powershell console host, so avoid it Smiley Happy

Secondly, you are not using your host variable in your loop. At the very least you should be doing (Get-View $vhost).VM | Get-VIObjectByVIView (note the replaced variable to avoid $host)

Next, doing (Get-View $vhost).VM | Get-VIObjectByVIView is actually a bit redundant. Retrieving a host view (which contains the "VM" attribute containing references to the VMs), and then getting the VM object via Get-VIObjectByVIView is a few steps more than you need. You can simply do $vhost | Get-VM as Get-VM will take a host object as a pipeline variable.

So putting that all together, we get:

$clusterName = “Intel_Prod_Cluster”

$vmHosts = Get-Cluster -Name $clusterName | Get-VMHost

foreach ($vmhost in $vmHosts)

{

    $vmhost | Get-VM

}

And finally... the "power" in Powershell really comes from the pipeline, I'd suggest doing a bit of searching and see how you can start to string cmdlets together to save you some work.

Other usefull cmdlets:

Get-Member - pipe a variable to this to see what methods and properties are valid (e.g. $vmhost | Get-Member)

Get-Help <cmdlet> - to get help for a particular command

Get-command - will return a list of cmdlets for when you can't quite remember e.g. get cluster commands: Get-command *-Cluster

0 Kudos
snteran
Contributor
Contributor
Jump to solution

Thank you again for your help.  I will continue to work on my output and presentation.  I've just signed up for a year's subscription to Pluralsight so hopefully I'll learn a bit more about PS/PowerCLI and other web tools to hopefully build a nice console for reporting.

i did come across one of your scripts -  vCheck-Tools.ps1!!!!!!


That is very impressive, I can only hope to get to understand half of what is going on in that script.  I'll keep plugging away and congrats on your skill level, very impressive indeed!


Cheers

0 Kudos