Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running remote GUI app in Powershell

Tags:

We have a custom comonent that wraps some of the functionality of powershell so it can be used frim BizTalk 2006. For most operations (checking a file path, copy or move a file) this works fine. However we have the need to fire up a GUI app remotely to do some processing. The component itself handles the connection to the remote box, all we have to do is set some parameters and then tell it to execute a command

Start-Process -FilePath "path to exe" -ArgumentList "arguments for exe" -WorkingDirectory "workingdir for exe" 

The issue is this: If we run this from a powershell command line on the box itself, this works fine. However when we fire it up remotely (from BizTalk, from a test harness, even using a remote Powershell command line and connection via Start-PSSession), this app will run briefly then exit without actually doing anything. My suspicion is that because the exe in question requires a GUI to load to run the process, that it is this that is causing an issue. I've tried everything I can think of, including -NoNewWindow and -WindowStyle but to no avail. Any help getting this working would be very much appreciated.

Note: We do not have access to the source for the application we are trying to run as it is an older win32 application, and no batch or command line version of this application has been supplied.

like image 608
Stefan John Park Avatar asked Aug 25 '10 21:08

Stefan John Park


People also ask

Can you launch a GUI based application from PowerShell?

Building PowerShell Scripts' GUI Using Visual StudioYou can use Visual Studio with a WPF (Windows Presentation Foundation) as a simple PowerShell GUI builder. Download and install Visual Studio Community 2019 version. WPF is a part of the . NET Framework that can be used to visualize user interfaces in Windows apps.

How do I create a GUI in PowerShell?

To start creating the GUI, open up Visual Studio and create a new project. Once Visual Studio is open, click on File (1) –> New (2) –> Project (3). Under the New Project window, choose Visual C# (1), select WPF App (. NET Framework) (2), change the name to PoshGUI-sample (3) and click OK.


2 Answers

Using standard PowerShell methods (WinRM, WMI) you can't launch applications with GUI. The only solution I know about is to use PsExec from SysInternals (or similar tools). It can launch applications which present GUI to the user. Your command line will look like this:

& ".\psexec" -accepteula -i "\\computername" -u "domain\username" -p "password" "command line" 
  • -accepteula — silently accept EULA.
  • -i — allow GUI.

Other solutions are more hacky, including remote adding of tasks to the scheduler.

like image 122
Athari Avatar answered Oct 11 '22 20:10

Athari


Since I came across this recently, here is my solution using Discord's suggestion of adding a remote task. I preferred the "hack" over having to setup a separate tool.

function Start-Process-Active {     param     (         [System.Management.Automation.Runspaces.PSSession]$Session,         [string]$Executable,         [string]$Argument,         [string]$WorkingDirectory,         [string]$UserID,         [switch]$Verbose = $false      )      if (($Session -eq $null) -or ($Session.Availability -ne [System.Management.Automation.Runspaces.RunspaceAvailability]::Available))     {         $Session.Availability         throw [System.Exception] "Session is not availabile"     }      Invoke-Command -Session $Session -ArgumentList $Executable,$Argument,$WorkingDirectory,$UserID -ScriptBlock {         param($Executable, $Argument, $WorkingDirectory, $UserID)         $action = New-ScheduledTaskAction -Execute $Executable -Argument $Argument -WorkingDirectory $WorkingDirectory         $principal = New-ScheduledTaskPrincipal -userid $UserID         $task = New-ScheduledTask -Action $action -Principal $principal         $taskname = "_StartProcessActiveTask"         try          {             $registeredTask = Get-ScheduledTask $taskname -ErrorAction SilentlyContinue         }          catch          {             $registeredTask = $null         }         if ($registeredTask)         {             Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false         }         $registeredTask = Register-ScheduledTask $taskname -InputObject $task          Start-ScheduledTask -InputObject $registeredTask          Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false     }  } 
like image 20
Ben Avatar answered Oct 11 '22 21:10

Ben