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:
- Invoke-VMScript
- Win32_Process.Create()
- Invoke-Command
- Start-Process
- psexec
- Invoke-WmiMethod
- [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'.