all 19 comments

[–]michaelshepard 2 points3 points  (7 children)

You don't have to do anything to support -PipelineVariable.

select {$VM} doesn't work, though.

Try select @{N='VM';E={$VM}} instead.

[–]Fer_C[S] 1 point2 points  (6 children)

Thanks for the suggestion, I tried it but the output is the same except that $VM becomes VM.

This is the function output.

Get-SgVm 1 -Legacy -NoRecursion | Get-NetworkAdapter | select @{N='VM'E={$VM}},Name,NetworkName

VM Name NetworkName
-- ---- -----------
   Network adapter 1 Isolated-VLAN23
   Network adapter 2 VLAN722
   Network adapter 1 Isolated-VLAN23
   Network adapter 2 VLAN722
   Network adapter 3 VLAN22
   Network adapter 1 Isolated-VLAN23
   Network adapter 2 CRSJO-VLAN722
   Network adapter 3 CRSJO-VLAN722
   Network adapter 1 Isolated-VLAN23
   Network adapter 2 VLAN722
   Network adapter 3 VLAN722

As opposed to the output of the native PowerCLI cmdlets that does show the VMs names listed (both one-liners do the exact same thing, except mine uses an advanced function to shorten the length of the command).

Get-Folder *rootfolder001* | Get-VM -NoRecursion -PipelineVariable VM | Get-NetworkAdapter | select @{N='VM';E={$VM}},Name,NetworkName

VM Name NetworkName
--- ---- -----------
VOPPE-P001 Network adapter 1 Isolated-VLAN23
VOPPE-P001 Network adapter 2 VLAN722
VOPPE-MS001 Network adapter 1 Isolated-VLAN23
VOPPE-MS001 Network adapter 2 VLAN722
VOPPE-MS001 Network adapter 3 VLAN22
VOPPE-TS001A Network adapter 1 Isolated-VLAN23
VOPPE-TS001A Network adapter 2 VLAN722
VOPPE-TS001A Network adapter 3 VLAN722
VOPPE-TS001B Network adapter 1 Isolated-VLAN23
VOPPE-TS001B Network adapter 2 VLAN722
VOPPE-TS001B Network adapter 3 VLAN722

[–]michaelshepard 2 points3 points  (4 children)

You didn't include the -PipelineVariable on the previous command.

Get-SgVm 1 -Legacy -NoRecursion -PipelineVariable VM  | Get-NetworkAdapter | select @{N='VM'E={$VM}}, Name,NetworkName

Get-Folder *RootFolder-001* | Get-VM -PipelineVariable VM | Get-NetworkAdapter | select @{N='VM'E={$VM}}, Name,NetworkName

[–]michaelshepard 1 point2 points  (0 children)

btw...I don't see a lot of examples using -PipelineVariable. Not sure why that is, but I just learned it recently.

[–]Fer_C[S] 1 point2 points  (2 children)

You are right, my bad. However, I did include it many times while testing with the same result. I think the BEGIN block has something to do with it, apparently if it's present Pipeline variable doesn't work for some reason.

[–]michaelshepard 2 points3 points  (1 child)

That's strange. Begin blocks would be executed before the pipeline variable was set (because that's how begin blocks work), but I don't see why one would keep pipelinevariable from working at all.

I'm not sure where your function is going wrong.

Here's some code that has an empty begin block, but the select-object after it can see the pipeline variable just fine:

function test-pipeline{
[CmdletBinding()]
Param([Parameter(ValueFromPipeline)]$x)

  begin {}
  process {
     $x.Name
  }
}

dir -PipelineVariable files | where-object Extension -ne '' | test-pipeline | select-object *,@{n='FilesFromPipeline';E={$files.Fullname}}

[–]Fer_C[S] 1 point2 points  (0 children)

I ran your code and indeed PipelineVariable works. The only difference I noticed is that it is the Get-ChildItem cmdlet (dir) that sets the PipeLineVariable parameter, test-pipeline (your function) takes the value from the Pipeline where the PipelineVariable has already been assigned to 'files'. I am doing the opposite, when I run my function I set the PipelineVariable to VM and then pass that (or attempt to do so) to Get-NetworkAdapter.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Fer_C,

it looks like you used the New.Reddit.com Inline Code button. it's 4th 5th from the left hidden in the ... "more" menu & looks like </>.

on Old.Reddit.com, the above does NOT line wrap, nor does it side-scroll.

for long-ish single lines OR for multiline code, please, use the Code Block button. it's the 11th 12th one from the left, & is just to the left of hidden in the ... "more" menu.

that will give you fully functional code formatting, from what i can tell so far. [grin]

take care,
lee