Okay, fed up with my brain + PowerShell. It just doesn't make sense. Why does this work:
Get-Inventory | %{ $type = ($_ | Get-View).GetType().Name $inv = $_ switch ($type) { "ClusterComputeResource" { $inv | select @{name="[MACHINE_NAME]"; expression={$server}}, @{name="[PATH]Id"; expression={$_.Id}}, @{name="[PATH]Name"; expression={$_.Name}}, ... ... } }
But this does not?
Get-VM | Get-Snapshot | %{ $p = $_ $p | select @{name="[MACHINE_NAME]"; expression={$_.VM}}, @{name="[PATH]Id"; expression={$_.Id}}, @{name="[PATH]Name"; expression={$_.Name}}, ... } Specified method is not supported. At :line:20 char:12 + $p | select <<<< @{name="[MACHINE_NAME]"; expression={$_.VM}},
Nor does this:
Get-VM | Get-Snapshot | %{ select @{name="[MACHINE_NAME]"; expression={$_.VM}}, @{name="[PATH]Id"; expression={$_.Id}}, @{name="[PATH]Name"; expression={$_.Name}}, ... } Specified method is not supported. At :line:19 char:7 + select <<<< @{name="[MACHINE_NAME]"; expression={$_.VM}},
Neither does any of the other six ways I've tried to get snapshot data formatted the way I want it. I've tried var assignment and foreach loops. Nope, no good.
I'm an "Enthusiast" on this forum, but I'm not feeling enthusiastic.
Script 3 doesn't work because you didn't "feed" an input object to the Select-Object cmdlet through the pipeline or through the -inputObject parameter.
Script 2 works for me. I think the problem must be in the code you didn't show.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Script 3 doesn't work because you didn't "feed" an input object to the Select-Object cmdlet through the pipeline or through the -inputObject parameter.
Script 2 works for me. I think the problem must be in the code you didn't show.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, thanks (you're awfully patient).
I think I get it, and you are right about #2.
This works well:
Get-VM | Get-Snapshot | ` select @{name="[MACHINE_NAME]"; expression={$_.VM}}, @{name="[PATH]Id"; expression={$_.Id}}, @{name="[PATH]Name"; expression={$_.Name}}, Name, Id, Description, Parent, Children, @{name="Power State"; expression={$_.PowerState}}, Quiesced, Created
What was breaking in #2 was that I was specifying $_. for the non-expression items. If you use the @{name=""; expression={}} syntax it must have the $_. If you don't then it shouldn't.
And, if I understand, you must | something to the select. If the select isn't the first command after a pipe then you are going to need to explicitly pipe into it.
Someday I'll get this (and then be moved to another project where I will forget it again...)