VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

Single ESXi Inventory Report Powercli error

Hi guys,

Am new here, Good to see you...

Today I tried a script which will fetch RAM , CPU , Local Datastore details of a single host. But I couldnt make it

Here is what I did

#######################

$esxi = Connect-VIServer -Server 10.0.0.20 -User 'root' -Password 'welcome'

Get-VMHost -Name $esxi | get-datastore |

Select name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N=”FreespaceGB”;E={“{0:n2}” -f ($_.FreespaceGB)}},

    CapacityGB,

    @{N=”Provisioned”;E={“{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}} | 

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture

#########################

In the output am getting the datastore values correctly, but not the RAM and CPU , It shows zero

If I skip the datastore part, Then I get the RAM and CPU values correctly

So guys, help me what I am missing , or provide me a alternate script of my requirement.

Thanks 🙂

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your Select works on the object in the pipeline, which in this case is a Datastore object.

You can use the PipelineVariable for retaining an object earlier in the pipeline construct.

$esxi = Connect-VIServer -Server 10.0.0.20 -User 'root' -Password 'welcome'

Get-VMHost -Name $esxi -PipelineVariable esx | Get-Datastore |

Select @{N='VMHost';E={$esx.Name}},Name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($esx.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($esx.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($esx.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($esx.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($esx.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N=”FreespaceGB”;E={{0:n2}-f ($_.FreespaceGB)}},

    CapacityGB,

    @{N=”Provisioned”;E={{0:n2}-f ($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}} |

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture


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

View solution in original post

11 Replies
LucD
Leadership
Leadership
Jump to solution

Your Select works on the object in the pipeline, which in this case is a Datastore object.

You can use the PipelineVariable for retaining an object earlier in the pipeline construct.

$esxi = Connect-VIServer -Server 10.0.0.20 -User 'root' -Password 'welcome'

Get-VMHost -Name $esxi -PipelineVariable esx | Get-Datastore |

Select @{N='VMHost';E={$esx.Name}},Name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($esx.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($esx.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($esx.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($esx.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($esx.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N=”FreespaceGB”;E={{0:n2}-f ($_.FreespaceGB)}},

    CapacityGB,

    @{N=”Provisioned”;E={{0:n2}-f ($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}} |

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LuCD,

I was expecting your reply....You are doing great here....Could saw your solution and comments in many posts....

And as always your above reply worked for me....Thanks 🙂

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LuCD,

Is there any way to input multiple hosts in the above script and get it as a single csv report.

Note: the hosts are not in cluster or in same datacenter.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can provide that list of multiple ESXi node names in many ways.

The following is a hard-coded list

# Specific list of ESXi node names

$esxNames = 'esx1','esx2','esx3','esx4','esx5'


Connect-VIServer -Server 10.0.0.20 -User 'root' -Password 'welcome' | Out-Null


Get-VMHost -Name $esxNames -PipelineVariable esx | Get-Datastore |

Select @{N='VMHost';E={$esx.Name}},Name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($esx.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($esx.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($esx.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($esx.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($esx.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N=”FreespaceGB”;E={{0:n2}-f ($_.FreespaceGB)}},

    CapacityGB,

    @{N=”Provisioned”;E={{0:n2}-f ($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}} |

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture

But you can also read that list of names from a file.
Just replace the first 2 lines

# List of ESXi node names from a .txt file (1 name per line)

$esxNames = Get-Content -Path .\esxnames.txt


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LuCD,

Its working.....

Am having two doubts....

1)I have to disconnect the hosts which were connected, Disconnect-VIServer $esxNames -Confirm:$false  adding this command in the end is ok or I have to use for loop?

2) The above script gives datastore values in GB, How to convert it as TB?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) Are you connecting to a vCenter?
In that case, you don't have to connect/disconnect from each ESXi node.

2) Just divide by 1KB

    @{N='FreespaceTB';E={'{0:n2}' -f ($_.FreespaceGB/1KB)}},

    @{N='CapacityTB';E={'{0:n2}' -f ($_.CapacityGB/1KB)}},

    @{N='Provisioned';E={'{0:n2}' -f (($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))/1KB)}} |


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No LuCD...There is no vcenter....
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

And also am going to run the script in task scheduler so there is a need for connection
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you could do the connect and disconnect for ESXi nodes at once, provided they all have the same user/password combination.

# List of ESXi node names from a .txt file (1 name per line)

$esxNames = Get-Content -Path .\esxnames.txt


Connect-VIServer -Server $esxNames -User 'root' -Password 'welcome' | Out-Null


Get-VMHost -Name $esxNames -PipelineVariable esx | Get-Datastore |

Select @{N='VMHost';E={$esx.Name}},Name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($esx.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($esx.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($esx.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($esx.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($esx.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N='FreespaceTB';E={'{0:n2}' -f ($_.FreespaceGB/1KB)}},

    @{N='CapacityTB';E={'{0:n2}' -f ($_.CapacityGB/1KB)}},

    @{N='Provisioned';E={'{0:n2}' -f (($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))/1KB)}} |

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture


Disconnect-VIServer -Server $esxNames -Confirm:$false


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Yes LuCD, the credentials are same.

By the above script I get the datastore info of both internal disk and external storage array. Is it possible to exclude the external storage array?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

# List of ESXi node names from a .txt file (1 name per line)

$esxNames = Get-Content -Path .\esxnames.txt


Connect-VIServer -Server $esxNames -User 'root' -Password 'welcome' | Out-Null


Get-VMHost -Name $esxNames -PipelineVariable esx | Get-Datastore |

where{-not $_.ExtensionData.Summary.MultipleHostAccess} |

Select @{N='VMHost';E={$esx.Name}},Name, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

   @{N='CPU GHz Used';E={[math]::Round($esx.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($esx.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($esx.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($esx.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($esx.MemoryTotalGB - $_.MemoryUsageGB),2)}},

    @{N='FreespaceTB';E={'{0:n2}' -f ($_.FreespaceGB/1KB)}},

    @{N='CapacityTB';E={'{0:n2}' -f ($_.CapacityGB/1KB)}},

    @{N='Provisioned';E={'{0:n2}' -f (($_.CapacityGB$_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))/1KB)}} |

Export-Csv -Path d:\Report.csv -NoTypeInformation -UseCulture


Disconnect-VIServer -Server $esxNames -Confirm:$false


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