App-V 5.0 Script to Update Environment Path

Posted on

The following script can be used as an example for to update the Windows system Environment variable PATH to include the root of the virtual file system for the package.  The script can be included as part of an App-V 5.0 dynamic configuration file (e.g. DeploymentConfig.xml).

@echo off
:: This script is intended to be embeded in an App-V 5.0 package for
::    the purpose of adding .exe files into command path.
:: It will retrive the current working directory of the script folder,
::    change the value from ending with \Scripts to \Root, and finally
::    update the PATH environment variable to include the directory path.

cls

:: Get the path of this script
set newpth=%~dp0
:: Change the value from \Scripts to \Root
set newpth=%newpth:scripts=Root\bin%
echo NEWPTH = %newpth%

:: Get existing PATH variable
for /F "tokens=2* delims= " %%f IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g

:: Determine if the new path already exists
echo.%OLD_SYSTEM_PATH% | findstr /i /c:%newpth% 1>nul

If errorlevel 1 (
  echo NEWPTH pattern not found
  setx PATH "%path%;%newpth%;" -m
) Else (
  echo NEWPTH pattern found, skipping
)

:: for troubleshooting, uncomment the pause below
:: pause

Then use this within the UserConfig of the DeploymentConfig.xml

<UserScripts>
    <PublishPackage>
        <Path>cmd.exe</Path>
        <Arguments>/c [{AppVPackageRoot}]\..\Scripts\newpath.bat</Arguments>
        <Wait RollbackOnError="true" Timeout="60"/>
    </PublishPackage>
</UserScripts>
Advertisements

One thought on “App-V 5.0 Script to Update Environment Path

    Unknown said:
    April 13, 2014 at 9:13 am

    Great script, you don’t need to add the full path, during scripting events, the script-folder is the current dir, so instead of using [{AppVPackageRoot}]\..\Scripts\newpath.bat, you can user newpath.bat.
    I would also add an unpublish event, to remove the dir from the path-var.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s