How do I remotely start a VM on VMWare Workstation using Powershell?

2.3k Views Asked by At

I have VMWare Workstation and beginner's knowledge of PowerShell.

I've created a script that successfully starts a VM on my local machine by using the vmrun tool. However, if I run it through a remote session, nothing happens. Any idea why?

Get-PSSession | Remove-PSSession
$VMHostMachine | New-PSSession 

$rs = Get-PSSession  # testing using localhost

Write-Debug ("Now starting VM on host: " + $VMHostMachine)
$script = {param($VMImagePath, $VMConsolePath);
    $QuotedVMPath = "`"{0}`"" -f $VMImagePath
    $Result = Start-Process -FilePath $VMConsolePath -ArgumentList "-T", "ws", "start", $QuotedVMPath -Wait -PassThru -NoNewWindow
}

Invoke-Command -Session $rs -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath

Invoke-Command works if I remove the session parameter:

Invoke-Command -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath

I have a similar script that successfully reverts to a snapshot on my localhost through a PSSession, so why is starting a VM giving me trouble?

1

There are 1 best solutions below

0
On

Does it work if you "enter-pssession" and then try to invoke?

I'm assuming it's "not working" for you here, because you invoke commands through PSSession where there are no visible desktops / gui-session, and you are expecting the VMWare workstation to appear? The receiving end is a service.

You could check if the process is running VMWare with Get-Process.

To do what you want to achieve here you could take a look at PsExec, which allows to start applications into an active session.

https://technet.microsoft.com/en-US/sysinternals/bb897553.aspx

Note the "-i" parameter on PSExec.