1

I've searched and tried different variations of commands, but none seems to work for me. I believe I have also gone through all the SO references on this.

Tried all the ways here: https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

My goal is to run an exe that already resides on a remote VM. I have created and released a Console application (in C#). Its more like a double click, I simply double click on the exe file and it does it tasks. I've tried different ways of using them, but I can't seem to get what I want.

Methods tried:

  1. Invoke-VMScript
  2. Win32_Process.Create()
  3. Invoke-Command
  4. Start-Process
  5. psexec
  6. Invoke-WmiMethod
  7. [diagnostics.process]::start

Few of the tests:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { D:\Path\to\ConsoleAppName.exe }

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\ConsoleAppName.exe }

#This ps1 file will in turn contain command to run the exe file. Tried same with bat as well
Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ConsoleAppPS.ps1" } 

Invoke-Command -ComputerName VMName -ScriptBlock { & "D:\Path\to\ConsoleAppName.exe" }
#Tried $session = New-PSSession -computername VMNAME -credential $cred

#Tried different other ways with no luck.

Can anyone please help me with this. If there is any other alternative I would really love to try that. Thanks in advance

UPDATE 1: The two latest stuff that I can come up with is something like putting the execution command into a bat/ps1 file (on the VM) and than calling that bat/ps1 file from the Invoke-VMScript cmdlet.

Scenario 1: I create a ps1 file containing the below script:

Set-Location "D:\folder\path\to\ConsoleAppName"
$cmd = "cmd /c `"ConsoleAppName.exe"`"

Write-Output "Script execution started"
Invoke-Expression -Command $cmd >> TestOutput.txt
Write-Output "Script executed"

And than I call the below cmdlet

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ps1file.ps1" }

Scenario 2: I create a ps1 file just like above and a bat file containing: PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ps1file.ps1". Than use the Invoke-VMScript like below:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\bat\file.bat }
#I can use Invoke-Item instead of cmd /c but no luck

Scenario 3: psexec way

D:\NewFolder\PAExec\psexec.exe \\VMIP -u $(User_ADMIN) -p "$(ADMIN_PASSWORD)" -i -s cmd /c D:\folder\path\to\ConsoleAppName\ConsoleAppName.exe

Error from psexec:

PsExec v2.34 - Execute processes remotely
...
The handle is invalid.
Connecting to xx.xxx.xx.xxx...

Couldn't access xx.xxx.xx.xxx:
Connecting to xx.xxx.xx.xxx...
##[error]Cmd.exe exited with code '6'. 
Raj
  • 199
  • 1
  • 12
  • Please include any error output for each of your attempted solutions. Make sure you [enable PowerShell remoting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enable-psremoting?view=powershell-7.2), for `Invoke-Command` to work. – zett42 Jun 10 '22 at 12:43
  • @zett42: unfortunately there are no errors with Invoke-VMScript cmdlet. I have also enabled PS remoting but still i get error saying cannot connect to the VMName with Invoke-Command. Strangest thing is no errors are there. – Raj Jun 10 '22 at 17:05
  • Maybe you have suppressed errors? Try `Invoke-Command -EA Stop ...` so it causes a terminating error on failure. You could also write some output from the script block to see if it actually runs: `-ScriptBlock { & "D:\Path\to\ConsoleAppName.exe"; "Test Output" }` – zett42 Jun 10 '22 at 17:26
  • @zett42: As you pointed out, it doesnt actually runs. More strange is it doesn't give any errors. – Raj Jun 13 '22 at 04:57

0 Answers0