Calling Ps1 file from a shortcut

Copper Contributor

we are trying to launch a PS1 file from a shortcut from an MSIX package. The script is calling another batch file. We have used the following configurations in config.json file and the script which is running the batch file is present under AppLauncher.ps1
{
"id": "PSFLAUNCHERTwo",
"executable": "C:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
"stopOnScriptError": false,
"startScript":{
"scriptExecutionMode": "-ExecutionPolicy Unrestricted",
"scriptPath": "AppLauncher.ps1",
"scriptArguments": "",
"runInVirtualEnvironment": true,
"showWindow": false,
"waitForScriptToFinish": true,
"runOnce": false

}

However, The script is not working at all and not launching the batch file. can you let me know how we can achieve launching the Ps1 script from a shortcut.

 

5 Replies

Your Json looks confused to me. The purpose of the "start script" is to run a powershell script prior to launching the primary application listed in the executable field.

So if there is a main app to run and you burred it in the ps1 file, you can remove the main app launch from there and replace the "executable" value with the relative path to that exe. The remaining portions of the script would then run prior to launching the main app exe. The launcher knows how to find the powershell.exe. Your PS1 file, along with the script wrapper ps1 file should be placed in the same folder as the PsfLauncher2.exe.

 

Look at Example 4 in the readme at the bottom of this page: MSIX-PackageSupportFramework/PsfLauncher at develop · TimMangan/MSIX-PackageSupportFramework (github... 

Hi Tim,
The problem with our application is that only when we call the exe directly it does not launch. only when we call it via a batch file or in a command shell or via a PS1 it works. so we needed to call the batch file inside a PS1 file. so our primary application itself is called in the startscript and since there was no exe file we have just pointed it to powershell.exe.

@sidharthverma 

I would suggest understanding/solving the original problem first.  There are many possible causes for the primary app exe not starting.  For example, in the json above the "workingdirectory" field is not set.  Troubleshooting this starts with either using PsfTracing/Monitor or even just a Process Monitor approach.

 

If you leave powershell.exe as the "executable" field, you must then add the StartMenuShellLaunchScriptwrapper.ps1 to the package and in the "arguments" field put in the relative location of that file followed by arguments that this script requires, which would be your script file relative path.  Because PowerShell likes to pop out of the container, the StartMenuShellLaunchScriptwrapper will use the local file association to then back inject a second powershell command into the container.  This requires that the client OS be 21H1 or above, and the end-user will see the powershell window.

 

If you want the powershell window hidden than the script support might be needed.  But this will require a dummy exe for the "executable" field that does nothing and returns.  PsfLauncher will run whatever is in the script section first.  Leave showWindow false and waitForScriptToFinish true.  Make sure that your script doesn't return until after the app does. Include StartingScriptWrapper.ps1 in the package for this to work.  If you are calling a bat file from within your ps1 that becomes problematic as the cmd process is likely to pop out of the container.  You'll need to test by leaving showWindow true and add in some sleep calls so you can see what is going on.

Hi Tim,

Thanks for your inputs. I did troubleshoot the application and was able to find the root cause of the issue. It is basically calling the batch file and creating the temporary file under the working directory of the shortcut. Now since in MSIX it needs permission we need to put file redirection for this folder. However, the file redirection is not working. Below is the config file that we have used:

{
"enableReportError": true,
"applications": [

{
"id": "PSFLAUNCHERTwo",
"executable": "VFS\\ProgramFilesX86\\Simulia\\CAE\\2021SE\\win_b64\\resources\\install\\se\\launcher.bat",
"arguments": "\"cae||pause\"",
"workingDirectory": "VFS\\AppVPackageDrive\\temp"
}
],
"processes" : [

{
"executable": ".*",
"fixups": [

{

"dll": "FileRedirectionFixup32.dll",
"config":
{
"redirectedPaths": {
"packageRelative": [

{ "base": "VFS\\ProgramFilesX86\\Simulia\\CAE\\2021SE\\win_b64\\resources\\install\\se",
"patterns": [

".*\\.txt"
]

},

{
"base": "VFS\\AppVPackageDrive\\temp",
"patterns": [
".*\\.txt"
]

}
]
}
}

}

]

}

]
}

We need to redirect any txt file creation under the folder - VFS\\ProgramFilesX86\\Simulia\\CAE\\2021SE\\win_b64\\resources\\install\\se and VFS\\AppVPackageDrive\\temp using PSF. however, by using the above config.json file it is not working. we have also used ".*" in place of ".txt" in the above json but its not working. Is there any chance that file redirection may not work in some cases.

@sidharthverma 

Although using PackageRelative might seem to make sense, when the paths to be matched are under VFS folders you need to configure them under the KnownFolders (unless you know that the app will absolutely use the package relative path reference instead of the "native" path that would map at a lower layer of the OS).

 

Your syntax for the RegEx pattern was fine as is.  I have a tendency to use more greedy matching and use exclusions (as seen in the example below), but you can add the base (without VFS\ProgramFilesX86) and patterns under the known folder cases too.

 


"redirectedPaths": {
   "packageRelative": [
      {
         "base": "",
         "patterns": [
             ".*\\.[eE][xX][eE]$",
             ".*\\.[dD][lL][lL]$",
             ".*\\.[tT][lL][bB]$",
             ".*\\.[oO][cC][xX]$",
             ".*\\.[cC][oO][mM]$",
            ".*\\.[fF][oO][nN]$",
            ".*\\.[tT][tT][cC]$",
            ".*\\.[tT][tT][fF]$"
         ],
        "isExclusion": true
      },
      {
         "base": "",
         "patterns": [
             ".*"
         ]
      }
   ],

   knownFolders": [
   {
      "id": "ProgramFilesX86",
      "relativePaths": [
         {
            "base": "",
            "patterns": [
                  ".*"
            ]
         }

      ]

   },

   {
      "id": "LocalAppData",
      "relativePaths": [
         {
            "base": "",
            "patterns": [
               ".*"
            ]
         }
      ]
   }