Resolve Font Issues with App-V 5

I recently sequenced a terminal emulation application that utilised a specific set of fonts for each terminal session.

When i first launched the App-V application shortcut after capturing a default installation, I was presented with random characters and symbols – the fonts hadn’t been successfully virtualised.

I checked the AppxManifest.xml file within my package and saw that the fonts had been captured and processed by the App-V font subsystem like so:

<appv:Extension Category="AppV.Fonts">
	<appv:Fonts>
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\bsfascii.fon" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\bsfisol1.fon" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfascii.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfcrm.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfdgint.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\Psfdgln.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfdgmos.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfdspec.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfdsupg.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfdtech.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfisol1.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfisol2.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfisol9.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\Psfkata.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\Psfpc0__.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfpcasc.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfsymbl.ttf" />
		<appv:Font Path="[{ProgramFilesX86}]\Persoft\STEssen\Fonts\psfwproc.ttf" />
	</appv:Fonts>
</appv:Extension>

Of course, this looked ok to me and the font files did exist inside the VFS at the correct location!

When i took a closer look at how the fonts were installed by the installation routine, I noticed shortcut icons in the C:\Windows\Fonts folder as opposed to physical files:

Fonts in the Application Folder
I wondered if this was the reason why the App-V font subsystem wasn’t correctly processing the font?  Since the physical font file didn’t exist inside the Fonts folder itself.

During my next attempt, I lazily tried dragging the fonts into the Fonts folder during the sequencing phase.  I noted again that by default the fonts got copied to the AppData folder and a shortcut was created to the font!  Of course after removing LocalAppData from the exclusions list in App-V, this yielded similar behaviour to my first attempt and the fonts didn’t work in my App-V package!

Fonts Local App Data

My third and final attempt consisted of highlighting all of the font files during the sequencing phase, right-clicking and selecting ‘Install for all users’.

Install Fonts for All UsersYou can see that doing it this way meant that the physical font files were stored in the C:\Windows\Fonts folder directly (no shortcuts!), and the App-V font subsystem then processed them correctly:

Fonts Folder

And the AppxManifest.xml:

<appv:Extension Category="AppV.Fonts">
	<appv:Fonts>
		<appv:Font Path="[{Fonts}]\bsfascii.fon" />
		<appv:Font Path="[{Fonts}]\bsfisol1.fon" />
		<appv:Font Path="[{Fonts}]\psfascii.ttf" />
		<appv:Font Path="[{Fonts}]\psfcrm.ttf" />
		<appv:Font Path="[{Fonts}]\psfdgint.ttf" />
		<appv:Font Path="[{Fonts}]\Psfdgln.ttf" />
		<appv:Font Path="[{Fonts}]\psfdgmos.ttf" />
		<appv:Font Path="[{Fonts}]\psfdspec.ttf" />
		<appv:Font Path="[{Fonts}]\psfdsupg.ttf" />
		<appv:Font Path="[{Fonts}]\psfdtech.ttf" />
		<appv:Font Path="[{Fonts}]\psfisol1.ttf" />
		<appv:Font Path="[{Fonts}]\psfisol2.ttf" />
		<appv:Font Path="[{Fonts}]\psfisol9.ttf" />
		<appv:Font Path="[{Fonts}]\Psfkata.ttf" />
		<appv:Font Path="[{Fonts}]\Psfpc0_.ttf" />
		<appv:Font Path="[{Fonts}]\psfpcasc.ttf" />
		<appv:Font Path="[{Fonts}]\psfsymbl.ttf" />
		<appv:Font Path="[{Fonts}]\psfwproc.ttf" />
	</appv:Fonts>
</appv:Extension>

 

App-V 5 Scripting Context

I carried out some simple App-V 5 scripting tests recently so that I could prove which context the various App-V 5 scripts run in.

I created a very simple App-V 5 package containing Dependency Walker, and added a shortcut to the executable located at [{ProgramFilesX86}]\DependencyWalker\depends.exe.

I also added a script to the Scripts folder called alkane.ps1:

param([string]$message)
add-content "c:\temp\alkane.log" "$message $([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)" -ErrorAction Stop

It’s a very simple PowerShell script that takes some text as an argument (I pass this to the script so that I know which scripting section it is running from).  Each time the script runs it will output the scripting section is has run from, whether the current scripting context is being run as an administrator, and also what account the script is running under.

In my DeploymentConfig.xml file I added the following UserScripts:

  <UserScripts>

      <StartProcess RunInVirtualEnvironment="true">
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts StartProcess"</Arguments>
        <Wait RollbackOnError="true"/>
        <ApplicationId>[{ProgramFilesX86}]\DependencyWalker\depends.exe</ApplicationId>
      </StartProcess>

      <ExitProcess>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts ExitProcess"</Arguments>
        <Wait RollbackOnError="false"/>
        <ApplicationId>[{ProgramFilesX86}]\DependencyWalker\depends.exe</ApplicationId>
      </ExitProcess>
      
      <StartVirtualEnvironment  RunInVirtualEnvironment="true">
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts StartVirtualEnvironment"</Arguments>
        <Wait RollbackOnError="true"/>
      </StartVirtualEnvironment>
    
      <TerminateVirtualEnvironment>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts TerminateVirtualEnvironment"</Arguments>
      <Wait RollbackOnError="false"/>
      </TerminateVirtualEnvironment>

      <PublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts PublishPackage"</Arguments>
        <Wait RollbackOnError="true" Timeout="30"/>
      </PublishPackage>

      <UnpublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig UserScripts UnpublishPackage"</Arguments>
        <Wait RollbackOnError="false" Timeout="30"/>
      </UnpublishPackage>
      
    </UserScripts>

and also the following MachineScripts:

 <MachineScripts>
      <PublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig MachineScripts PublishPackage"</Arguments>
        <Wait RollbackOnError="true" Timeout="30"/>
      </PublishPackage>

      <UnpublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig MachineScripts UnpublishPackage"</Arguments>
        <Wait RollbackOnError="false" Timeout="30"/>
      </UnpublishPackage>
      
      <AddPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig MachineScripts AddPackage"</Arguments>
        <Wait RollbackOnError="true" Timeout="30"/>
      </AddPackage>
      <RemovePackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "DeploymentConfig MachineScripts RemovePackage"</Arguments>
        <Wait RollbackOnError="false" Timeout="60"/>
      </RemovePackage>
  </MachineScripts>

and in the UserConfig.xml file I added the following UserScripts:

 <UserScripts>

      <StartProcess RunInVirtualEnvironment="true">
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts StartProcess"</Arguments>
        <Wait RollbackOnError="true"/>
        <ApplicationId>[{ProgramFilesX86}]\DependencyWalker\depends.exe</ApplicationId>
      </StartProcess>

      <ExitProcess>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts ExitProcess"</Arguments>
        <Wait RollbackOnError="false"/>
        <ApplicationId>[{ProgramFilesX86}]\DependencyWalker\depends.exe</ApplicationId>
      </ExitProcess>
      
      <StartVirtualEnvironment  RunInVirtualEnvironment="true">
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts StartVirtualEnvironment"</Arguments>
        <Wait RollbackOnError="true"/>
      </StartVirtualEnvironment>
    
      <TerminateVirtualEnvironment>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts TerminateVirtualEnvironment"</Arguments>
      <Wait RollbackOnError="false"/>
      </TerminateVirtualEnvironment>

      <PublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts PublishPackage"</Arguments>
        <Wait RollbackOnError="true" Timeout="30"/>
      </PublishPackage>

      <UnpublishPackage>
        <Path>powershell.exe</Path>
        <Arguments>-ExecutionPolicy ByPass -File "[{AppVPackageRoot}]\..\Scripts\alkane.ps1" -message "UserConfig UserScripts UnpublishPackage"</Arguments>
        <Wait RollbackOnError="false" Timeout="30"/>
      </UnpublishPackage>

    </UserScripts>

The Test

In chronological order I added the package, published the package, launched the application, closed the application, unpublished the package and removed the package. I specified various user/deployment XML permutations during add and publish time. The results can be seen below.

The Results

Test 1

  • Apply DeploymentConfig.xml
  • Do NOT apply UserConfig.xml
  • Publish to user
Script File Script Section Script Type Run As Admin Context
DeploymentConfig MachineScripts AddPackage True System
DeploymentConfig UserScripts PublishPackage False User
DeploymentConfig UserScripts StartVirtualEnvironment False User
DeploymentConfig UserScripts StartProcess False User
DeploymentConfig UserScripts ExitProcess False User
DeploymentConfig UserScripts TerminateVirtualEnvironment False User
DeploymentConfig UserScripts UnpublishPackage False User
DeploymentConfig MachineScripts RemovePackage True System

Test 2

  • Apply DeploymentConfig.xml
  • Do NOT apply UserConfig.xml
  • Publish to machine
Script File Script Section Script Type Run As Admin Context
DeploymentConfig MachineScripts AddPackage True System
DeploymentConfig MachineScripts PublishPackage True System
DeploymentConfig UserScripts StartVirtualEnvironment False User
DeploymentConfig UserScripts StartProcess False User
DeploymentConfig UserScripts ExitProcess False User
DeploymentConfig UserScripts TerminateVirtualEnvironment False User
DeploymentConfig MachineScripts UnpublishPackage True System
DeploymentConfig MachineScripts RemovePackage True System

Test 3

  • Do NOT apply DeploymentConfig.xml
  • Apply UserConfig.xml
  • Publish to user
Script File Script Section Script Type Run As Admin Context
UserConfig UserScripts PublishPackage False User
UserConfig UserScripts StartVirtualEnvironment False User
UserConfig UserScripts StartProcess False User
UserConfig UserScripts ExitProcess False User
UserConfig UserScripts TerminateVirtualEnvironment False User
UserConfig UserScripts UnpublishPackage False User

Test 4

  • Do NOT specify DeploymentConfig.xml
  • Apply UserConfig.xml
  • Publish to machine

Error – cannot specify UserConfig.xml if published to machine (-global)

Test 5

  • Apply DeploymentConfig.xml
  • Apply UserConfig.xml
  • Publish to user
Script File Script Section Script Type Run As Admin Context
UserConfig UserScripts PublishPackage False User
UserConfig UserScripts StartVirtualEnvironment False User
UserConfig UserScripts StartProcess False User
UserConfig UserScripts ExitProcess False User
UserConfig UserScripts TerminateVirtualEnvironment False User
UserConfig UserScripts UnpublishPackage False User
DeploymentConfig MachineScripts RemovePackage True System
UserConfig UserScripts PublishPackage False User
UserConfig UserScripts StartVirtualEnvironment False User
UserConfig UserScripts StartProcess False User
UserConfig UserScripts ExitProcess False User
UserConfig UserScripts TerminateVirtualEnvironment False User
UserConfig UserScripts UnpublishPackage False User

Test 6

  • Apply DeploymentConfig.xml
  • Apply UserConfig.xml
  • Publish to machine

Error – cannot specify UserConfig.xml if published to machine

Summary

A summary of these results is depicted in the table below:

Script Script Section Script Type Can Run Inside Virtual Environment? Potentially RUNs Context
DeploymentConfig UserScripts StartProcess Yes Multiple User
DeploymentConfig UserScripts ExitProcess No Multiple User
DeploymentConfig UserScripts StartVirtualEnvironment Yes Multiple User
DeploymentConfig UserScripts TerminateVirtualEnvironment No Multiple User
DeploymentConfig UserScripts PublishPackage No Multiple User/System*
DeploymentConfig UserScripts UnpublishPackage No Multiple User/System*
DeploymentConfig MachineScripts PublishPackage No Multiple System
DeploymentConfig MachineScripts UnpublishPackage No Multiple System
DeploymentConfig MachineScripts AddPackage No Once System
DeploymentConfig MachineScripts RemovePackage No Once System
UserConfig UserScripts StartProcess Yes Multiple User
UserConfig UserScripts ExitProcess No Multiple User
UserConfig UserScripts StartVirtualEnvironment Yes Multiple User
UserConfig UserScripts TerminateVirtualEnvironment No Multiple User
UserConfig UserScripts PublishPackage No Multiple User
UserConfig UserScripts UnpublishPackage No Multiple User

Additional Notes

  • Administrative privileges are required to publish globally, as such globally published packages will be published using the system account
  • You can not specify a UserConfig.xml file when publishing globally
  • The UserScripts in UserConfig.xml will override the UserScripts in DeploymentConfig.xml if both config files are applied
  • Only the StartProcess and StartVirtualEnvironment script types have the option to run inside the virtual environment.
  • Only AddPackage and RemovePackage run once in a package lifecycle.  Every other script type has the potential to run multiple times, including (Un)PublishPackage (for example, if we use Set-AppVClientPackage to specify a new config file we will need to re-publish the package, but not re-add it.)
  • * All UserScripts specified in the DeploymentConfig file will run in a user context when published to a user.  However, when published to a machine all UserScripts specified in the DeploymentConfig file will run in a user context with the exception of PublishPackage and UnpublishPackage, which will run under a system context.

 

App-V 5, Junctions and Path Length Limitations

I was migrating an old application recently which launched Macromedia Director.  It launched fine during sequencing and when installed locally, but when I ran the App-V 5 version i saw the error message:

Unable to load movie playlist. Does the .INI file exist? It must contain a section ‘[Movies]’ with an entry ‘Movie01=Pathname.dir’.

To cut a story short, it turns out the when App-V mounts the application to a path such as C:\ProgramData\App-V\[PackageGUID]\[VersionGUID]\Root\Vocal Pathology_1\Vocal Pathology_I.exe  the path name becomes too long and exceeds the character limit of 127 characters!  See here.

The only way I could think of resolving this via an App-V package would be to spoof it by using a symbolic link, or more specifically a ‘junction’.

Both symbolic links and junctions essentially link one location to another location.  But creating a junction seemed like a better option according to this post on SuperUser – I particularly liked the way that remote users could access the junction from a remote computer.

Our task then, was to create a short path junction that would link to the longer path used by App-V.

When creating a junction on the command line, the syntax is:

mklink /J "[Non Existing Junction Full Path]" "[Existing App-V Full Path]"

It’s important to note that where you are linking from should NOT exist when you run this command, and where you are linking to SHOULD exist. 

We will create a junction with the path of C:\VocalPathology after the package is added (hence at publish time) so that the target path exists, and for cleanliness we will also remove the junction at the time of un-publishing the app.

In the UserConfig.xml file (my apps are published to users and not machines) I added the following code – note that I first rename C:\VocalPathology if it already exists (otherwise creating a junction will fail), then create the junction.  Also note that the token [{AppVPackageRoot}] ultimately resolves to C:\ProgramData\App-V\[PackageGUID]\[VersionGUID]\Root and that our target executable lives in a sub folder of the package root called ‘Vocal Pathology_1’.
.

<UserScripts>
	<PublishPackage>
		<Path>cmd.exe</Path>
		<Arguments>/c move /Y "C:\VocalPathology" "C:\VocalPathology.old" &amp; mklink /J "C:\VocalPathology\" "[{AppVPackageRoot}]\Vocal Pathology_1\"</Arguments>
		<Wait RollbackOnError="false" Timeout="30"/>  		
	</PublishPackage> 		
	<UnpublishPackage>
		<Path>cmd.exe</Path>
		<Arguments>/c rmdir "C:\VocalPathology\"</Arguments> 
		<Wait RollbackOnError="false" Timeout="30"/>  		
	</UnpublishPackage>
</UserScripts>

Now that we have created the junction, we need to amend where the shortcut points to in our UserConfig file! Find the <Shortcuts> section and amend the <Target> element.  Beforehand it points to the the full App-V location since it uses [{AppVPackageRoot}]:

<Target>[{AppVPackageRoot}]\Vocal Pathology_1\Vocal Pathology_I.exe</Target>

But we will need to hard-code the shortcut to point to our junction (shorter path) instead. The new shortcut will be:

<Target>C:\VocalPathology\Vocal Pathology_I.exe</Target>

When the package gets published, the shortcut will get ‘sucked’ into the virtual file system since App-V will append the /appvve parameter like so:

"C:\VocalPathology\Vocal Pathology_I.exe" /appvve:[PackageGUID]_[VersionGUID]

 

App-V 5 on Windows 10 x64 and Windows Server 2016

I’ve recently been working on an App-V 5 application where two variants are required – one is for Windows 10 x64 and one is for Windows Server 2016 (Citrix virtual desktop sessions).

To reduce administration, my current client wanted both App-V variants to be published to the same Active Directory group so that users could float between their standard desktops and Citrix sessions seamlessly.  My first thoughts were that I could simply set the ‘target OS’ condition for each variant….until I stumbled upon this:

Windows 10 Server 16 Target OS

In App-V 5 we can’t distinguish between Windows 10 x64 and Windows Server 2016 by using the ‘Target OS’ functionality.  So instead I wrote the following AddPackage script to add similar logic in the Deployment.config file:

<MachineScripts>
	<AddPackage>
		<Path>powershell.exe</Path>
		<Arguments>-ExecutionPolicy ByPass -WindowStyle Hidden -Command  "&amp; { if ([int](Get-CimInstance Win32_OperatingSystem | Select-Object -Expand ProductType) -eq 3) { Exit 0 } else { Exit 1 } }"</Arguments>
		<Wait RollbackOnError="true" Timeout="30"/>
	</AddPackage>
</MachineScripts>

This line of PowerShell merely checks to see if the current machine is a server platform (a ProductType of 3) or not (Work Station = 1, Domain Controller = 2, Server = 3).  If it is a server platform the script will return 0 and AddPackage will run successfully .  If it is not a server package the script will fail (return a non-zero value of 1) and roll back.  This will of course be silent to the end-user.

It doesn’t go into the granularity of checking the specific operating system, but it serves our purpose well since we are only packaging for Windows 10 and Server 2016.

 

Synchronise App-V 5 Client with Publishing Server

I’ve recently set up a couple of virtual machines to start sequencing and testing App-V 5 packages.  When setting up the client, rather than configure it for local testing I wanted to synchronise it with the App-V 5 publishing service.  We can do this (at the most basic level) using a PowerShell one-liner:

Add-AppvPublishingServer -name "<NAMEOFSERVER>" -Url "<URL OF Publishing Service>" | Sync-AppvPublishingServer

When you run this from an elevated PowerShell prompt, you may see one or more of the following errors:

Sync-AppvPublishingServer – Application Virtualization Service failed to complete requested operation.

HttpRequest sendRequest failed.

Getting server publishing data failed.

This is probably because the URL you specified is incorrect.  To find the correct URL, navigate the registry on your App-V 5 publishing server to the following location:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppV\Server\PublishingService

Grab the value of PUBLISHING_MGT_SERVER.  Replace ‘localhost’ with the fully qualified domain name (FQDN) of the server itself.  And replace the port with the value in PUBLISHING_WEBSITE_PORT.  Assuming that:

PUBLISHING_MGT_SERVER=http://localhost:8000/

PUBLISHING_WEBSITE_PORT=8001

and the FQDN of the server is appv5.alkane.uk

Your PowerShell one-liner would be:

Add-AppvPublishingServer -name "ALKANEAPPV5" -Url "http://appv5.alkane.uk:8001/" | Sync-AppvPublishingServer

To verify this, if you put the URL into a browser session you should see an XML view of published packages for the authenticated user.

App-V 5 and Killing Child Processes

I recently packaged an application that, upon launch, started a process called EntDistributorService.exe (the parent process).

EntDistributorService.exe spawned a child process called hvced.exe (I knew hvced.exe was a child process by running the wmic command found here.), which remained running when the launched application was closed.

When a process contained within the App-V package remains open, it presents issues when we need to unpublish it – the App-V package can’t be unpublished if it is still deemed as being in a ‘running’ state!

I tried to kill hvced.exe using a script (I tried taskkill.exe and Powershell’s Stop-Process) which ran in the ExitProcess scripting phase of the UserConfig.xml.  Unfortunately the context that these scripts run in didn’t have enough permissions to kill the process, since EntDistributorService.exe was launched in a SYSTEM context and as a result hvced.exe was also running in a SYSTEM context.

After all this fiddling around, it turned out that the solution was quite simple really.  In the DeploymentConfig.xml file we can simply add the parent file who’s child processes we want to terminate like so:

<TerminateChildProcesses>      
    <Application Path="[{AppVPackageRoot}]\VFS\ProgramFilesCommonX86\HealthVISION\EntDistributorService.exe" />         
</TerminateChildProcesses>

And it worked like a charm.

Rename App-V 4.6 Package Before Converting to App-V 5.1

I’ve been converting a bunch of apps recently from App-V 4.6 format to App-V 5.1 format.  I knew this could be done using PowerShell cmdlets, so rather than reinvent the wheel I used a great script written by Tim Mangan to do this for me.

The problem I had was that our App-V 4.6 packages all had a specific naming convention that included the characters ‘AV46’…..or worse….didn’t have a consistent naming convention whatsoever!  So it meant that when these packages were converted to App-V 5.1 they either still contained the characters ‘AV46’ (which looked stupid in my App-V 5.1 console) or were named inconsistently.

Since the ConvertFrom-AppvLegacyPackage cmdlet doesn’t expose a way of naming the ‘output’ package, and I didn’t want to open and re-save each of my converted App-V 5.1 packages to merely change the package name, the only option I saw was to rename the App-V 4.6 package before I converted it!  So that’s exactly what this script does!

Why is it written in VBScript and not PowerShell?  Simply so we can drag a package folder onto the .vbs file and rename it.  Otherwise to do the same for PowerShell would require hacking around in the registry.

How it works

Consider you have a folder containing the following App-V 4.6 package:

App-V 4.6 package

First you should grab the script below, and save it as (for example) RenameAV46.vbs.

You would then simply drag the parent package folder (Gateway_WinDip_2.2_32_AV46_R01) on to RenameAV46.vbs.  It will then prompt you for a new package name, where you would specify the new package name (for example) ‘Gateway_WinDip_2.2_32_AV51_R01’ or similar.

The script renames the minimal amount required to prepare your App-V 4.6 package for App-V 5.1 conversion.  For example, it will not rename the icons folder and re-point the OSD shortcut icons since this is not required for the conversion process to be successful.

Once you have renamed the package, it is then ready to run through Tim’s conversion script!

Option Explicit

Dim objFSO : Set objFSO = createobject("Scripting.FilesystemObject")

Dim objXMLDoc : Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 

Dim objXMLDoc2 : Set objXMLDoc2 = CreateObject("Microsoft.XMLDOM") 
objXMLDoc2.async = False 
	
Dim folderPath : folderPath = wscript.arguments(0)
Dim oldPackageName : oldPackageName = ""
Dim currentFilePath : currentFilePath = ""
Dim currentFileName : currentFileName = ""
Dim currentParentFilePath : currentParentFilePath = ""
Dim currentFileExtension: currentFileExtension = ""

Dim newFilePath : newFilePath = ""
Dim newFileName : newFileName = ""

Dim existingPATH : existingPATH = ""
Dim newPATH : newPATH = ""
Dim existingHREF : existingHREF = ""
Dim newHREF : newHREF = ""
Dim existingNAME : existingNAME = ""
Dim newNAME : newNAME = ""

Dim newPackage : newPackage = ""

Dim parentFolderPath : parentFolderPath = objFSO.GetParentFolderName(folderPath)
Const ForReading = 1
Const ForWriting = 2		

Public Function DecodeXml(TextToDecode)
    'Take text that has been encoded for XML and change it to normal text
    Dim Res
    Res = Replace(Replace(Replace(Replace(TextToDecode, "&quot;", """"), "&gt;", ">"), "&lt;", "<"), "&amp;", "&")
    DecodeXml = Res
End Function

Public Function EncodeXml(TextToEncode)
    'Take text and change special chars so that it can be included in an XML file
    Dim Res
    Res = Replace(Replace(Replace(Replace(TextToEncode, "&", "&amp;"), ">", "&gt;"), "<", "&lt;"), """", "&quot;")
    EncodeXml = Res
End Function

Function GetNewFilePath(existingFilePath, oldPackageName, newPackageName)	
	'get new file name based on old file name, stripping out any version ("_!","_£" etc)	
	Dim RegX : Set RegX = new RegExp
				
	RegX.Pattern = "(" & oldPackageName & ")(_\d+)*"
	RegX.Global = True

	GetNewFilePath = RegX.Replace(existingFilePath, newPackageName)	
End Function

Sub RenameFile(currentFilePath, newFilePath)
	If (currentFilePath <> newFilePath) Then
		objFSO.MoveFile currentFilePath, newFilePath
		currentFilePath = newFilePath		
	End if			
End Sub

Dim newPackageName : newPackageName = InputBox("Enter new package name")

If (trim(newPackageName) = "") Then
	MsgBox "The package name was blank - please populate.  Quitting."
	wscript.quit
End If

Dim newPackagePath : newPackagePath = parentFolderPath & "\" & newPackageName

'rename package folder with new name
objFSO.MoveFolder folderPath, newPackagePath

Dim newPackagePathFolder : Set newPackagePathFolder = objFSO.GetFolder(newPackagePath)
Dim nodetext,folder,file

'get SPRJ (project) name
For Each file In newPackagePathFolder.Files
	Select Case UCase(objFSO.GetExtensionName(file.Path))
		Case "SPRJ"
			oldPackageName = objFSO.GetBaseName(file.Path)
			Exit For
	End Select
Next

'iterate though package files
For Each file In newPackagePathFolder.Files
	
	currentFilePath = file.Path
	
	'We need to be careful of the encoding in the DEFAULTOSD element
	Select Case UCase(objFSO.GetExtensionName(currentFilePath))
	Case "MSI","XML"				
		'just rename these file types
		newFilePath = GetNewFilePath(currentFilePath, oldPackageName, newPackageName)	
		RenameFile currentFilePath, newFilePath	
	Case "SPRJ"		
		
		objXMLDoc.load(currentFilePath)
		
		If 0 <> objXMLDoc.ParseError Then
			msgbox "SPRJ " & objXMLDoc.ParseError.Reason & " " & currentFilePath
		Else		
			'change PACKAGEFILE
			Dim PackageFile : Set PackageFile = objXMLDoc.selectSingleNode(".//SEQUENCERPROJECT/PACKAGEFILE")
			If Not PackageFile Is Nothing Then	  
				existingPATH = PackageFile.getAttribute("Path")		

				'we rename the SFT in here instead of in the SELECT...CASE statement in case there are multiple SFTs (messy package folder)
				newFilePath = GetNewFilePath(newPackagePathFolder & "\" & existingPATH, oldPackageName, newPackageName)
				RenameFile newPackagePathFolder & "\" & existingPATH, newFilePath				
				
				newPATH = GetNewFilePath(existingPATH, oldPackageName, newPackageName)	
				PackageFile.setAttribute "Path",newPATH
				objXMLDoc.Save(currentFilePath)	
			Else
				Msgbox "cannot find PACKAGEFILE node"
			end if
			
			Set PackageFile = Nothing				
			
			'the DEFAULTOSD node is actually encoded XML, so here we read it all in so we can parse the XML DOM and update CODEBASE HREF and PACKAGE NAME
			
			Dim DefaultOSDNode : Set DefaultOSDNode = objXMLDoc.selectSingleNode(".//SEQUENCERPROJECT/DEFAULTOSD")
			If Not DefaultOSDNode Is Nothing Then
			
				'remember that the DEFAULTOSD node contains XML itself, so we re-load the contents into the XML parser
				nodetext = DefaultOSDNode.text

				objXMLDoc2.loadXML(nodetext)
				
				If 0 <> objXMLDoc2.ParseError Then
					msgbox "DEFAULTOSD CODEBASE " & objXMLDoc2.ParseError.Reason
				Else
				
					Dim CodebaseNode : Set CodebaseNode = objXMLDoc2.selectSingleNode(".//SOFTPKG/IMPLEMENTATION/CODEBASE")
					If Not CodebaseNode Is Nothing Then	  
						existingHREF = CodebaseNode.getAttribute("HREF")
						newHREF = GetNewFilePath(existingHREF, oldPackageName, newPackageName)				
						CodebaseNode.setAttribute "HREF",newHREF							
					Else
						Msgbox "cannot find CODEBASE node"
					end if
					
					Set CodebaseNode = Nothing	
					
					Dim PackageNode : Set PackageNode = objXMLDoc2.selectSingleNode(".//SOFTPKG/PACKAGE")
					If Not PackageNode Is Nothing Then
						PackageNode.setAttribute "NAME",newPackageName							
					Else
						Msgbox "cannot find PACKAGE node"
					end if
					
					Set PackageNode = Nothing			
									
				End If			
				'set new value for DEFAULTOSD contents
				DefaultOSDNode.text = objXMLDoc2.selectSingleNode(".//SOFTPKG/").xml
				objXMLDoc.Save(currentFilePath)	
			Else
				Msgbox "cannot find DEFAULTOSD node"
			End If					
			Set DefaultOSDNode = Nothing	
		End If	
		
		newFilePath = GetNewFilePath(currentFilePath, oldPackageName, newPackageName)	
		RenameFile currentFilePath, newFilePath
		
	Case "OSD"
		
		objXMLDoc.load(currentFilePath)
		
		If 0 <> objXMLDoc.ParseError Then
			msgbox "OSD " & objXMLDoc.ParseError.Reason & " " & currentFilePath
		Else		
			Set CodebaseNode = objXMLDoc.selectSingleNode(".//SOFTPKG/IMPLEMENTATION/CODEBASE")
			If Not CodebaseNode Is Nothing Then	   
				existingHREF = CodebaseNode.getAttribute("HREF")
				newHREF = GetNewFilePath(existingHREF, oldPackageName, newPackageName)				
				CodebaseNode.setAttribute "HREF",newHREF
				objXMLDoc.Save(currentFilePath)	
			Else
				Msgbox "cannot find CODEBASE node"
			end if
			
			Set CodebaseNode = Nothing				
		End If			
	End Select	
Next

MsgBox "Done"

Set objXMLDoc = Nothing
Set objFSO = Nothing

 

 

Start an App-V 5 Process immediately after Publishing

I have a piece of software that acts as an ‘agent’ sitting in the background (in the System Tray) waiting to intercept print requests.  It needs to be running all the time since it intercepts print requests from ANY desktop application.

The target platforms are a combination of standard desktops and non-persistent VDI desktops.  Making it work with standard desktops wasn’t really the problem – by default the software placed a shortcut in the Startup folder and after publishing, logging off and logging in the background process started.

The trouble with a non-persistent VDI is that when a user logs off and logs back in, they get a completely fresh desktop experience and all the apps get published again.  As a result of this we can’t just lump some logic in a Run key or the Startup folder since the application is published after these features are processed.

Combined with this there were another 2 applications that needed connection-grouping with this agent application.

Unless there’s a more elegant way of achieving this that I’m not aware of, I present my solution forthwith:

Firstly, since we’re publishing to users, I have added a user script at publish time in the AppxManifest.xml file (or you could use the UserConfig.xml file – your choice depending on which version of App-V you’re using):

		
<appv:UserScripts>
        <appv:PublishPackage>
            <appv:Path>cmd.exe</appv:Path>
            <appv:Arguments>/c START "" "powershell.exe" -ExecutionPolicy ByPass -WindowStyle Hidden -File "[{AppVPackageRoot}]\..\Scripts\runProcess.ps1"</appv:Arguments>
            <appv:Wait RollbackOnError="false" />
        </appv:PublishPackage>
    </appv:UserScripts>
	

What’s important to note is that we can’t just point at our background process for 2 reasons:

  1. Because the application is not yet published at this point, and so we can’t access/run it
  2. Even if we could access/run it, the publishing process would stick at running the .exe since the .exe will always be running and the PublishPackage action will never release the handle (unless we manually kill it).  And hence the package would never publish!

Instead we’ve called cmd.exe with the START command.  What this does is launch powershell.exe as a separate process and does NOT wait for it to complete.  Hence the publishing of the app will complete as normal.  Meanwhile in the background, powershell.exe runs a script that I’ve placed in the Scripts folder in the background.  So what does the script do?  In essence it:

  • Loops 10 times maximum, with a 5 second delay between each loop, and attempts to get the package (Get-AppvClientPackage) and ensure it is published to the user (IsPublishedToUser).  We cannot run the process if the package is not published.
  • Loops 10 times maximum, with a 5 second delay between each loop, and attempts to get the connection group (Get-AppvClientConnectionGroup) and ensure it is enabled (IsEnabledToUser).  If using a connection group, we cannot enable a connection group if a package is in use!  So we need to wait until the connection group is enabled before we attempt to start the process.
  • Loops 10 times maximum, with a 5 second delay between each loop, and attempts to start the process (Start-Process).  There’s no reason why it shouldn’t start first time, but I still add in some retries just in case.

Here is the script (runProcess.ps1)

#variable to store the current script folder (in case, for ease, we want to put our exe's in the same folder as the script)
$ScriptFolder = $PSScriptRoot

#path that points to the 'Root' folder inside an app-v package
$appvRoot = split-path $ScriptFolder -parent
$appvRoot = $appvRoot + "\Root"

#default number of attempts
$attempts = 10
#path to exe inside app-v package
$processPath = "$appvRoot\VFS\ProgramFilesX86\Equitrac\Express\Client\EQMsgClient.exe"
#process name without exe (used with Get-Process)
$processName = "EQMsgClient"
#name of App-V package
$packageName = "Nuance_EquitracExpressClient_5.4.23.4801"
#name of connection group
$connectionGroupName = "Equitrac Express"

#initialise
$packageObject = $false
$connectionGroupObject = $false
$newProcess = $null

$eventMessage = "Attempting to run: " + $processPath
write-host $eventMessage


#wait until package is published
$packageObject = Get-AppvClientPackage -Name $packageName -ErrorAction SilentlyContinue

while (($packageObject -eq $null -Or (!($packageObject.IsPublishedToUser))) -And $attempts -gt 0)
{   
    Start-Sleep -s 5
    $attempts = $attempts - 1

    $eventMessage = "Checking if the following package is published: " + $packageName + " (Attempts remaining: " + $attempts + ")"
    write-host $eventMessage  

    $packageObject = Get-AppvClientPackage -Name $packageName -ErrorAction SilentlyContinue  
}


if ($packageObject.IsPublishedToUser)
{
    $eventMessage = "The following package is published: " + $packageName
    write-host $eventMessage

    #now we need to check that the connection group has applied and is published!
    $connectionGroupObject = Get-AppvClientConnectionGroup -Name $connectionGroupName -ErrorAction SilentlyContinue
    
    #reset attempts
    $attempts = 10

    while (($connectionGroupObject -eq $null -Or (!($connectionGroupObject.IsEnabledToUser))) -And $attempts -gt 0)
    {   
        Start-Sleep -s 5
        $attempts = $attempts - 1

        $eventMessage = "Checking if the following connection group is enabled: " + $connectionGroupName + " (Attempts remaining: " + $attempts + ")"
        write-host $eventMessage

        $connectionGroupObject = Get-AppvClientConnectionGroup -Name $connectionGroupName -ErrorAction SilentlyContinue
    }

    
    if ($connectionGroupObject.IsEnabledToUser)
    {
        $eventMessage = "The following connection group is enabled: " + $connectionGroupName
        write-host $eventMessage

        #reset attempts
        $attempts = 10

        #now try and start our process
        while (((Get-Process -Name $processName -ErrorAction SilentlyContinue) -eq $null) -And $attempts -gt 0)
        {
            if (Test-Path $processPath)
            {
                $newProcess = Start-Process -FilePath $processPath -NoNewWindow -ErrorAction SilentlyContinue       
            }
            else
            {
                $eventMessage = "Process path : " + $processPath + " was not found.  Unable to start process."
                write-host $eventMessage
                break
            }
            $attempts = $attempts - 1

            $eventMessage = "Trying to start process: " + $processPath + " (Attempts remaining: " + $attempts + ")"
            write-host $eventMessage

            Start-Sleep -s 5
        }
        
        if ($newProcess -eq $null)
        {
            $eventMessage = "Process : " + $processPath + " could not be launched."
            write-host $eventMessage
        }
        else
        {
            $eventMessage = "Process : " + $processPath + " was launched successfully."
            write-host $eventMessage
        }
        
    }
    else
    {
        $eventMessage = "Connection Group : " + $connectionGroupName + " was not enabled.  Unable to start process."
        write-host $eventMessage
    }
}
else
{
    $eventMessage = "Package : " + $packageName + " was not published.  Unable to start process."
    write-host $eventMessage
}

Things to tweak

The variables at the top (package names etc).  Also remember this targets packages published to the user, not the machine.  So you’ll need to tweak things like ‘isEnabledToUser’ and ‘isPublishedToUser’ etc.  Also you may not be using a connection group like me, so you could chop that logic out too.

A note before you commence…

Since the background process is always running, it obviously can’t be unpublished instantly (because the package will be in use).  Luckily we’re running the App-V 5 SP2 client which will set up a pending task to un-publish it when the user logs off and back in.  You may want to consider this before implementing it.

Using Taskkill.exe to Kill Multiple Processes on One Line

I’ve had an issue with my current App-V 5 application.  When I launch an executable called CentralSurveillance.exe, it spawns another two processes called K2DataLogger.exe and K2MSService.exe.  And these remain open even when I close CentralSurveillance.exe.  My first thoughts were that these were child processes, so I added the following to the DeploymentConfig:

<TerminateChildProcesses>
<Application Path="[{ProgramFilesX86}]\K2 Medical Systems\Guardian\CentralSurveillance.exe" />
</TerminateChildProcesses>

Sure enough, when I closed the instance of CentralSurveillance.exe….the instances of K2DataLogger.exe and K2MSService.exe were still running!  So I dug a little deeper – were these actually child processes or not?

I opened up a command prompt and ran:

wmic process Get Name,Description,CommandLine,ProcessId,ParentProcessId /format:list > processes.txt

What this does is list the name, description, command line, process id and parent process id of each running process and outputs it to a text file called processes.txt.  The results (after I’d deleted the irrelevant stuff) were as follows:

CommandLine="C:\Program Files\Microsoft Application Virtualization\Client\AppVClient.exe"
Description=AppVClient.exe
Name=AppVClient.exe
ParentProcessId=496
ProcessId=2664

CommandLine=C:\windows\system32\svchost.exe -k DcomLaunch
Description=svchost.exe
Name=svchost.exe
ParentProcessId=496
ProcessId=612

CommandLine=C:\windows\Explorer.EXE
Description=explorer.exe
Name=explorer.exe
ParentProcessId=4032
ProcessId=4224

CommandLine="C:\Users\xxxxx\AppData\Local\Microsoft\AppV\Client\Integration\07390066-7746-41A4-B6FB-3D1977D289BA\Root\VFS\ProgramFilesX86\K2 Medical Systems\Guardian\CentralSurveillance.exe"
Description=CentralSurveillance.exe
Name=CentralSurveillance.exe
ParentProcessId=4224
ProcessId=6120

CommandLine="C:\ProgramData\App-V\07390066-7746-41A4-B6FB-3D1977D289BA\CF8C5523-5038-4C73-991E-EBB4812B110B\Root\VFS\ProgramFilesX86\K2 Medical Systems\Guardian\K2DataLogger.exe"
Description=K2DataLogger.exe
Name=K2DataLogger.exe
ParentProcessId=2664
ProcessId=6040

CommandLine="C:\ProgramData\App-V\07390066-7746-41A4-B6FB-3D1977D289BA\CF8C5523-5038-4C73-991E-EBB4812B110B\Root\VFS\ProgramFilesX86\K2 Medical Systems\Guardian\K2MSService.exe"
Description=K2MSService.exe
Name=K2MSService.exe
ParentProcessId=2664
ProcessId=3872

We can clearly see that the parent process of K2DataLogger.exe and K2MSService.exe is NOT CentralSurveillance.exe (process id 6120) but instead AppVClient.exe (process id 2664).  And this is probably why TerminateChildProcesses will not work in this instance.  The workaround I used is to run an ExitProcess script so that when we close CentralSurveillance.exe, it kills both K2DataLogger.exe and K2MSService.exe.

I did this like so:

<UserScripts>
<ExitProcess>
<Path>CMD.EXE</Path>
<Arguments>/C taskkill /im "K2DataLogger.exe" /f /t &amp; taskkill /im "K2MSService.exe" /f /t</Arguments>
<Wait RollbackOnError="false" />
<ApplicationId>[{ProgramFilesX86}]\K2 Medical Systems\Guardian\CentralSurveillance.exe</ApplicationId>
</ExitProcess>
</UserScripts>

You can see that I can specify multiple command lines to run via CMD.exe by separating each command by an ampersand (& – this needs to be URL encoded in an XML file which is why it would appear as &amp; instead).  In the workaround above I specify two processes.  However by using brackets and nesting commands, I can specify more!  Here’s an example of an argument that kills 4 processes:

taskkill /im "odbcad32.exe" /f /t & (taskkill /im "wordpad.exe" /f /t & (taskkill /im "notepad.exe" /f /t & taskkill /im "mspaint.exe" /f /t))

Using RunVirtual to Present Excel Addins

I’ve recently been having a look at the best way to present Excel Add Ins using our locally installed instance of Microsoft Excel and our virtualised Add Ins.

The easiest approach is probably to create an additional shortcut in your package called ‘Microsoft Excel 2010 with XXX Add In’ which points to your local instance of Excel.exe.  Running this shortcut will launch the local instance of Excel.exe inside the virtual bubble, and you should then see your virtualised Add Ins when the application is loaded.

The trouble with this approach is that users will generally head straight to the main shortcut to Microsoft Excel (the shortcut to the local instance) and wonder why they can’t see their Add In.  On top of this, using the approach above could mean that we have multiple ‘Microsoft Excel 2010 with XXX Add In’ shortcuts, which can look quite untidy.

An alternative approach is presented by the RunVirtual registry key introduced in App-V 5 .  What this feature enables us to do is to launch the shortcut to the locally installed version of Excel and drag this process (Excel.exe) instance inside the virtual bubble of a specified package.

Again, I won’t go into too much detail here since there’s a great blog about how RunVirtual works here and also a mention of an important update for App-V 5 SP3 here, which enables us to target packages published to the User.

Anyway, I’m babbling a bit so we’ll crack on….

In my opinion RunVirtual is ALMOST great.  Ideally we would be able to create a RunVirtual reference to a local application like so (Microsoft Excel in this example):

HKEY_CURRENT_USER\Software\Microsoft\AppV\Client\RunVirtual\Excel.exe

and then list multiple virtualised Addins underneath that (by specifying their PackageId and VersionId).  Unfortunately we can only specify one virtual package under this key.

What this limitation means is that we will need to specify one ’empty’ virtual package which maintains a static PackageID and VersionId.  This package will NEVER change.  We will then use a Connection Group to connect this single package to our multiple virtualised Add In packages.  What this essentially means is that when we launch the local version of Excel.exe, it will launch our associated RunVirtual package (our ’empty’ package) which in turn will launch all of its connected packages.

In honesty though, and before we continue, you would really require App-V 5 SP3 as a minimum.  Why?  Because the schema for connection groups has changed and it now enables us to specify OPTIONAL packages (i.e, if a connected package is not published just ignore it and don’t throw an error!) and to automatically use the latest published version of a package.  For me, the ‘optional’ addition is the most important.  After all, our Add Ins aren’t necessarily for all users!  We may have one Add In policied to a specific group of users, and another Add In policied to another specific group of users.  And membership of these policied AD Groups may very well be mutually exclusive!  The optional attribute saves us a great headache in that sense and provides us with a flexibility that helps to reduce the management overhead.

Am I still babbling?  Ok.  An example….

Step 1.  Create an ‘Empty’ Package

We will link to this from the RunVirtual key.  This package isn’t entirely empty – make sure you add a dummy file to the package otherwise it will throw an error when you’re adding the package (presumably because there is no VFS to mount).

Also, ensure you allow Named Objects and COM objects to interact with the local system.  They’re not both required for this example (although I think Named Objects IS required), but we need to think ahead.  If we are packaging a COM Add In for Excel, this may well be required.

Finally follow this guide.  This guide lets us dynamically add the relevant RunVirtual key from our AppxManifest.xml.  In this example we are going to add RunVirtual in the HKCU key, since we are publishing the package to the User and NOT the machine.

Note to SCCM 2012 Virtual Environment users (and this part is all untested by myself):

As mentioned in the comments section below, when using SCCM 2012 Virtual Environments it is not a requirement that you specify a dummy package. Instead you can specify the AppConnectionGroupId and VersionId of the connection group. The problem with this approach is that the connection group gets dynamically generated on each client, which means that the AppConnectionGroupId and VersionId of the connection group is different for each client. I have suggested that to set the RunVirtual registry value dynamically you can use the approach documented here, so long as the dynamically generated connection group has a static name (I can’t verify this because I don’t have the environment to test with)!!

Get-AppvClientConnectionGroup -all | where {$_.Name -eq 'Name_Of_Connection_Group'} | foreach { New-Item -Path HKCU:\SOFTWARE\Microsoft\AppV\Client\RunVirtual -Name Excel.exe -Value ($_.AppConnectionGroupId.toString() + '_' + $_.VersionId.toString()) -Force }

The alternative approach is to use a dummy package like I have suggested in this blog, whereby the PackageId and VersionId of the dummy package remains static.

Another Note:

As Tim Mangan has mentioned in the comments section below, delivery via an App-V server differs from SCCM 2012 Virtual Environments in that it requires at least one non-optional package.

Step 2.  Sequence Two Add In Packages

Each package will contain a different Excel Add In.  For this proof of concept I Googled for free XLA files and created a package for each one.  I simply lumped the XLA file into the following location, and that was that:

%AppData%\Microsoft\Excel\XLSTART

(You can alternatively copy it here for a per-machine install)
C:\Program Files\Microsoft Office\Office14\XLSTART

Again, ensure you allow Named Objects and COM objects to interact with the local system.  We need to ensure that all of our connected packages contain the same setting for COM and Named Objects, otherwise we may get errors when launching the connection group such as:

The Application Failed to Launch.  This may be due to a network failure
or
The connection group {xxx} version {xxx} could not be published because the virtual COM settings of the individual packages conflict.
Verify that the virtual COM settings are the same for all member packages and try again.

Step 3 – Create Connection Group for Local Testing

Create a connection group using this tool.  You should add the ’empty’ package as being mandatory (i.e, NOT optional).  The version shouldn’t matter since this package will NEVER change.

The other two Add In packages should be marked as Optional and Ignore Version.

Connection Group(Note that in your live environment you will probably just use your App-V publishing server to manage this connection group and its settings)

Step 4 – Local Testing

Ensure scripting is enabled on your machine

Set-AppvClientConfiguration -EnablePackageScripts 1

Add and Publish all your packages (since our RunVirtual key should be installing to HKCU, we publish our packages to the User and NOT the machine)

Add-AppvClientPackage -Path "Empty_Package.appv" | Publish-AppvClientPackage
Add-AppvClientPackage -Path "Addin1.appv" | Publish-AppvClientPackage
Add-AppvClientPackage -Path "Addin2.appv" | Publish-AppvClientPackage

Add and Enable our Connection Group

Add-AppvClientConnectionGroup -path "AddInCG.xml" | Enable-AppvClientConnectionGroup

Launch the local Excel version, and see your Add Ins appear!

As a further test, try un-publishing just one of the Add In packages and then launch Excel again.  You will notice 2 things:

1)  You don’t get an error, even though the package is not published!  This is due to the Optional attribute in our connection group.  So that works well.

2)  You will notice that the Add In still appears in the Excel Add In tab!  Doh.  Why has this happened?

Well.  Putting App-V aside for a second.  You will notice that when you lump your XLA file into %AppData%\Microsoft\Excel\XLSTART and then launch Excel, the date modified of the XLA file changes!  Close Excel and the Date Modified reverts back to its original Date Modified date.  So what I believe is happening here is that App-V detects a modification to the file, and writes it to a Copy on Write location.

Importantly, it doesn’t write it to the Copy on Write location of the Add In package but instead to the Copy and Write location of the Connection Group!!

If you copied your XLA file to the per-user location (as in this example), you will find a saved XLA file in this location:

(Copy on Write (COW) Roaming) %AppData%\Microsoft\AppV\Client\VFS\{ConnectionGroupPackageId}\AppData\Microsoft\Excel\XLSTART

If you copied your XLA file to the per-machine location, you will find a saved XLA file in this location:

(Copy on Write (COW) Local) %LocalAppData%\Microsoft\AppV\Client\VFS\{ConnectionGroupPackageId}\ProgramFilesX86\Microsoft Office\Office14\XLSTART

So in essence, when you un-publish your Add In packages you will need to add some logic to delete this file.