Powershell: Out-Gridview -passthru switch

My co-worker saw this on twitter and sent this to me:

get-vm | out-gridview -passthru | open-vmconsolewindow

Upon first glance, I was thinking: "Well, that'll kill my machine trying to open 10000 VM consoles". But upon closer inspection, it works a bit differently than I first thought.

Basically what the above does is this:
  1. Gathers a list of VMs
  2. Outputs that list into gridview
    1. Clicking OK on the highlighted row, passes thru the highlighted 'object' to the next command.
      • You can also select multiple rows and have them all pass to the next cmdlet.  
      • I assume this will only work if the next cmdlet knows how to handle multiple objects.
      • In the case of Open-VMConsoleWindow, it does work.
  3. Open-VMConsoleWindow takes in the highlighted object and runs.
I didn't realize out-gridview had a passthru option, but now that I do, that can certainly make interactive scripts easier when I want someone to make a selection.  Obviously when I want to be lazy.

Basically, you can make use of it anytime you want prompt for a choice and just insert out-gridview in-between your source and destination.
So using the example above, lets say I want to stop a specific process, but I don't know the name of it.  Normally, I would get the list of processes by using get-process, record its name or PID, etc. then capture it a variable or just pipe it to stop-process.  Out-Gridview makes this a one step process:

Get-Process | Out-GridView -passthru | Stop-Process 

Now I just highlight the process I want to kill (granted you can do this in task manager, but hopefully you see the concept), and click OK.


Comments

Popular posts from this blog

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

MacOS: AnyConnect VPN client was unable to successfully verify the IP forwarding table modifications.

vCenter: Cluster Skip Quickstart Workflow via API