winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Find-WinGetPackage.ps1 (5276B)


      1 Function Find-WinGetPackage{
      2     <#
      3         .SYNOPSIS
      4         Searches for a package on configured sources. 
      5         Additional options can be provided to filter the output, much like the search command.
      6         
      7         .DESCRIPTION
      8         By running this cmdlet with the required inputs, it will retrieve the packages installed on the local system.
      9 
     10         .PARAMETER Filter
     11         Used to search across multiple fields of the package.
     12         
     13         .PARAMETER Id
     14         Used to specify the Id of the package
     15 
     16         .PARAMETER Name
     17         Used to specify the Name of the package
     18 
     19         .PARAMETER Moniker
     20         Used to specify the Moniker of the package
     21 
     22         .PARAMETER Tag
     23         Used to specify the Tag of the package
     24         
     25         .PARAMETER Command
     26         Used to specify the Command of the package
     27         
     28         .PARAMETER Exact
     29         Used to specify an exact match for any parameters provided. Many of the other parameters may be used for case-insensitive substring matches if Exact is not specified.
     30 
     31         .PARAMETER Source
     32         Name of the Windows Package Manager private source. Can be identified by running: "Get-WinGetSource" and using the source Name
     33 
     34         .PARAMETER Count
     35         Used to specify the maximum number of packages to return
     36 
     37         .PARAMETER Header
     38         Used to specify the value to pass as the "Windows-Package-Manager" HTTP header for a REST source.
     39 
     40         .PARAMETER VerboseLog
     41         Used to provide verbose logging for the Windows Package Manager.
     42         
     43         .PARAMETER AcceptSourceAgreement
     44         Used to accept any source agreement required for the source.
     45 
     46         .EXAMPLE
     47         Find-WinGetPackage -id "Publisher.Package"
     48 
     49         This example searches for a package containing "Publisher.Package" as a valid identifier on all configured sources.
     50 
     51         .EXAMPLE
     52         Find-WinGetPackage -id "Publisher.Package" -source "Private"
     53 
     54         This example searches for a package containing "Publisher.Package" as a valid identifier from the source named "Private".
     55 
     56         .EXAMPLE
     57         Find-WinGetPackage -Name "Package"
     58 
     59         This example searches for a package containing "Package" as a valid name on all configured sources.
     60     #>
     61     PARAM(
     62         [Parameter(Position=0)] $Filter,
     63         [Parameter()]           $Id,
     64         [Parameter()]           $Name,
     65         [Parameter()]           $Moniker,
     66         [Parameter()]           $Tag,
     67         [Parameter()]           $Command,
     68         [Parameter()] [switch]  $Exact,
     69         [Parameter()]           $Source,
     70         [Parameter()] [ValidateRange(1, [int]::maxvalue)][int]$Count,
     71         [Parameter()] [ValidateLength(1, 1024)]$Header,
     72         [Parameter()] [switch]  $VerboseLog,
     73         [Parameter()] [switch]  $AcceptSourceAgreement
     74     )
     75     BEGIN
     76     {
     77         [string[]]          $WinGetArgs  = @("Search")
     78         [WinGetPackage[]]   $Result      = @()
     79         [string[]]          $IndexTitles = @("Name", "Id", "Version", "Available", "Source")
     80 
     81         if($PSBoundParameters.ContainsKey('Filter')){
     82             ## Search across Name, ID, moniker, and tags
     83             $WinGetArgs += $Filter
     84         }
     85         if($PSBoundParameters.ContainsKey('Id')){
     86             ## Search for the ID
     87             $WinGetArgs += "--Id", $Id.Replace("…", "")
     88         }
     89         if($PSBoundParameters.ContainsKey('Name')){
     90             ## Search for the Name
     91             $WinGetArgs += "--Name", $Name.Replace("…", "")
     92         }
     93         if($PSBoundParameters.ContainsKey('Moniker')){
     94             ## Search for the Moniker
     95             $WinGetArgs += "--Moniker", $Moniker.Replace("…", "")
     96         }
     97         if($PSBoundParameters.ContainsKey('Tag')){
     98             ## Search for the Tag
     99             $WinGetArgs += "--Tag", $Tag.Replace("…", "")
    100         }
    101         if($PSBoundParameters.ContainsKey('Command')){
    102             ## Search for the Moniker
    103             $WinGetArgs += "--Command", $Command.Replace("…", "")
    104         }
    105         if($Exact){
    106             ## Search using exact values specified (case-sensitive)
    107             $WinGetArgs += "--Exact"
    108         }
    109         if($PSBoundParameters.ContainsKey('Source')){
    110             ## Search for the Source
    111             $WinGetArgs += "--Source", $Source.Replace("…", "")
    112         }
    113         if($PSBoundParameters.ContainsKey('Count')){
    114             ## Specify the number of results to return
    115             $WinGetArgs += "--Count", $Count
    116         }
    117         if($PSBoundParameters.ContainsKey('Header')){
    118             ## Pass the value specified as the Windows-Package-Manager HTTP header
    119             $WinGetArgs += "--header", $Header
    120         }
    121         if($PSBoundParameters.ContainsKey('VerboseLog')){
    122             ## Search using exact values specified (case-sensitive)
    123             $WinGetArgs += "--VerboseLog", $VerboseLog
    124         }
    125         if($AcceptSourceAgreement){
    126             ## Accept source agreements
    127             $WinGetArgs += "--accept-source-agreements"
    128         }
    129     }
    130     PROCESS
    131     {
    132         $List = Invoke-WinGetCommand -WinGetArgs $WinGetArgs -IndexTitles $IndexTitles
    133     
    134         foreach ($Obj in $List) {
    135             $Result += [WinGetPackage]::New($Obj) 
    136         }
    137     }
    138     END
    139     {
    140         return $Result
    141     }
    142 }
    143 
    144 Export-ModuleMember -Function Find-WinGetPackage