VMware Cloud Community
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Getting Disk Sizes using Where-Object

Hey Guys,

Perhaps im doing something really wrong here, but everytime I run this command, all I get is the VM, not the property:

$VM | Where-Object {($_.Guest.Disks | Where-Object {$_.Path -eq 'C:\'}).CapacityGB}

Any thoughts?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can.

First loop through all the VMs in the $VM variable, then extract the object that represents the C-partition into variable $a.

Once done, you access the properties via the $a variable

$VM | %{

    $a = $_.Guest.Disks | Where-Object {$_.Path -eq 'C:\'}

    $a.CapacityGB

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.

You have to place the elements from $_.Guest.Disks in the pipeline, then filter what gets through with the Where-clause, and finnally extract the property you want

$VM | Select Name,

    @{N='C Size';E={($_.Guest.Disks | Where-Object {$_.Path -eq 'C:\'}).CapacityGB}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

That seems to work a treat, thank you :smileygrin:

I take it there is no way to get the value without having to select the name?

I stored the whole thing into a variable like this:

$a = ($VM | Select Name, @{N='SystemDisk';E={($_.Guest.Disks | Where-Object {$_.Path -eq 'C:\'}).CapacityGB}})

Then retrieve the value like this:

$a.SystemDisk

I was hoping to get the value in one line of code, can it be done?

Thanks again :smileygrin:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can.

First loop through all the VMs in the $VM variable, then extract the object that represents the C-partition into variable $a.

Once done, you access the properties via the $a variable

$VM | %{

    $a = $_.Guest.Disks | Where-Object {$_.Path -eq 'C:\'}

    $a.CapacityGB

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Thats even better than what I wanted :smileygrin:

Now I can pick off parameters from each server with a system disk :smileygrin:

Thanks again LucD :smileygrin:

Your legendary skills make this community such an awesome place to come to for assistance :smileygrin:

0 Kudos