VMware Cloud Community
zenivox
Hot Shot
Hot Shot
Jump to solution

Select-Object output to a variable

Hello,

anytime I fill a variable with the output of a calculated property and then I use that variable later on in a script, instead of simply getting the value of the variable I also get @{} around it. Is there a way to avoid that? This is one line for instance:

$path2 = Get-VM $vm.VM | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} | select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }}

If I run the one liner it works. But if I use the $path2 variable somewhere else I get it wrapped as a hash table. Do I have to force text manipulation each time?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Use the ExpandProperty parameter to only get the value

$path2 = Get-VM $vm.VM | %{

    $_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}

} |

Select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }} |

Select -ExpandProperty Path2


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Use the ExpandProperty parameter to only get the value

$path2 = Get-VM $vm.VM | %{

    $_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}

} |

Select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }} |

Select -ExpandProperty Path2


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

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

many thanks indeed, I used the ExpandProperty but in the first Select operation and of course it didn't work, now I know why!! Cheers!

Reply
0 Kudos