winget-cli

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

WinGetPackageManagerSample.ps1 (3596B)


      1 # Copyright (c) Microsoft Corporation. All rights reserved.
      2 # Licensed under the MIT License.
      3 
      4 <#
      5     .SYNOPSIS
      6         Simple sample on how to use WinGetIntegrity DSC resource.
      7         Requires PSDesiredStateConfiguration version 2.0.6
      8 
      9         IMPORTANT: This will modify the winget you have installed
     10 #>
     11 
     12 #Requires -Modules Microsoft.WinGet.Client, Microsoft.WinGet.DSC
     13 
     14 using module Microsoft.WinGet.DSC
     15 
     16 $resource = @{
     17     Name = 'WinGetPackageManager'
     18     ModuleName = 'Microsoft.WinGet.DSC'
     19     Property = @{
     20     }
     21 }
     22 
     23 # This just demonstrate the Get command.
     24 $getResult = Invoke-DscResource @resource -Method Get
     25 
     26 if (-not([string]::IsNullOrWhiteSpace($getResult.Version)))
     27 {
     28     Write-Host "Current winget version $($getResult.Version)"
     29 }
     30 else
     31 {
     32     # If the result of get contains an empty version, it means that winget is not installed. This is not the right
     33     # way to do it though, is better to call Test with an empty Version property.
     34     if (-not (Invoke-DscResource @resource -Method Test).InDesiredState)
     35     {
     36         Write-Host "winget is not installed"
     37     }
     38     else
     39     {
     40         Write-Error "BUG BUG!!"
     41         return
     42     }
     43 }
     44 
     45 # At the time I'm doing this the second latest released winget version is v1.3.2091. Lets assume you want to stay there forever.
     46 $v132091 = "v1.3.2091"
     47 $resource = @{
     48     Name = 'WinGetPackageManager'
     49     ModuleName = 'Microsoft.WinGet.DSC'
     50     Property = @{
     51         Version = $v132091
     52     }
     53 }
     54 
     55 $testResult = Invoke-DscResource @resource -Method Test
     56 if ($testResult.InDesiredState)
     57 {
     58     Write-Host "winget is already in a good state (aka in version $v132091)"
     59 }
     60 else
     61 {
     62     # Oh no, we are not in a good state. Lets get you there.
     63     # Internally, Set calls Repair-WinGet -Version v1.3.2691 which means that it will try to repair winget
     64     # by downloading v1.3.2691 and installing it if needed. For example, if your AppInstaller is not registered
     65     # it will register the package and then verify the specified version is installed.
     66     Invoke-DscResource @resource -Method Set | Out-Null
     67 
     68     # Now this should work.
     69     $testResult = Invoke-DscResource @resource -Method Test
     70     if ($testResult.InDesiredState)
     71     {
     72         Write-Host "winget is in a good state (aka in version $v132091)"
     73     }
     74     else
     75     {
     76         Write-Error "BUG BUG!!"
     77         return
     78     }
     79 }
     80 
     81 # Now, lets say that you want to have always the latest winget installed. You can specify UseLatest.
     82 $resource = @{
     83     Name = 'WinGetPackageManager'
     84     ModuleName = 'Microsoft.WinGet.DSC'
     85     Property = @{
     86         UseLatest = $true
     87     }
     88 }
     89 $testResult = Invoke-DscResource @resource -Method Test
     90 if ($testResult.InDesiredState)
     91 {
     92     Write-Host "winget version is the latest version"
     93 }
     94 else
     95 {
     96     Write-Host "winget version is not latest version"
     97 }
     98 
     99 # You can also do UseLatestPreRelease
    100 $resource = @{
    101     Name = 'WinGetPackageManager'
    102     ModuleName = 'Microsoft.WinGet.DSC'
    103     Property = @{
    104         UseLatestPreRelease = $true
    105     }
    106 }
    107 $testResult = Invoke-DscResource @resource -Method Test
    108 if ($testResult.InDesiredState)
    109 {
    110     Write-Host "winget version is the latest prerelease version"
    111 }
    112 else
    113 {
    114     # Get the latest prerelease.
    115     Invoke-DscResource @resource -Method Set | Out-Null
    116 
    117     $testResult = Invoke-DscResource @resource -Method Test
    118     if ($testResult.InDesiredState)
    119     {
    120         Write-Host "winget version is the latest prerelease version"
    121     }
    122     else
    123     {
    124         Write-Error "BUG BUG!!"
    125         return
    126     }
    127 }