Public/Find-NugetPackage.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# All credit and major props to Joel Bennett for this simplified solution that doesn't depend on PowerShellGet
# https://gist.github.com/Jaykul/1caf0d6d26380509b04cf4ecef807355
function Find-NugetPackage {
    <#
    .SYNOPSIS
        Query a Nuget feed for details on a package
 
    .FUNCTIONALITY
        CI/CD
 
    .DESCRIPTION
        Query a Nuget feed for details on a package
 
        We return:
            Name
            Author
            Version
            Uri
            Description
            Properties (A collection of even more properties)
 
    .PARAMETER Name
        Name of the nuget package
 
    .PARAMETER IsLatest
        Only return the latest package
 
    .PARAMETER Version
        Query this specific version of a package. Superceded by IsLatest
 
    .PARAMETER PackageSourceUrl
        Nuget PackageSourceUrl to query.
            PSGallery Module URL: https://www.powershellgallery.com/api/v2/ (default)
            PSGallery Script URL: https://www.powershellgallery.com/api/v2/items/psscript/
 
    .EXAMPLE
        Find-NugetPackage PSDepend -IsLatest
 
        # Get details on the latest PSDepend package from the PowerShell Gallery
 
    .EXAMPLE
        Find-NugetPackage Open-ISEFunction -PackageSourceUrl https://www.powershellgallery.com/api/v2/items/psscript/
 
        # Get details on the latest Open-ISEFunction package from the PowerShell Gallery scripts URI
 
    .EXAMPLE
        Find-NugetPackage PSDeploy
 
        # Get a list of every PSDeploy release on the PowerShell gallery feed
 
    .LINK
        https://github.com/RamblingCookieMonster/BuildHelpers
 
    .LINK
        about_BuildHelpers
    #>

    [CmdletBinding()]
    param(
        # The name of a package to find
        [Parameter(Mandatory)]
        $Name,
        # The repository api URL -- like https://www.powershellgallery.com/api/v2/ or https://www.nuget.org/api/v2/
        $PackageSourceUrl = 'https://www.powershellgallery.com/api/v2/',

        #If specified takes precedence over version
        [switch]$IsLatest,

        [string]$Version
    )

    #Ugly way to do this. Prefer islatest, otherwise look for version, otherwise grab all matching modules
    if($IsLatest)
    {
        Write-Verbose "Searching for latest [$name] module"
        $URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name' and IsLatestVersion"
    }
    elseif($PSBoundParameters.ContainsKey($Version))
    {
        Write-Verbose "Searching for version [$version] of [$name]"
        $URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name' and Version eq '$Version'"
    }
    else
    {
        Write-Verbose "Searching for all versions of [$name] module"
        $URI = "${PackageSourceUrl}Packages?`$filter=Id eq '$name'"
    }

    Invoke-RestMethod $URI | 
    Select-Object @{n='Name';ex={$_.title.('#text')}},
                  @{n='Author';ex={$_.author.name}},
                  @{n='Version';ex={$_.properties.NormalizedVersion}},
                  @{n='Uri';ex={$_.Content.src}},
                  @{n='Description';ex={$_.properties.Description}},
                  @{n='Properties';ex={$_.properties}}
}