VMware Cloud Community
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

Power CLI script to get VMhost count per cluster

I am trying to get 2 reports one is VM host count per cluster an another one is all the host name and cluster name as excel output. As like below format

Report 1

Cluster Name        vHost Count

Cluster01                     35

Cluster02                     08

Report 2

HostDatacenterCluster

I tried as below but does not worked as expected.

foreach ($vCenter in $vCenters)

{

        Connect-VIServer $vCenter -Username $vcUser -Password $vcPassword

}

$report = foreach ($vc in $global:DefaultVIServers) {

  Get-Cluster | Select Name, @{N="Host Count"; E={($_ | Get-VMHost).Count}},  @{N="VM Count"; E={($_ | Get-VM).Count}} | export-csv

}

Can anyone help on this.

Thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure I'm following.

You say that you uninstalled the MSI, but the module versions you showed earlier do not come in an MSI.

I'm afraid you have modules from the PSGallery mixed with remnants from the MSI installation.

Some things to check:

- make sure the MSI is uninstalled. Check that folder C:\Program Files (x86)\VMware\Infrastructure\ contains no files from PowerCLI

- remove all PowerCLI modules from the directories listed in $env:PSMOdulePath

Once you are sure all traces of PowerCLI are removed, do a new install from the PSGallery.

This is done through Install-Module, no MSI file


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

View solution in original post

15 Replies
LucD
Leadership
Leadership
Jump to solution

Try with these two snippets.

Since it was not clear if you wanted the ESXi node or the VM count per cluster, I included both.

Get-Cluster |

  Select @{N = 'Cluster Name'; E = {$_.Name}},

@{N = 'vHost Count'; E = {$_.ExtensionData.Host.Count}},

@{N = 'VM Count'; E = {(Get-VM -Location $_).Count}} |

   Export-Csv -Path .\report1.csv -NoTypeInformation -UseCulture

and the 2nd one

Get-Datacenter -PipelineVariable dc |

   Get-Cluster -PipelineVariable cluster |

   Get-VMHost |

  Select @{N = 'Host'; E = {$_.Name}},

   @{N = 'Cluster'; E = {$cluster.Name}},

   @{N = 'Datacenter'; E = {$dc.Name}} |

   Export-Csv -Path .\report2.csv -NoTypeInformation -UseCulture


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

ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

Hi Luc, i am getting error as like Get-Cluster can not find an item at C:\xxxxx\xxx.ps1:16 Char1

Get-Cluster |

+ ~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetCluster

I am running  PSVersion - 5.1.14409.1018 and below PowerCLI modules are loaded.

ModuleType Version    Name

---------- -------    ----

Script     6.7.0.8... VMware.DeployAutomation

Script     6.7.0.8... VMware.ImageBuilder

Manifest   11.0.0.... VMware.PowerCLI

Script     6.7.0.1... VMware.Vim

Script     11.0.0.... VMware.VimAutomation.Cis.Core

Script     11.0.0.... VMware.VimAutomation.Cloud

Script     11.0.0.... VMware.VimAutomation.Common

Script     11.0.0.... VMware.VimAutomation.Core

Script     7.6.0.1... VMware.VimAutomation.HorizonView

Script     10.0.0.... VMware.VimAutomation.License

Script     11.0.0.... VMware.VimAutomation.Nsxt

Script     11.0.0.... VMware.VimAutomation.Sdk

Script     11.0.0.... VMware.VimAutomation.Security

Script     10.0.0.... VMware.VimAutomation.Srm

Script     11.0.0.... VMware.VimAutomation.Storage

Script     1.3.0.0    VMware.VimAutomation.StorageUtili

Script     11.0.0.... VMware.VimAutomation.Vds

Script     11.0.0.... VMware.VimAutomation.Vmc

Script     10.0.0.... VMware.VimAutomation.vROps

Script     6.5.1.7... VMware.VumAutomation

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is the Get-Cluster cmdlet returning anything when you run it on its own?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

Hi Luc, This is error i am getting Get-Cluster

Get-Cluster : 2019-02-27 15:02:42       Get-Cluster             Could not find item C:\Users\.....

At line:1 char:1

+ Get-Cluster

+ ~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetCluster

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have an issue with your profile.

How did you get your original script to run?

That is also using the Get-Cluster cmdlet.


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

I  am using PowerShell, I saved the script as .ps1  and running as administrator.

The same server i have many script and running fine. please check the below script and guide me.

Clear-Host

Add-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue

$vCenters = "Server1","server2","Server3","server4"

$vcUser = "admin"

$vcPassword = "xxxx"

foreach ($vCenter in $vCenters)

{

        Connect-VIServer $vCenter -Username $vcUser -Password $vcPassword

}

$report = foreach ($vc in $global:DefaultVIServers)

Get-Cluster -Name Clustername |

  Select @{N = 'Cluster Name'; E = {$_.Name}},

@{N = 'vHost Count'; E = {$_.ExtensionData.Host.Count}},

@{N = 'VM Count'; E = {(Get-VM -Location $_).Count}} |

   Export-Csv -Path C:\temp\report1.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you show me a screenshot when you run the Get-Cluster cmdlet?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

Please find it

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So how do you run your script with Get-Cluster in there?

And did you already put the Get-Cluster cmdlet on it's own in a .ps1 file and try to run that?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

yes i have included Get-Cluster cmdlet , but i am getting below error. May be i am missing something. it able to connect my Vcenter and giving error as below

At C:\Users\Desktop\Datahost.ps1:16 char:1

+ Get-Cluster |

+ ~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetCluster

Clear-Host

Add-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue

$vCenters = "Server1","server2","Server3","server4"

$vcUser = "admin"

$vcPassword = "xxxx"

foreach ($vCenter in $vCenters)

{

        Connect-VIServer $vCenter -Username $vcUser -Password $vcPassword

}

Get-Cluster -Name Clustername |

  Select @{N = 'Cluster Name'; E = {$_.Name}},

@{N = 'vHost Count'; E = {$_.ExtensionData.Host.Count}},

@{N = 'VM Count'; E = {(Get-VM -Location $_).Count}} |

   Export-Csv -Path C:\temp\report1.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why are you loading a PSSnapin?

In the PowerCLI version you listed earlier there are no PSSnapin anymore.

Did you clean up the old installation that was done through a MSI file?

Do you still see PowerCLI in the Programs and Features applet?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

This is windows 2008 R2 Server and i dont see any PowerCLI programs. only Get-cluster command is not working. when i type Get-Datastorecluster cmdt it working perfect. is there anything to load Get-cluster command.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I have mor eand more the impression that your PowerCLI installation might be corrupted.

Can you remove it completely, and do a fresh install from the PSGallery?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast
Jump to solution

PowerCLI MSI is completely uninstalled. Even i am getting help get-cluster works fine. Not sure where is the problem.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure I'm following.

You say that you uninstalled the MSI, but the module versions you showed earlier do not come in an MSI.

I'm afraid you have modules from the PSGallery mixed with remnants from the MSI installation.

Some things to check:

- make sure the MSI is uninstalled. Check that folder C:\Program Files (x86)\VMware\Infrastructure\ contains no files from PowerCLI

- remove all PowerCLI modules from the directories listed in $env:PSMOdulePath

Once you are sure all traces of PowerCLI are removed, do a new install from the PSGallery.

This is done through Install-Module, no MSI file


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