New! Powershell in App-V 5; some example scripts

Using App-V 4.x we have become acquainted to the somewhat cumbersome command line management implementation of the App-V Client. Both options (SFTTRAY and specifically SFTMIME) did the job but they were only focused on the client, tough to understand and learn and even harder to customize to your need. Needless to say not the most powerful tools in the world.

In App-V 5 all command line scripting has been replaced with PowerShell (requires PoSh 3.0 as a minimum). As we’ve seen from other products of Microsoft the options that are available in the GUI are also available on command line.

There’s only one caveat: we have to learn it! (at least I did clip_image001)

There are several modules available in App-V 5, depending if you’re on the server, sequencer or on the client they are named [AppVServer], [AppVSequencer] or [AppVClient]. A new feature of using PoSh 3.0 is that the App-V cmdlets no longer need to be imported before they can be used. Each of the modules has a variety of cmdlets available for you to use. They can be queried by using the help command. Here’s an example for the AppVClient

clip_image002

As there are many of them I’ve chosen to write about the “getting started” cmdlets that you might want to use before or during an evaluation or migration to App-V 5.

Note: I’m no PowerShell guru, so there are probably better, quicker and shorter ways to achieve similar functionality. These worked for me, but feel free to comment if you have any comments.

Note 2: Some of the cmdlets require admin privileges.

Converting Legacy App-V packages

If you are an App-V 4.x customer you would have to migrate you App-V 4.x applications to be compatible with the new format. Don’t worry you don’t have to re-sequence your entire application estate!

The conversion process can be initiated by the [Test-AppvLegacyPackage], which will test the App-V 4.x package for compatibility and output any error and warning but does not convert the package, and the [ConvertFrom-AppvLegacyPackage] which does the same but also actually converts it. Some of the conversion process caveats are further described here: http://www.softgridblog.com/?p=180

# ConvertPkg.PS1
# by Ment van der Plas, Login Consultants
# October 2012 - version 0.1
#
# This script enumerates through a given source dir and convert the App-V package to the given destination dir
# All actions will be logged in a log in the destination directory

# Remember to set execution policy (as administrator):
# Set-ExecutionPolicy Unrestricted
# Remember to import the AppVSequencer modules (as administrator):

#Define Source and Destination directories and create log file
$Now = Get-Date -Format yyyyMMdd_HHmmmss
$SourceDir = "C:\Convert\Source"
$TargetRoot = "C:\Convert\Destination"
$LogFile = $TargetRoot + "\Convert_" + $Now + ".log"
Write-Host "Creating $LogFile" -Foregroundcolor Green
#New-Item $TargetRoot -type Dir | Out-Null
New-Item $LogFile -type File | Out-Null

#Enumerate through folder structure and find App-V packages to convert
$DirArray = Get-ChildItem -path $SourceDir -recurse | where{$_.PsIsContainer -and $_-notmatch ‘Icons’}
ForEach ($Dir in $DirArray)
{
$TargetDir = $TargetRoot + "\" + $Dir
If (Test-Path $TargetDir) {
} else{
  Write-Host "Creating $TargetDir" -Foregroundcolor Green
  New-Item -path $TargetDir -type Directory | Out-Null
}

# Convert or Test any found App-V package. Remove remark if required
# $Dir | Test-AppvLegacyPackage >> $LogFile
$Dir | ConvertFrom-AppvLegacyPackage -Destinationpath $TargetDir >> $LogFile | Out-Null
}

Adding App-V 5 package to the local App-V client

So if you have App-V 5 packages created, either converted or created, you’d want to test them on the new App-V client. The quickest way to test if the packages actually works is to add them locally to the App-V 5 client. The following table shows the old and new command line options to achieve this:

App-V 4.x

App-V 5

SFTMIME ADD

Add-AppvClientPackage

SFTMIME ADD

Publish-AppvClientPackage

SFTMIME LOAD

Mount-AppvClientPackage

The following script allows you to import all packages from a certain source directory:

# AddPublishClient.PS1
# by Ment van der Plas, Login Consultants
# October 2012 - version 0.1
#
# This script enumerates through a given source dir and adds and/or publishes and/or mounts the package to the client
# All actions will be logged in a log in the %temp% directory

# Remember to set execution policy (as administrator):
# Set-ExecutionPolicy Unrestricted
# Remember to import the AppV Client module (as administrator):

#Define Source directory and create log file
$Now = Get-Date -Format yyyyMMdd_HHmmmss
$SourceDir = "C:\Temp"
$LogFile = $env:TEMP + "\Import_" + $Now + ".log"
Write-Host "Creating $LogFile" -Foregroundcolor Green
#New-Item $TargetRoot -type Dir | Out-Null
New-Item $LogFile -type File | Out-Null

#Loop through folder structure and find App-V packages
$Packages = Get-ChildItem -path $SourceDir -recurse | where{$_-match ‘.appv’}

ForEach ($Package in $Packages)
{
    #For package found add / publish / mount the package to the client
    #Remove quotes when required

    #Write-Host "Importing $Package" -Foregroundcolor Green
    #Add-AppvClientPackage $Package.FullName >> $LogFile | Out-Null

    Write-Host "Importing and publishing $Package" -Foregroundcolor Green
    Add-AppvClientPackage $Package.FullName | Publish-AppvClientPackage >> $LogFile | Out-Null

    #Write-Host "Importing, mounting and publishing $Package" -Foregroundcolor Green
    #Add-AppvClientPackage $Package.FullName | Publish-AppvClientPackage | Mount-AppvClientPackage >> $LogFile | Out-Null
}

Write-Host "Check out the log file $LogFile for any warnings or errors" -ForegroundColor Green

Adding Packages to the Management Server

Having a management server enhances the customization capabilities of deploying an App-V 5 application. Adding to and deploying applications through the management can be a manual process but can be automated as well. Remember that this was not possible in App-V 4.x (i.e. there are no API’s for importing packages into the server). Extremely useful to build a quick test environment or to automate a production environment!

The cmdlet [AppVServer] holds the possibility to add, grant and publish a package to the environment through scripting. The following script is an example of these actions:

# AddPublishServer.PS1
# by Ment van der Plas, Login Consultants
# October 2012 - version 0.1
#
# This script enumerates through a given source dir and adds and/or publishes and/or grants the package in the management console
# If required it can replace the UNC path (used for enumeration) with an HTTP path (used for streaming)

# Remember to set execution policy (as administrator) and import AppVServer module:
# Set-ExecutionPolicy Unrestricted

#Define Source and HTTP directory, create log file and set the group for publishing permissions
$Now = Get-Date -Format yyyyMMdd_HHmmmss
$SourceDir = "\\appvsrv3\content$"
$HttpDir = "
http://appvpub.appv.demo.local/content"
$Group = $env:USERDOMAIN + "\Domain Users"
$LogFile = $env:Temp + "\Import_" + $Now + ".log"
Write-Host "Creating $LogFile" -Foregroundcolor Green
#New-Item $TargetRoot -type Dir | Out-Null
New-Item $LogFile -type File | Out-Null

#Loop through folder structure and find App-V packages

$Packages = Get-ChildItem -path $SourceDir -recurse | where{$_-match ‘.appv’}

ForEach ($Package in $Packages)
{
    #Import, Grant and Publish the package to the App-V Server   
    #Write-Host "Importing, Granting and Publishing package: [$Package]" -Foregroundcolor Green
    #Import-AppvServerPackage $Package.FullName.Replace($SourceDir, $HttpDir) | Grant-AppvServerPackage -Groups $Group | Publish-AppvServerPackage 2>&1 >> $LogFile | Out-Null

    #Import and Grant the package to the App-V Server. Not publishing! 
    Write-Host "Importing and granting: [$Package]" -Foregroundcolor Green
    Import-AppvServerPackage $Package.FullName.Replace($SourceDir, $HttpDir) | Grant-AppvServerPackage -Groups $Group 2>&1 >> $LogFile | Out-Null

    #Import the package to the App-V Server. The packages are not granted not published!
    #Write-Host "Importing: [$Package]" -Foregroundcolor Green
    #Import-AppvServerPackage $Package.FullName.Replace($SourceDir, $HttpDir) 2>&1 >> $LogFile | Out-Null
}

Write-Host "Check out the log file $LogFile for any warnings or errors" -ForegroundColor Green

As you can see having PowerShell as an automation framework foundation really extends the product and allows extreme customization!

I hope these scripts help you get started with App-V 5!