App-V CD/DVD powershell script

Some applications require a CD/DVD for the licence of the application. This can be a challenge if the application does not check the CD/DVD source. Some applications save the drive settings in an ini file and that isn’t very dynamic. When the drive letter of the CD/DVD drive changes the application can’t find the source anymore because the INI file doesn’t detect the change. I had to sequence an application that required the CD and save the source settings in an ini file that is located in the Windows directory. This application has to be dynamic with the different drive letters because the application is deployed to different hardware set-ups. To make this dynamic I created a powershell script that checks all logicaldisks on the volumename and drivetype. When there is no CD/DVD that contains the volumename and correct drivetype the script displays an error message in a messagebox.

This way the application is dynamic with diffrent drive letters. In my case the application has a file type association (FTA) that also has to be compatible with the dynamic drive letters. To realise this I added a parameter in the script to open the file with the right FTA. If a different file has opened the script will display an error, saying that the FTA is not compatible with the application.

param($Arg1)
$ErrorActionPreference = "SilentlyContinue"
trap {

# Global error

[VOID][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Windows.Forms.MessageBox]::Show("Could not continue.",
"APP Error",
[Windows.Forms.MessageBoxButtons]::Ok,
[Windows.Forms.MessageBoxIcon]::exclamation)
}

# Function to get the CD/DVD drive letter
function Get-CDDrives
{
@(Get-WmiObjectwin32_logicaldisk | where-Object{$_.VolumeName -eq "APP"} | where-Object {$_.Drivetype -eq "5"} | ForEach-Object { $_.DeviceID})
}

# Opens the application with FTA
function Open-param
{
(get-content "$env:windir\APP.ini") -replace "DataDir=.*", "DataDir=$DRIVE" | set-content "$env:windir\APP.ini"

& "Q:\APP.101\Application\App.exe" "$ARG1"
}

# Opens the application normal
function Open-norm
{
(get-content "$env:windir\APP.ini") -replace "DataDir=.*", "DataDir=$DRIVE" | set-content "$env:windir\APP.ini"

& "Q:\APP.101\Application\App.exe"
}

# Error no disc
function Open-error
{
[VOID][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Windows.Forms.MessageBox]::Show("Please insert the application CD and start the application again.",
"APP message",
[Windows.Forms.MessageBoxButtons]::Ok,
[Windows.Forms.MessageBoxIcon]::exclamation)
}

IF (!$ARG1)
{
$TEMP = Get-CDDrives
$DRIVE = $TEMP + "\"

IF ($DRIVE -eq "\") {Open-error}
ELSE {Open-norm}
} ELSEIF ($ARG1 -like "*.extension")
{
$TEMP = Get-CDDrives
$DRIVE = $TEMP + "\"
IF ($DRIVE -eq "\") {Open-error}
ELSE {Open-param}

} ELSE
{

# Error wrong FTA

[VOID][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Windows.Forms.MessageBox]::Show("Could not continue, please make sure you're opening the right file.",
"APP Error",
[Windows.Forms.MessageBoxButtons]::Ok,
[Windows.Forms.MessageBoxIcon]::exclamation)
}

Change the red highlighted lines to your application and error messages. When you’re sequencing the application open a powershell prompt and run “set-executionpolicy remotesigned” or just add a reg file with the right settings. When you are at step 4 of 6 replace the application path of the application with the following line:

“C:\Windows\System32\WINDOW~1\v1.0\Powershell.exe” -WindowStyle hidden “Q:\Youre script location.ps1”. Make sure you use the 8.3 file names. Add a FTA and use the parameter %1. Finish the sequence and use the application.

If you have a suggestion or question, leave a comment.