By: Brenton Blawat
I recently was working with a customer who required a custom script to launch an application. Essentially, the Remote Desktop “PowerShell Launching Script” had to do prerequisite checks to ensure the user could run the application (files, folders, HKCU registry entries, etc). After the prereq’s were installed, the application automatically launched using start-process.
Note: If your application requires file type associations, proceed to Custom Application with File Associations.
If not, continue below:
To achieve this, we add “PowerShell” as the “Published” RemoteApp program, update the icon using Set-rdRemoteApp CMDlet to that of the custom application, and set the command line arguments to –executionpolicy RemoteSigned –WindowStyle Hidden –f “c:\PathToPS1File.ps1”.
Example code below:
- Logon to Connection Broker
- Open Server Manager
- Select Remote Desktop Services > Expand Collections > Select the Session Collection of choice.
- Under RemoteApp Programs > Select Tasks > Select Publish Remote App Programs
- Wait for RDS to scan the session hosts.
- Select Windows PowerShell > Select Next > Select Publish
- Right-click on PowerShell > Select Edit Properties
- Rename the RemoteApp Program Name:
- Select Parameters > Select Always use the following command-line parameters: Set it to:
- –ExecutionPolicy RemoteSigned –WindowStyle Hidden –f “c:\path_to_ps1file.ps1”
- Select Apply > Select OK.
Update the Remote App Icon
- Open an Administrator PowerShell Window (this step will fail if it’s not launch as administrator)
- Type: Import-Module RemoteDesktop
- Type: Get-RDRemoteApp –alias “powershell” | set-rdremoteapp –iconpath “c:\path_to_exe.exe” –IconIndex 0
In doing this, the application appears to be the “Custom Application”, but actually invokes a hidden PowerShell script to launch the application. This works great in instances where you don’t care about file type associations.
Custom Application with File Associations
The above works great if you don’t need any file type associations for your application. As we had found out, the custom application has custom file type associations. As of this posting, there isn’t a way to register new file type associations and you could only modify the existing associations for an exe. Since we used PowerShell above, we could only see those associations available to PowerShell.
As a result, we needed to rethink the strategy. We decided to Publish the actual application executable. We then modified the filepath property to PowerShell, and updated the Command-line parameters as we had prior. We noticed that while we were still launching PowerShell as a wrapper, all of the file type associations remained. We just needed to handle the “shell” in the PowerShell Script and it solved the issue!
Step 1: Here is how we did it:
- Logon to Connection Broker
- Open Server Manager
- Select Remote Desktop Services > Expand Collections > Select the Session Collection of choice.
- Under RemoteApp Programs > Select Tasks > Select Publish Remote App Programs
- Wait for RDS to scan the session hosts.
- Select the Custom Application> Select Next > Select Publish
Step 2: Modify the FilePath For the Application:
- Open an Administrator PowerShell Window (this step will fail if it’s not launch as administrator)
- Type: Import-Module RemoteDesktop
- Type:
Get-RDRemoteApp –alias “Custom Application Name” | set-rdremoteapp –FilePath “c:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe”
- After this step, you must close out of Server Manager and reopen it. The interface won’t reflect the new settings until you restart Server Manager.
- Open Server Manager
- Select Remote Desktop Services > Expand Collections > Select the Session Collection of choice.
- Right-click on Custom Application > Select Edit Properties
- Select Parameters > Select Always use the following command-line parameters:
- Set it to:
–ExecutionPolicy RemoteSigned –WindowStyle Hidden –f “c:\path_to_ps1file.ps1”
- Select File Type Associations > Check All of the File Type Associations you want.
- Select Apply > Select OK.
Modify your PowerShell Wrapper / Launching Script
- Open the Registry and Browse to HKEY_CLASSES_ROOT
- Browse for your File type association.
- From your application instance Browse to Shell > Open > Command
- Take note of the command.
- (alternatively you can search this area for your Application Exe)
- If your command leverages just “%1”, then the exe accepts the file path as the first parameter.
- If your command leverages any other language like /open, /file, /spawn etc, take note of the syntax.
- Open your PowerShell Script
- As the very first line after the synopsis add:
Param($filename)
- For launching the application, insert the following code:
# If $filename has content, launch it with parameters
If ($filename) { start-process c:\path_to_your_app.exe –ArgumentList $filename }
# If $filename doesn’t have any content, launch it without parameters
If (!$filename) { start-process c:\path_to_your_app.exe }
If you have additional parameters, you can do the following instead:
# insert Parameters into variable
$filename = “/open $filename“
# If $filename has content, launch it with parameters
If ($filename) { start-process c:\path_to_your_app.exe –ArgumentList $filename }
# If $filename doesn’t have any content, launch it without parameters
If (!$filename) { start-process c:\path_to_your_app.exe }