VMware Cloud Community
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Is there a better or more elegant way of doing this?

Hi all!

Is there a better or more elegant way of doing this?

Get-VM *f00* | ForEach-Object{[PSCustomObject]@{ Name =$_.Name; DataCenter = ($_|Get-Datacenter).Name}} | Format-Table -AutoSize

 

Thanks in advance for your help.

-Chris

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Since the more common cmdlets (like Get-VM and Get-Datacenter) are already improved significantly in the latest PowerCLI releases, there is no real gain to be made by using Get-View.

But by changing the order in the pipeline, you can limit the number of calls to Get-Datacenter.
Which, in my tests, resulted in a faster execution (but your mileage may vary).

  Get-Datacenter -PipelineVariable dc | Get-VM -Name foo* |
  ForEach-Object -Process {
    [PSCustomObject]@{ 
      Name = $_.Name
      DataCenter = $dc.Name
    }
  } | Format-Table -AutoSize

  


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean by "elegant"?
Faster?


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

0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I'll take faster, please.

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since the more common cmdlets (like Get-VM and Get-Datacenter) are already improved significantly in the latest PowerCLI releases, there is no real gain to be made by using Get-View.

But by changing the order in the pipeline, you can limit the number of calls to Get-Datacenter.
Which, in my tests, resulted in a faster execution (but your mileage may vary).

  Get-Datacenter -PipelineVariable dc | Get-VM -Name foo* |
  ForEach-Object -Process {
    [PSCustomObject]@{ 
      Name = $_.Name
      DataCenter = $dc.Name
    }
  } | Format-Table -AutoSize

  


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

0 Kudos