VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

list powered on vm's only in script

Given the portion of script I have below, is there a way to edit the 'Num of VMs' to be only powered on VMs, i'm not sure what to add at the end of $ESXHost.vm.count

Thanks, Dan

$Report = @()

$ESXHosts = Get-VMHost | Sort Name | Get-View

ForEach ($ESXHost in $ESXHosts)

{

$ReportObj = "" | Select Name, "VMotion Enabled", "Connection State", "CPU Used (MHz)", "Total CPU (MHz)", "% CPU Used", "RAM Used (GB)", "Total RAM (GB)", "% RAM Used", "Num of VMs"

$ReportObj.Name = $ESXHost.Name

$ReportObj."VMotion Enabled" = $ESXHost.summary.config.vmotionenabled

$ReportObj."Connection State" = $ESXHost.summary.runtime.connectionstate

$ReportObj."CPU Used (MHz)" = $ESXHost.summary.quickStats.overallCpuUsage

$ReportObj."Total CPU (MHz)" = (($ESXHost.summary.hardware.NumCpuCores)*($ESXHost.summary.hardware.CpuMhz))

$ReportObj."% CPU Used" = "{0:F1}" -f (100(($ESXHost.summary.quickStats.overallCpuUsage)/(($ESXHost.summary.hardware.NumCpuCores)($ESXHost.summary.hardware.CpuMhz))))

$ReportObj."RAM Used (GB)" = "" -f ($ESXHost.summary.quickStats.overallMemoryUsage/1024)

$ReportObj."Total RAM (GB)" = "" -f ($ESXHost.Summary.Hardware.MemorySize/1GB)

$ReportObj."% RAM Used" = "" -f (100*(($ESXHost.summary.quickStats.overallMemoryUsage/1024)/($ESXHost.Summary.Hardware.MemorySize/1GB)))

$ReportObj."Num of VMs" = $ESXHost.vm.count

$Report += $ReportObj

}

$Report | ConvertTo-Html -title "Advanced Host Information" -body "<H4>Advanced Host Information</H4>" | Out-File -Append $filelocation

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Nice solution Allan, but I think it should say

$ReportObj."Num of VMs" = (Get-vmhost $ESXHost.name | get-vm | where {$_.Powerstate -eq "PoweredON"} | Measure-Object).Count


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

View solution in original post

Reply
0 Kudos
4 Replies
AllanChristians
Enthusiast
Enthusiast
Jump to solution

Hi,

Nice script, just change the line:

$ReportObj."Num of VMs" = $ESXHost.vm.count

To:

$ReportObj."Num of VMs" = Get-vmhost $ESXHost.name | get-vm | where {$_.Powerstate -eq "PoweredON"}

Hope it helps.

/Allan

/Allan http://doitsmarter.blogspot.com
LucD
Leadership
Leadership
Jump to solution

Nice solution Allan, but I think it should say

$ReportObj."Num of VMs" = (Get-vmhost $ESXHost.name | get-vm | where {$_.Powerstate -eq "PoweredON"} | Measure-Object).Count


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

Reply
0 Kudos
AllanChristians
Enthusiast
Enthusiast
Jump to solution

Hi,

As always u are right LucD

/Allan

/Allan http://doitsmarter.blogspot.com
Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Thanks both of you for your help, the script is really coming on and it wouldn't be was it not for your input so thank you.

Dan

Reply
0 Kudos