Batch Script
Script to check/kill process before install
The following batch file can be used as an example of how to do a software installation/upgrade when process X is not running. Note that also requires using PsKill from SysInternals. The script will:
- Check for the existence of a running process (.exe) of the software
- Stop the process if detected as running
- Perform a software installation
- Then start the software again
@echo off Set CURPATH = %~dp0 tasklist | findstr /i screenagent.exe echo errorlevel = %ERRORLEVEL% if ERRORLEVEL 0 goto Running if ERRORLEVEL 1 goto DoInstall :exit exit 0 :DoInstall echo Installing NICE ScreenAgent Software... start /wait /i "NICE Agent" "%CURPATH%Setup.exe" start ScreenAgent.exe goto exit :Running echo ScreenAgent Running: stopping execution "%CURPATH%pskill.exe" /accepteula ScreenAgent.exe goto DoInstall
App-V 5.0 Script to Update Environment Path
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>
App-V 5.0 Demo – Uninstall a Native Application
This is the walkthrough for demo #1 in my Microsoft Master webcast on App-V 5.0 scripting. For the full webcast information, visit https://t3chn1ck.wordpress.com/2014/02/03/playback-of-microsoft-master-app-v-5-0/.
This demo is on uninstalling native (local) applications when an App-V package is added to a system. For this example, I used 7-zip.
1) First, sequence your application (7-zip). Nothing special to do, just make sure it’s been done ;-)
2) Create a .bat file to perform the uninstall of the software. In this case, I just looked up GUID of the package to have msiexec perform the uninstall and added an extra step to then echo “Removed 7zip” into a text file for evidence that the script executed. So my batch file looks like below.
Note: that you don’t have necessarily have to do this uninstall so simply as I have .. or even use a .bat script. It could be VBScript. Or PowerShell. And/or include any custom checks to make sure it gets removed. And the options go on.
MsiExec.exe /x {23170F69-40C1-2702-0920-000001000000} /qb echo Removed 7zip >> C:\uninst_7zip.txt
3) Save your .bat file onto a server share. This share needs to have read, list, execute rights for all users and all computers of the domain. In my examples, I essentially just had the share as \\server\AppV_Scripts\ which I can use for other scripts and whatnot.
4) Edit the DeploymentConfig.xml file of the AppV package. What we want to do is
- Locate the <MachineScripts> section – uncomment the blocked text so it can used
- Locate the <AddPackage> element and delete the rest of the elements
- Set the <Path> to execute cmd.exe on the system
- Set the <Arguments> to run the .bat file on the server; if you’re unaware /c means to cmd.exe to “run this command” which would then be your \\server\share\script.bat
<!-- Machine Scripts Example - customize and uncomment to use machine scripts --> <MachineScripts> <AddPackage> <Path>cmd.exe</Path> <Arguments>/c \\alderaan2\appv_scripts\uninst_7zip.bat</Arguments> <Wait RollbackOnError="true" Timeout="30"/> </AddPackage> </MachineScripts>
5) Now we’ll add the package into ConfigMgr as an Application. Note that ConfigMgr will choose the most recently modified Config.xml file to use for the Deployment Type.
- Add a new Application
- Select to add an App-V 5.0 package and select the .appv file
- Complete the wizard
- Using standard ConfigMgr procedures, deploy the application to the desired user collection (or computer)
- Run the application from the targeted user’s Application Catalog
- Watch the local system as the natively installed application is removed and replaced by the AppV package is there!
- Then check that the C:\ for presence of the uninst_7zip.txt file thus giving further proof that script executed!
Scripting Service Account Changes
I recently had a situation where I needed to script the creation of a Windows service and specify a domain user/password to run the service. The main struggle was getting the service to grant the user “Log On As Service” rights, which wasn’t being automatically completed with the SC.exe tool. To accomplish this feat, the NTRights.exe utility from the Windows Server 2003 Resource Kit was needed, and will be needed for the below script!
sc.exe create "My Service" type= own start= auto binpath= "C:\Program Files\Product\MyApp.exe" sc.exe config "My Service" obj= "domain\username" password= "thePassword" type= own "ntrights.exe" -u domain\username +r SeServiceLogonRight sc.exe start "My Service"
Installing VMware View Agent in a Task Sequence
The VMware View agent is deployable as an MSI file and uses basic command line parameters. However, I recently had significant difficulties with getting it to install within a ConfigMgr Task Sequence as part of an OS image deployment. Despite that the agent could be installed manually using the parameters, in a TS the installer would fail (e.g. return a non-success exit code) and thus fail the TS.
Through investigation, the agent’s install log file (located in %windir%\temp\) stated error 1603 and a message that if failed due to a detected pending restart. So in the TS, I added a pre-restart command prior to the install…..but that still did not solve the problem. Several various other solutions were attempted, but all to no avail.
Eventually I found a VMware KB article which outlined the issue. It was being caused by multiple registry keys – FileRenameOperations, Run, and RunOnce. So I created a quick batch script to
- Backup the reg keys to %windir%\temp
- Delete the reg keys
- Run the install
- Restore the reg keys
:: Backup problem registry keys for VMware Agent Install reg.exe EXPORT "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager" c:\windows\temp\hklmsession.reg /y reg.exe EXPORT "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" c:\windows\temp\hklmrun.reg /y reg.exe EXPORT "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" c:\windows\temp\hklmrunonce.reg /y :: Delete problematic registry keys reg.exe DELETE "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f reg.exe DELETE "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /va /f reg.exe DELETE "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /va /f :: Install VMware agent xcopy "%~dp0VMware-viewagent-x86_64-5.0.1-640055.exe" c:\windows\temp /y c:\windows\temp\VMware-viewagent-x86_64-5.0.1-640055.exe /s /v" /qn REBOOT=R" :: Restore registry keys reg.exe IMPORT c:\windows\temp\hklmsession.reg reg.exe IMPORT c:\windows\temp\hklmrun.reg reg.exe IMPORT c:\windows\temp\hklmrunonce.reg del c:\windows\temp\hklm*.reg exit
However, this still wasn’t good enough to solve the problem! The .msi needed to be copied locally to execute AND this needed to be run with 64-bit redirection disabled. It was a mess to figure out and test, but I was able to eventually discover the right combination to have the agent automatically in a Task Sequence!
Batch File to Schedule WinXP Restart
In a troubleshooting situation with Windows XP hanging during a restart of Windows after a CM07 package deployment, I built a workaround script that creates a scheduled task to perform a forced Windows restart (for 2 minutes after install completion). The reason for doing 2 minutes is because there are times when the scheduled task can be created in the 59th second of a minute, but there isn’t enough time for the installation to exit.
@echo off Set /a MIN=%TIME:~3,2% + 2 Set /a HOUR=%TIME:~0,2% :: Remove the echo command to actually schedule the restart echo at %HOUR%:%TIME% /interactive C:\Windows\system32\shutdown.exe -r -t 60 -f