winget-cli

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

WinGetSourceSample.ps1 (1841B)


      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 WinGetSourceResource DSC resource.
      7         Requires PSDesiredStateConfiguration version 2.0.6
      8 
      9         IMPORTANT: This deletes the main winget source and add it again.
     10         Run as admin for set.
     11 #>
     12 
     13 #Requires -Modules Microsoft.WinGet.Client, Microsoft.WinGet.DSC
     14 
     15 using module Microsoft.WinGet.DSC
     16 using namespace System.Collections.Generic
     17 
     18 [CmdletBinding()]
     19 param (
     20     [Parameter()]
     21     [string]
     22     $SourceName = "winget",
     23 
     24     [Parameter()]
     25     [string]
     26     $Argument = "https://cdn.winget.microsoft.com/cache",
     27 
     28     [Parameter()]
     29     [string]
     30     $Type = ""
     31 )
     32 
     33 $resource = @{
     34     Name = 'WinGetSourceResource'
     35     ModuleName = 'Microsoft.WinGet.DSC'
     36     Property = @{
     37         Name = $SourceName
     38     }
     39 }
     40 
     41 $getResult = Invoke-DscResource @resource -Method Get
     42 Write-Host "Current sources"
     43 Format-List $getResult -Force
     44 
     45 $resource.Property = @{
     46     Argument = $Argument
     47     Type = $Type
     48 }
     49 
     50 # The default value comparison for test is Partial, so if you have the winget source this should succeed.
     51 $testResult = Invoke-DscResource @resource -Method Test
     52 if ($testResult.InDesiredState)
     53 {
     54     Write-Host "winget source is present"
     55 }
     56 else
     57 {
     58     Write-Host "winget source is not present"
     59     return
     60 }
     61 
     62 # Removing winget. Note this will fail if not run as admin.
     63 $resource.Property = @{
     64     Ensure = [Ensure]::Absent
     65 }
     66 
     67 Invoke-DscResource @resource -Method Set | Out-Null
     68 Write-Host "winget source removed"
     69 
     70 # Test again
     71 $testResult = Invoke-DscResource @resource -Method Test
     72 if (-not $testResult.InDesiredState)
     73 {
     74     Write-Host "winget source is still present."
     75 }
     76 else
     77 {
     78     Write-Host "winget was removed."
     79 }