3

I am using VirtualBox and I want to be able to obtain a list of all Virtual Machines using a powershell script (after clicking a button on the gui).

I know you can use the command

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" list vms

in cmd/batch to list all VMs but I can't seem to figure out how to display the output with my powershell script. That is why I wanted to create a new powershell window which executes this command so I have a list of VMs.

I tried doing that but nothing happens at all:

& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms

Thanks in advance.

best regards, John

J. Doe
  • 43
  • 5
  • 1
    You might want to have a look at [this question about output capturing](https://stackoverflow.com/questions/8097354/how-do-i-capture-the-output-into-a-variable-from-an-external-process-in-powershe) (not related with running command but with 2nd part of your question). – Robert Dyjas Jun 28 '18 at 11:16
  • Maybe try using a ListView to make it look a little less "vibrant". Added a function in there and an example for you. https://pastebin.com/9RaKU0bk – Drew Jun 28 '18 at 12:40

2 Answers2

4

Have you tried:

cmd /c 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms

You should also be able to do this:

powershell.exe "& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms"

Edit

If you want a new window, use this:

Start-Process powerShell.exe "& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms; pause"

The pause will wait for you to hit enter before the new window exits.

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Your code works if I just have the command. But my command goes into a Button and nothing happens. This is my code: `$Button4.Add_Click({ cmd /c 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms}) ` – J. Doe Jun 28 '18 at 11:17
  • Okay, is there part of the code missing? Are you trying to open a new powershell window and display the results on that? the command is getting executed but you are not receiving it. Even if it opens a new window, it will exit immediately after it executes. – Sid Jun 28 '18 at 11:34
  • Yes I am trying to open a new powershell window since I only have the GUI and I need something to list the VMs in. Here's the complete code: https://pastebin.com/UE8Yw5cV – J. Doe Jun 28 '18 at 11:44
  • Try this: `Start-Process powerShell.exe "& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms; pause"` – Sid Jun 28 '18 at 12:06
  • I tried it on your code. It is working for me. However, it returns blank coz i dont have any vms in virtual box. Hoping it will be different for you. – Sid Jun 28 '18 at 12:07
  • `Start-Process powerShell.exe "& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms; pause"` worked for me. Thank you very much! – J. Doe Jun 28 '18 at 12:21
0

When running a PowerShell scriptblock from an event (e.g. click) handler, the output of the scriptblock is simply discarded. What you need to do is capture the output in a global variable as follows:

$Button4.Add_Click({$global:result =  & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms})

This assigns the output to a global variable $global:result which you can then use in other tasks.

Yes I am trying to open a new powershell window

Do you really want to open a window or do you just want to run a background (asynchronous) job? If you just want a background job, then you should look at Start-Job. One of the problems with using Start-Process is that you won't get the results back but you can with PowerShell jobs. If you use Start-Job, the code would look like:

$Button4.Add_Click({$global:lvmjob =  Start-Job { & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms}})

Then elsewhere in your code, you can receive the job results by doing

$data = receive-job -Job $global:lvmjob
Bruce Payette
  • 2,511
  • 10
  • 8
  • Thank you for your detailed answer but is it possible to get the output into a combobox? – J. Doe Jun 29 '18 at 08:14
  • Sure - instead of assigning it to a value, use the `AddRange()` method on the `Items` property on the combobox as in: `$button.add_Click{$combobox.Items.AddRange((& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list vms))` where `$combobox` is a variable pointing to your combobox control. – Bruce Payette Jun 29 '18 at 23:55