private/Get-AppXManifest.ps1
<#
.SYNOPSIS This function extracts the AppxManifest.xml file from a given package and returns its contents as an XML object. .DESCRIPTION The Get-AppXManifest function extracts the AppxManifest.xml file from a given package and returns its contents as an XML object. .PARAMETER Filename Specifies the path to the package from which to extract the AppxManifest.xml file. .PARAMETER Retries Specifies the number of times to retry opening the package. The default value is 3. .EXAMPLE PS C:\> Get-AppXManifest -Filename "C:\Packages\MyApp.msix" Returns the contents of the AppxManifest.xml file from the MyApp.msix package as an XML object. .EXAMPLE PS C:\> Get-AppXManifest -Filename "C:\Packages\MyApp.appx" Returns the contents of the AppxManifest.xml file from the MyApp.appx package as an XML object. .NOTES Function : Get-AppXManifest Author : John Billekens Copyright: Copyright (c) AppVentiX Version : 1.0 #> function Get-AppXManifest { [CmdletBinding()] param( [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0 )] [Alias("Path")] [string]$Filename, [Int]$Retries = 3 ) Begin { Add-Type -Assembly System.IO.Compression.FileSystem } Process { if (Test-Path -Path $Filename) { while ($Retries -gt 0) { try { $zipFile = [io.compression.zipfile]::OpenRead($Filename) break } catch { $Retries-- if ($Retries -eq 0) { throw $_ } else { Write-Warning "Failed to open package $Filename, retrying..." Start-Sleep -Seconds 3 } } } $manifest = $zipFile.Entries | Where-Object name -Like 'AppxManifest.xml' if ($manifest.Name -like 'AppxManifest.xml') { $stream = $manifest.Open() $streamReader = New-Object IO.StreamReader($stream) [xml]$xml = $streamReader.ReadToEnd() $streamReader.Close() $stream.Close() $zipFile.Dispose() Write-Output $($xml) } else { Throw [System.Management.Automation.ItemNotFoundException] "AppxManifest.xml not found in $(Split-Path -Path $Filename -Leaf)" } } else { Throw [System.Management.Automation.ItemNotFoundException] "File not found: `"$Filename`"" } } End { Clear-Variable Filename, zipFile, xml -ErrorAction SilentlyContinue } } |