Reputation: 43
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
Upvotes: 3
Views: 1871
Reputation: 2639
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
Upvotes: 0
Reputation: 2676
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.
Upvotes: 4