PowerShell: Get version and ProductId from an .app package
By Anatoly Mironov
In my project I deploy some apps directly through the ObjectModel directly with PowerShell. The apps are built with TFS I have a script that installs or updates apps if there is a new version of the app. Previously I used Import-SPAppPackage to compare the version and productid with an existing app instance, but often I get this error:
The provided App differs from another App with the same version and product ID.
Here you have the issue. Now the solution is to read the AppManifest.xml. But first the .app package has to be extracted like a usual zip file. This is the powershell function for that: [code language=“powershell”] function Get-SPAppPackageMetadata { <# .SYNOPSIS Gets the version and productid of an app package .DESCRIPTION This function extracts the AppManifest.xml from the app and gets the Version and the ProductId .EXAMPLE Get-SPAppPackageMetadata- AppPackagePath “C:\folder\app.publish\1.0.0.1\App1.app” .PARAMETER AppPackagePath The path to the new version of the app .Notes Name: Get-SPAppPackageVersion Author: Anatoly Mironov Last Edit: 2013-12-05 Keywords: SPApp, SPAppInstance .Link http://chuvash.eu #> [CmdLetBinding()] param([Parameter(Mandatory=$true)][string]$AppPackagePath) $shell = new-object -com shell.application $item = get-item $AppPackagePath $zipFilePath = $item.FullName + “.zip” $directory = $item.Directory.FullName Copy-Item $item $zipFilePath $manifest = @{ “Name” = “AppManifest.xml” } $manifest.Path = Join-Path $directory $manifest.Name $manifest.File = $shell.NameSpace($zipFilePath).Items() | ? { $_.Name -eq $manifest.Name } $shell.Namespace($directory).CopyHere($manifest.File) $manifest.Xml = [xml](get-content $manifest.Path) $metadata = @{ “VersionString” = $manifest.Xml.App.Version “ProductId” = $manifest.Xml.App.ProductID } Remove-Item $zipFilePath Remove-Item $manifest.Path return $metadata } [/code]