Automation

 View Only
  • 1.  Getting over the hump, custom properties...

    Posted Sep 10, 2009 02:17 PM

    Begin {

    1. Collection of property names for custom object

    $PropertyCol = @(

    "Name", "OS", "Host", "CPU (Count)", "Memory (MB)",

    "VM Extent", "Datastore Name"

    )

    }

    Process {

    if ( $_ -isnot http://VMware.VimAutomation.Client20.VirtualMachineImpl ) { continue }

    $process = "" | Select-Object -Property $PropertyCol

    $process.Name = $_.Name

    $process.OS = $_.Guest.OSFullName

    $process.Host = $_.Host.Name

    $process."CPU (Count)" = $_.NumCpu

    $process."Memory (MB)" = $_.MemoryMB

    $process."VM Extent" = $_ | get-datastore | get-view | %{ $_.Info.Vmfs.Extent }

    $process."Datastore Name" = $_ | get-datastore | Select Name

    Write-Output $process

    }

    The problem I'm having is in the output:

    Name : MyVM

    OS : Microsoft Windows Server 2003, Standard Edition (32-bit)

    Host : esx.domain.com

    CPU (Count) : 1

    Memory (MB) : 4096

    VM Extent : VMware.Vim.HostScsiDiskPartition

    Datastore Name : @{Name=SomeName}

    I need to expand out VM Extent & Datastore name. Thoughts?

    -Cody Bunch

    vExpert, VCP VI3

    http://professionalvmware.com



  • 2.  RE: Getting over the hump, custom properties...

    Broadcom Employee
    Posted Sep 10, 2009 02:43 PM

    Is this what you want ?

    $process."VM Extent" = ($_ | get-datastore | get-view | %{ $_.Info.Vmfs.Extent }).DiskName
    $process."Datastore Name" = ($_ | get-datastore | Select Name).Name

    If you found this information useful, please consider awarding points for Correct or Helpful.

    Alan Renouf

    http://virtu-al.net



  • 3.  RE: Getting over the hump, custom properties...

    Posted Sep 10, 2009 02:59 PM

    Closer, but doesn't work with multiple values returned for said properties. Closer than I was however. Thanks.

    -Cody Bunch

    vExpert, VCP VI3



  • 4.  RE: Getting over the hump, custom properties...
    Best Answer

    Broadcom Employee
    Posted Sep 10, 2009 03:14 PM

    For multiple items, this works (bit I dont like it) someone else may find a nicer way.

    Foreach ($Extent in ($_ | get-datastore | get-view | %{ $_.Info.Vmfs.Extent })){
    		$process."VM Extent" += " $($Extent.DiskName)"
    	}
    
    Foreach ($Datastorename in ($_ | get-datastore | Select Name)){
    		$Process."Datastore Name" += " $($Datastorename.Name)"
    	}

    If you found this information useful, please consider awarding points for Correct or Helpful.

    Alan Renouf

    http://virtu-al.net



  • 5.  RE: Getting over the hump, custom properties...

    Posted Sep 10, 2009 04:38 PM

    That does it! Now to keep that in a place where it can be remembered :smileyhappy:

    Thanks again!

    -Cody Bunch

    vExpert, VCP VI3



  • 6.  RE: Getting over the hump, custom properties...

    Posted Sep 10, 2009 04:48 PM

    V1

    Filter VMQuickReport {
        Where-Object -InputObject $_ -FilterScript {$_.gettype().name -eq "VirtualMachineImpl"} |
        Select-Object Name, NumCpu, MemoryMB,
        @{
            Name='OS'
            Expression={$_.Guest.OSFullName}
        },
        @{
            Name='Host'
            Expression={$_.Host.Name}
        },
        @{
            Name='Extent'
            Expression={
                Get-Datastore -VM $_ | Get-View -Property info |%{
                    $_.Info.VMfs.Extent|%{
                        $_.DiskName
                    }
                }
            }
        },
        @{
            Name='Datastore'
            Expression={
                Get-Datastore -VM $_|%{
                    $_.name
                }
            }
        } 
    }
    

    V2

    Function Get-VMReport {
        [CmdletBinding()]
        Param(
            [parameter(Mandatory=$true,ValueFromPipeline=$true)]
            \[VMware.VimAutomation.Client20.VirtualMachineImpl]
            $VM
        )
        #called up front to avoid multiple calls
        $DataStore = Get-Datastore -VM $VM
    
        Write-Output $VM | 
            Select-object Name, NumCpu, MemoryMB,
        @{
            Name='OS'
            Expression={$_.Guest.OSFullName}
        }, 
        @{
            Name='Host'
            Expression={$_.Host.Name}
        },
        @{
            Name='Datastore'
            Expression={$DataStore.Name}
        },
        @{
            Name='Extent'
            Expression={
                $datastore | 
                    Get-View -Property Info | 
                    Select-Object -ExpandProperty Info |
                    Where-Object {$_.VMFS} |
                    Select-Object -ExpandProperty VMFS |
                    Select-Object -ExpandProperty Extent |
                    Select-Object -ExpandProperty DiskName
            }
        }
    }
    

    The V2 advanced function is about 50% faster. This is due to both the use of Select-Object -Expand vs nested Foreach-object loops. We also get a nice boost by offloading all parameter validation.

    Perhaps not exactly what you were looking for, but I prefer to build stuff I can use from the cmdline.

    ~Glenn