winget-cli

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

Install-WinGetPackage.ps1 (6817B)


      1 Function Install-WinGetPackage
      2 {
      3     <#
      4         .SYNOPSIS
      5         Installs a package on the local system. 
      6         Additional options can be provided to filter the output, much like the search command.
      7         
      8         .DESCRIPTION
      9         By running this cmdlet with the required inputs, it will retrieve the packages installed on the local system.
     10 
     11         .PARAMETER Filter
     12         Used to search across multiple fields of the package.
     13         
     14         .PARAMETER Id
     15         Used to specify the Id of the package
     16 
     17         .PARAMETER Name
     18         Used to specify the Name of the package
     19 
     20         .PARAMETER Moniker
     21         Used to specify the Moniker of the package
     22 
     23         .PARAMETER Tag
     24         Used to specify the Tag of the package
     25         
     26         .PARAMETER Command
     27         Used to specify the Command of the package
     28 
     29         .PARAMETER Scope
     30         Used to specify install scope (user or machine)
     31         
     32         .PARAMETER Exact
     33         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.
     34 
     35         .PARAMETER Source
     36         Name of the Windows Package Manager private source. Can be identified by running: "Get-WinGetSource" and using the source Name
     37 
     38         .PARAMETER Interactive
     39         Used to specify the installer should be run in interactive mode.
     40 
     41         .PARAMETER Silent
     42         Used to specify the installer should be run in silent mode with no user input.
     43 
     44         .PARAMETER Locale
     45         Used to specify the locale for localized package installer.
     46 
     47         .PARAMETER Log
     48         Used to specify the location for the log location if it is supported by the package installer.
     49 
     50         .PARAMETER Header
     51         Used to specify the value to pass as the "Windows-Package-Manager" HTTP header for a REST source.
     52 
     53         .PARAMETER Version
     54         Used to specify the Version of the package
     55 
     56         .PARAMETER VerboseLog
     57         Used to provide verbose logging for the Windows Package Manager.
     58         
     59         .PARAMETER AcceptPackageAgreement
     60         Used to accept any package agreement required for the package.
     61         
     62         .PARAMETER AcceptSourceAgreement
     63         Used to explicitly accept any agreement required by the source.
     64 
     65         .PARAMETER Local
     66         Used to install from a local manifest
     67 
     68         .EXAMPLE
     69         Install-WinGetPackage -id "Publisher.Package"
     70 
     71         This example expects only a single package containing "Publisher.Package" as a valid identifier.
     72 
     73         .EXAMPLE
     74         Install-WinGetPackage -id "Publisher.Package" -source "Private"
     75 
     76         This example expects the source named "Private" contains a package with "Publisher.Package" as a valid identifier.
     77 
     78         .EXAMPLE
     79         Install-WinGetPackage -Name "Package"
     80 
     81         This example expects a configured source contains a package with "Package" as a valid name.
     82     #>
     83 
     84     PARAM(
     85         [Parameter(Position=0)] $Filter,
     86         [Parameter()]           $Name,
     87         [Parameter()]           $Id,
     88         [Parameter()]           $Moniker,
     89         [Parameter()]           $Source,
     90         [Parameter()] [ValidateSet("User", "Machine")] $Scope,
     91         [Parameter()] [switch]  $Interactive,
     92         [Parameter()] [switch]  $Silent,
     93         [Parameter()] [string]  $Version,
     94         [Parameter()] [switch]  $Exact,
     95         [Parameter()] [switch]  $Override,
     96         [Parameter()] [System.IO.FileInfo]  $Location,
     97         [Parameter()] [switch]  $Force,
     98         [Parameter()] [ValidatePattern("^([a-zA-Z]{2,3}|[iI]-[a-zA-Z]+|[xX]-[a-zA-Z]{1,8})(-[a-zA-Z]{1,8})*$")] [string] $Locale,
     99         [Parameter()] [System.IO.FileInfo]  $Log, ## This is a path of where to create a log.
    100         [Parameter()] [switch]  $AcceptSourceAgreements,
    101         [Parameter()] [switch]  $Local # This is for installing local manifests
    102     )
    103     BEGIN
    104     {
    105         $WinGetFindArgs = @{}
    106         [string[]] $WinGetInstallArgs  = "Install"
    107         IF($PSBoundParameters.ContainsKey('Filter')){
    108             IF($Local) {
    109                 $WinGetInstallArgs += "--Manifest"
    110             }
    111             $WinGetInstallArgs += $Filter
    112         }
    113         IF($PSBoundParameters.ContainsKey('Filter')){
    114             IF($Local) {
    115                 $WinGetInstallArgs += "--Manifest"
    116             }
    117             $WinGetInstallArgs += $Filter
    118             $WinGetFindArgs.Add('Filter', $Filter)
    119         }
    120         IF($PSBoundParameters.ContainsKey('Name')){
    121             $WinGetInstallArgs += "--Name", $Name
    122             $WinGetFindArgs.Add('Name', $Name)
    123         }
    124         IF($PSBoundParameters.ContainsKey('Id')){
    125             $WinGetInstallArgs += "--Id", $Id
    126             $WinGetFindArgs.Add('Id', $Id)
    127         }
    128         IF($PSBoundParameters.ContainsKey('Moniker')){
    129             $WinGetInstallArgs += "--Moniker", $Moniker
    130             $WinGetFindArgs.Add('Moniker', $Moniker)
    131         }
    132         IF($PSBoundParameters.ContainsKey('Source')){
    133             $WinGetInstallArgs += "--Source", $Source
    134             $WinGetFindArgs.Add('Source', $Source)
    135         }
    136         IF($PSBoundParameters.ContainsKey('Scope')){
    137             $WinGetInstallArgs += "--Scope", $Scope
    138         }
    139         IF($Interactive){
    140             $WinGetInstallArgs += "--Interactive"
    141         }
    142         IF($Silent){
    143             $WinGetInstallArgs += "--Silent"
    144         }
    145         IF($PSBoundParameters.ContainsKey('Locale')){
    146             $WinGetInstallArgs += "--locale", $Locale
    147         }
    148         if($PSBoundParameters.ContainsKey('Version')){
    149             $WinGetInstallArgs += "--Version", $Version
    150         }
    151         if($Exact){
    152             $WinGetInstallArgs += "--Exact"
    153             $WinGetFindArgs.Add('Exact', $true)
    154         }
    155         if($PSBoundParameters.ContainsKey('Log')){
    156             $WinGetInstallArgs += "--Log", $Log
    157         }
    158         if($PSBoundParameters.ContainsKey('Override')){
    159             $WinGetInstallArgs += "--override", $Override
    160         }
    161         if($PSBoundParameters.ContainsKey('Location')){
    162             $WinGetInstallArgs += "--Location", $Location
    163         }
    164         if($Force){
    165             $WinGetInstallArgs += "--Force"
    166         }
    167     }
    168     PROCESS
    169     {
    170         ## Exact, ID and Source - Talk with Demitrius tomorrow to better understand this.
    171         IF(!$Local) {
    172             $Result = Find-WinGetPackage @WinGetFindArgs
    173         }
    174 
    175         if($Result.count -eq 1 -or $Local) {
    176             & "WinGet" $WinGetInstallArgs
    177             $Result = ""
    178         }
    179         elseif($Result.count -lt 1){
    180             Write-Host "Unable to locate package for installation"
    181             $Result = ""
    182         }
    183         else {
    184             Write-Host "Multiple packages found matching input criteria. Please refine the input."
    185         }
    186     }
    187     END
    188     {
    189         return $Result
    190     }
    191 }
    192 
    193 Export-ModuleMember -Function Install-WinGetPackage