Automating vmrun with PowerShell

2.1k Views Asked by At

I have a PowerShell 2.0 script that I use to clone a target vmware workstation guest using vmrun in a windows host environment. The code to clone a virtual machine executes correctly.

I am now trying to expand on this script to automate more of the process, for example, to check to see if a virtual machine is running, vmrun list, and to stop the virtual machine, vmrun stop [pathToVMXfile] if running.

With a windows command prompt, when I run vmrun list, it returns the following:

Total running VMs: 2
D:\[path]\[name].vmx
E:\[path]\[name].vmx

When I attempt the following in PowerShell, nothing returns. Here is what I have attempted so far. I am expecting an array to return with the values I see when I run from a command prompt.

$vmwareRun = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"

#Original test, also tried with single quotes and no quotes, 
#also tried quoting the -filePath   
Start-Process $vmwareRun -ArgumentList "list" 

#This gives a handle to the process but no return value  
$myProcess = Start-Process $vmwareRun -ArgumentList "list" -PassThru 

#$t is empty
Start-Process $vmwareRun -ArgumentList "list" -OutVariable $t  

#$t is empty, clone command requires the -T ws parameters  
Start-Process $vmwareRun -ArgumentList "-T ws list" -OutVariable $t 

#RedirectStandardOutput creates a file with the expected output, $t is empty
Start-Process -FilePath "$vmwareRun" -ArgumentList $argList -OutVariable $t -RedirectStandardError "C:\testError.log" -RedirectStandardOutput C:\testOut.log"

No matter what I attempt, I do not get any output. What am I missing? Note: Vmrun command line documentation is found here: "https://www.vmware.com/support/developer/vix-api/vix112_vmrun_command.pdf"

Thanks.

2

There are 2 best solutions below

0
On

I found a similar post here: Capturing standard out and error with Start-Process

For whatever reason the powershell solution doesn't send results of the process to the -OutVariable as I expected.

So instead I followed the steps on the page indicated to create a new $pinfo = New-Object System.Diagnostics.ProcessStartInfo. I set the corresponding properties and then started the process using System.Diagnostics.Process and it allows me to output the results to a string variable.

0
On

Just use a batch file doing your vmrun script, start pwsh with startvmrun.bat