Scott Forsyth
Scott Forsyth

Reputation: 1194

PowerShell Format-List from different objects in the chain

How do I output properties from parent objects in a piped chain?

For example:

get-vm | get-vmdisk | forEach {Get-VHDInfo $_.DiskPath} | Select -Property Path, ParentPath, VM.VMElementName

Basically it's the VM.VMElementName that I'm wondering about (I made up that syntax). It's not the immediate object (which would be from Get-VHDInfo) but the grandparent (from get-vm) that I want to get a value for.

Upvotes: 2

Views: 720

Answers (1)

Shay Levy
Shay Levy

Reputation: 126782

You cannot get values from upstream cmdlets the way you want to. You can use foreach-object right after calling get-vm and save the value in a variable, then assign it back to the select-object as a new calculated property.

get-vm | foreach-object{    
    $VMElementName = $_.VMElementName   
    get-vmdisk | forEach {Get-VHDInfo $_.DiskPath} | Select Path,ParentPath,@{Name='VMElementName';Expression={$VMElementName}}
} 

Upvotes: 5

Related Questions