Automation

 View Only
  • 1.  Get custom field information

    Broadcom Employee
    Posted Jul 16, 2008 02:55 PM

    For each of the VM's we have a custom field added in the notes section of VC called Created and the value is the date we created the VM, is there anyway to get a list of the machine name and the value of this custom field in a list type format similar to the below:

    Get-VM | Select-Object -property "Name","PowerState","Description","NumCPU","MemoryMB"



  • 2.  RE: Get custom field information
    Best Answer

    Posted Jul 16, 2008 04:06 PM

    Here's how to read it:

    $vm = get-vm fooVM
    $vm.CustomFields.Item("keyName")

    Here's how to do it in a table:

    56# Get-VM adama* | Select-Object -property "Name","PowerState","Description","NumCPU","Memor
    yMB", { $_.CustomFields.Item("dhcp") }
    
    
    Name                          : Adama_win2k3
    PowerState                    : PoweredOn
    Description                   :
    NumCpu                        : 1
    MemoryMB                      : 596
    $_.CustomFields.Item("dhcp")  : 1
    
    
    
    57# Get-VM adama* | Select-Object -property "Name","PowerState","Description","NumCPU","Memor
    yMB", { $_.CustomFields.Item("dhcp") } | ft
    
    Name                 PowerState Description             NumCpu       MemoryMB $_.CustomField
                                                                                  s.Item("dhcp")
    
    ----                 ---------- -----------             ------       -------- --------------
    Adama_win2k3          PoweredOn                              1            596 1
    

    To make it prettier, add a hashtable object holding the name of the new property and the expression used to generate it. Like so:

    $CustomFieldExp = @ { Name = "My Key"; Expr = { $_.CustomFields.Item("keyName") } }
    Get-VM | Select-Object -property "Name","PowerState","Description","NumCPU","MemoryMB", $CustomFieldExp

    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 3.  RE: Get custom field information

    Broadcom Employee
    Posted Jul 17, 2008 07:19 AM

    As always a correct answer from halr9000, cant wait for the book !