VMware Cloud Community
SophiaSupport
Contributor
Contributor

Need to get the name of the ESXi server, CPU, # of cores and RAM from Powercli

Hello,

I need to get this information to integrate it and post process it into the product I support but until now I havn't found anything relevant or been able to use it properly... It would be greate if I could also get this information rom the VMs (Virtual Machines) it hosts...

I am making my tests on an ESXi server 6.0 (which was upgraded from 5.0 -> 5.1 -> 5.5) using the root account to connect to it. I connect to it using this command line:

PowerCLI C:\> Connect-VIServer -server **.**.***.*** -user root -password ******

Then run scripts or commands directly.

1- Get information for the host:

This article seems to be the most relevant i found but I get this error message when I try to run the command that (is (was) suppose to work on an ESXi:

PowerCLI C:\> $hardware = Get-VMhost | where {$_.ConnectionState -eq "Connected" } | Get-VMHostHardware

Get-VMHostHardware : 1/27/2017 3:51:55 PM       Get-VMHostHardware              Current license or ESXi version prohibits execution of

the requested operation.

At line:1 char:72

+ ... st | where {$_.ConnectionState -eq "Connected" } | Get-VMHostHardware

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

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

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVMHost

   Hardware

Has this functionality been taken out of the ESXi license since this article? I suppose I can the information in another way siçnce

2- Get information for VMs:

I found this thread on the forum from which I created a script that I ran as a .ps1 from PowerCli, while it was connected to my esxi server:

$result = @()

$vms = Get-view  -ViewType VirtualMachine

foreach ($vm in $vms) {

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name

    $obj | Add-Member -MemberType NoteProperty -Name vCPUs -Value $vm.config.hardware.NumCPU

    $obj | Add-Member -MemberType NoteProperty -Name vSockets -Value ($vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket)

    $obj | Add-Member -MemberType NoteProperty -Name Persocket -Value $vm.config.hardware.NumCoresPerSocket

    $result += $obj

}

$result

Which is great as I get the list of VMs that are hosted along with the # of CPUs and sockets but can someone explain to me what could this information be usefull for "$vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket"? I'm not really sure I understand what it does... All I want is the # of virtual CPUs and the # of virtual cores for each VM.

Also I'd need the RAM  and eventually the OS that is installed etc. How do I get the list of parameters that are available to get this extra information, or do you know of a script that does that already?

Note:

- I'm new to the PowerCli world, I read this document as someone from this forum suggested: http://xtravirt.com/media/2012/11/Beginners-Guide-to-Managing-VMware-using-Powershell.pdf

- just for the background I installed and configured PowerCLI by following parts of these documents:

Back to Basics: Part 1 - Installing PowerCLI - VMware PowerCLI Blog - VMware Blogs

Configuring Your PowerShell to run VMware vSphere PowerCLI cmdlets – GOVARDHAN GUNNALA

Reply
0 Kudos
4 Replies
jpsider
Expert
Expert

for the first issue, Are your ESX server(s) properly licensed? Without licensing you are not allowed to perform powercli calls (same goes for the free version).

Add this line to get the Memor(Ram) in MB:

$obj | Add-Member -MemberType NoteProperty -Name Persocket -Value $vm.config.hardware.MemoryMB

And this for the Guest OS

obj | Add-Member -MemberType NoteProperty -Name Persocket -Value $vm.guest.GuestFullName

Reply
0 Kudos
SophiaSupport
Contributor
Contributor

Well, it doesn't ask me to reimport the free license so I suippose I am.

Thanks for the extra code, i'll try that later and summarize what else I found and tried since I created this thread.

Reply
0 Kudos
jpsider
Expert
Expert

With the free version you are not entitled to use powercli.

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast

SophiaSupport

Your entitlement on PowerCLI with a free version of ESXi will be limited to read-only operations (most Get- cmdlets). You could be encountering a bug, what version of PowerCLI are you using?

For your first issue you can use a script very similar to the one for a VM:

$result = @()

$vmhosts = Get-view  -ViewType hostSystem

foreach ($vmh in $vmhosts) {

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vmh.Name

    $obj | Add-Member -MemberType NoteProperty -Name CPUType -Value $vmh.hardware.CPUPkg[0].Description

    $obj | Add-Member -MemberType NoteProperty -Name NumPhysCPU -Value $vmh.hardware.cpuinfo.NumCpuPackages

    $obj | Add-Member -MemberType NoteProperty -Name NumPhysCores -Value $vmh.hardware.cpuinfo.NumCPUCores

    $obj | Add-Member -MemberType NoteProperty -Name NumThreads -Value $vmh.hardware.cpuinfo.NumCPUThreads

    $obj | Add-Member -MemberType NoteProperty -Name Memory -Value ([Math]::Round($vmh.hardware.MemorySize/1GB)).ToString()

    $result += $obj

}

$result

For your second question, bear in mind that with regards to a virtual machine, the total virtual cores and virtual CPUs are the same thing. The line "$vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket" was in response to a specific need for the number of sockets the vCPUs were divided up into which is important for some OS licensing purposes. For instance you can give a guest running Windows 2008 R2 Standard 16 vcpu's but you must divide that up into some number of cores per socket, if left at the default of 1 core per socket, Windows will only use 4 vcpu due to licensing of the OS, but divided up, the OS will use all cores assigned.

For the additional properties on the VMs. Modify the script as follows and you should be good to go:

$result = @()

$vms = Get-view  -ViewType VirtualMachine

foreach ($vm in $vms) {

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name

    $obj | Add-Member -MemberType NoteProperty -Name vCPUs -Value $vm.config.hardware.NumCPU

    $obj | Add-Member -MemberType NoteProperty -Name vSockets -Value ($vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket)

    $obj | Add-Member -MemberType NoteProperty -Name Persocket -Value $vm.config.hardware.NumCoresPerSocket

    $obj | Add-Member -MemberType NoteProperty -Name Memory -Value $vm.config.hardware.MemoryMB

    $obj | Add-Member -MemberType NoteProperty -Name OS -Value $vm.config.GuestFullName

    $result += $obj

}

$result



Getting the parameters is a bit more difficult, but definitely worth exploring. If you've already gotten PowerShell ISE set up to run powercli commands, then find an add-on that allows you to explore variables. Alternatively you can do this from the command prompt. For instance, from above typing in $vms | Get-member after running the script will show you the methods and properties (and their types) that were returned, but only the top level (Objects are hierarchical and some properties contain other objects with their own properties) you'll see some that have a property type of vmware.vim.something (vmware.vim.VimClient, Vmware.vim.VirtualMachineConfigInformation, etc). These you'll have to dig into, for instance $vms.config | get-member will return methods and properties on the virtual machine config, again with some properties that are objects as well with their own properties.

Cheers!

Reply
0 Kudos