VMware Cloud Community
cody_bunch
Hot Shot
Hot Shot
Jump to solution

Getting over the hump, custom properties...

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

-Cody Bunch http://professionalvmware.com
0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

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

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
5 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

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

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
cody_bunch
Hot Shot
Hot Shot
Jump to solution

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

-Cody Bunch

vExpert, VCP VI3

-Cody Bunch http://professionalvmware.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

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

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
cody_bunch
Hot Shot
Hot Shot
Jump to solution

That does it! Now to keep that in a place where it can be remembered Smiley Happy

Thanks again!

-Cody Bunch

vExpert, VCP VI3

-Cody Bunch http://professionalvmware.com
0 Kudos
glnsize
Contributor
Contributor
Jump to solution

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

0 Kudos