functions/Get-ChocoPackageMetaData.ps1

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
Function Get-ChocoPackageMetaData {
    <#
    .SYNOPSIS
    Return package metadata from a given Chocolatey Package(s)
 
    .DESCRIPTION
    Reads the contents of the nupkg and extracts metadata from the nuspec contained within it
 
    .PARAMETER ChocolateyPackage
    The chocolatey package(s) you wish to extract data from
 
    .EXAMPLE
    Get-ChocoPackageMetaData -ChocolateyPackage C:\Packages\googlechrome.nupkg
 
    .NOTES
    Written by Stephen Valdinger of Chocolatey Software for Dan Franciscus
    #>


    [cmdletBinding()]
    Param(
        [ValidateScript({Test-Path $_})]
        [String[]]
        $ChocolateyPackage
    )

    begin { $null = Add-Type -Assemblyname "System.IO.Compression.Filesystem" }

    process {
        Foreach($package in $ChocolateyPackage){
            $obj = @{}
            $entry =  [IO.Compression.Zipfile]::OpenRead($package).Entries |
            Where-Object { $_.Name -match "nuspec" }
            $stream = $entry.Open()
            $reader = New-Object IO.StreamReader($stream)
            $text = $reader.ReadToEnd()
            [xml]$xml = $text
            $obj.Add("Name","$($xml.package.metadata.id)")
            $obj.Add("Version","$($xml.package.metadata.version)")
            $reader.Close()
            $stream.Close()
            [pscustomobject]$obj

        }
    }
}