Automation

 View Only
  • 1.  Get VMhost of a VM

    Posted Dec 26, 2012 07:39 PM

    Hello,

    I'm trying to get the host that a list of VMs are on, but I'd also like the VM name printed out so I know what VM I'm looking at.

    For example, if I run 'get-vm | get-vmhost', I get the output below. I can see the name of the host, but I don't know what the actual VM is. Is there a way to include the VM name in this output as well?

    Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Ver
                                                                                                                   sio
                                                                                                                     n
    ----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- ---
    204.154.130.151      Connected       PoweredOn       8         126       18080          19.774          23.987 0.0
    labesxi1.hq.tella... Connected       PoweredOn      12        2057       36696          45.648          95.987 1.0
    usnvwvf5esx2.hq.t... Connected       PoweredOn      16        1051       29776          42.050         127.987 0.0
    usnvwvf5esx3.hq.t... Connected       PoweredOn      24        1786       47856          95.862         255.987 0.0

    Thanks,

    Scott



  • 2.  RE: Get VMhost of a VM
    Best Answer

    Posted Dec 26, 2012 07:44 PM

    You can do

    Get-VM | Select @{N="VM";E={$_.Name}},@{N="ESX";E={$_.Host.Name}}

    Are there any other properties you want to list ?



  • 3.  RE: Get VMhost of a VM

    Posted Dec 26, 2012 08:42 PM

    Wow! That was awesome!!!

    Can you tell me what that syntax is? N=VM, E={ }. Just trying to understand how that command works.

    Thanks a lot!!!



  • 4.  RE: Get VMhost of a VM

    Posted Dec 26, 2012 09:59 PM

    Sure, the line has 2 cmdlets, where the objects returned by Get-VM (VirtualMachine objects) are passed through the pipeline to the Select-Object cmdlet.

    On the Select-Object cmdlet I use a calculated property (the expression between @{...})

    Such a calculated property consists of 2 parts, first the name (N) we want to give to the property, second the expression (E) where we calculate the value of the property.

    In the VirtualMachine object the Host property holds the VMHost object, and in there we find the Name of the host.

    Did that make it any clearer, or are you more confused now ?



  • 5.  RE: Get VMhost of a VM

    Posted Dec 27, 2012 02:07 PM

    I think that makes sense :smileyhappy:

    At least more than it did before.

    Thanks a lot for your help Luc.