The last couple of days there was quite a bit of ThinApp news !
- a new ThinApp, version 4.7.3 build 4.7.3.891762, which brought Windows 8 support
- a new ThinApp SDK, version 4.7.3 build 891762
- a new ThinApp Factory, version 0.3.2, which supports ThinApp 4.7.3
- and to top it all off, a ThinApp book from the hand of Peter Björk, one of the top contributors on the VMTN ThinApp Community
Time to post another ThinApp-PowerShell function.
Intro
With the ThinApp SDK you have to possibility to “query” your ThinApp packages from PowerShell. See my earlier Using the ThinApp SDK from PowerShell post for more info.
One parameter you can use in the package.ini file for a ThinApp package is the ExcludePattern. This parameter allows you to “…excludes files or directories during the application build process“. Which is a good idea, since we want to keep our ThinApp packages as slim as possible.
The obvious filetype candidates, and they appear in the examples in the ThinApp Package.ini Parameters Reference Guide and in Peter’s book, are .MSI and .CAB. When these type of files appear in a %temp% or in an Installer folder, it is obvious that you can remove them. But if there are some .MSI or .CAB files in other folders, then the decision is not so obvious.
The following script will help you investigate your existing packages, it will scan them for the filetypes you specified and report where these files were found.
The script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
function Get-TAFiles{ <# .SYNOPSIS Retrieves file info from ThinApp packages .DESCRIPTION The function will scan all ThinApp packages in one or more repositories, and find all files that are of a specific filetype. .NOTES Author: Luc Dekens .PARAMETER Repository One or more repositories where the ThinApp packages are stored .PARAMETER FileType The filetypes of the files the function should look for .EXAMPLE PS> Get-TAFiles -Repository D:\ThinAPp -FileType "*.msi" .EXAMPLE PS> $folders | Get-TAFiles -FileType "*.cab","*.msi" #> param( [CmdletBinding()] [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$Repository, [string[]]$FileType ) begin{ function Get-TAFilesInFolder { param($Folder) $folder.SubFolders |%{ Get-TAFilesInFolder -Folder $_ } $folder.Files | where {$_.Name -match $FileType} } $thinMgmt = New-Object -ComObject ThinApp.Management $FileType = [string]::Join('|',($FileType | %{$_ + '$'})).Replace('*','.*') } process{ foreach($folder in $Repository){ Get-ChildItem -Path $folder -Recurse -Include "*.exe","*.dat" | where{$thinMgmt.GetThinAppType($_.FullName) -eq 1} | %{ $package = $thinMgmt.OpenPackage($_.FullName) $fileSys = $package.GetVFileSystemObject(0) $rootFolder = $fileSys.GetFolder("") Get-TAFilesInFolder -Folder $rootFolder | %{ New-Object PSObject -Property @{ Repository = $folder PackageName = $package.InventoryName FileName = $_.Name FolderName = $_.ParentFolder.Path "Size (MB)" = [Math]::Round($_.Size/1MB,2) } } } } } } |
Annotations
Line 21: The name(s) of the repositories can be passed on the Repository parameter, which also accepts pipeline input.
Line 27-34: Scanning through a directory tree structure is by definition a recursive process. This small helper function, called Get-TAFilesInFolder, handles the recursive part. It will scan a specific folder if there are subfolders, and if yes, the function will call itself with that subfolder. Then it checks all the files in the folders and will only return the ones that are of the specific filetype.
Line 37: The FileType parameter needs to be converted to a RegEx expression so the -match operator can be used to filter the correct files.
Line 42: The outer loop looks recursively at each entry passed through the Repository parameter.
Line 43: The function looks at all the .EXE and .DAT files
Line 44: … and checks if they are ThinApp files.
Line 48: Starting from the “root” of the package’s filesystem, the function returns all files of the specified type.
Line 49-55: For each file that meets the specifications the function returns an object in the pipeline
Sample Run
The function is quite simple to use, just provide one or more ThinApp repository paths and specify which filetypes you want to look for. The result can be stored in a CSV file for reference.
1 2 3 4 5 6 7 8 |
$RepositoryRoot = "\\MyShare\ThinApp", "\\Srv1\ThinApp" $FileTypes = "*.cab","*.msi" Get-TAFiles -Repository $RepositoryRoot -FileType $FileTypes | Select Repository,PackageName,FileName,FolderName,"Size (MB)" | Export-Csv C:\TA-ExcludeFiles.csv -NoTypeInformation -UseCulture |
Note that I used a Select-Object before the Export-Csv cmdlet to guarantee the order of the exported properties. Note that this is not required anymore in PowerShell v3.
The resulting CSV file shows you clearly where the .MSI and .CAB files are located, and if it is safe to use the ExcludePattern parameter globally.
Time to start inserting this ExcludePattern parameter and do a rebuild !
I succeeded in saving more than 1GB of storage on a small ThinApp repository of some 20 packages.
Enjoy !