winget-cli

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

WinGetAdminSettingsResourceSample.ps1 (1593B)


      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 WinGetAdminSettings DSC resource.
      7         Requires PSDesiredStateConfiguration version 2.0.6
      8         
      9         IMPORTANT: This will leave LocalManifestFiles enabled
     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 $resource = @{
     19     Name = 'WinGetAdminSettings'
     20     ModuleName = 'Microsoft.WinGet.DSC'
     21     Property = @{
     22     }
     23 }
     24 
     25 $getResult = Invoke-DscResource @resource -Method Get
     26 Write-Host "Current sources"
     27 $getResult.Settings
     28 
     29 $expectedSources = [List[Hashtable]]::new()
     30 $expectedSources.Add(@{
     31     Name = "winget"
     32     Arg = "https://cdn.winget.microsoft.com/cache"
     33 })
     34 
     35 # Lets see if LocalManifestFiles is enabled
     36 $resource.Property = @{
     37     Settings = @{
     38         LocalManifestFiles = $true
     39     }
     40 }
     41 
     42 $testResult = Invoke-DscResource @resource -Method Test
     43 if (-not $testResult.InDesiredState)
     44 {
     45     Write-Host "LocalManifestFiles is disabled, enabling"
     46     Invoke-DscResource @resource -Method Set | Out-Null
     47 
     48     # Now try again
     49     $testResult2 = Invoke-DscResource @resource -Method Test
     50     if (-not $testResult.InDesiredState)
     51     {
     52         Write-Host "LocalManifestFiles is now enabled"
     53     }
     54     else
     55     {
     56         Write-Host "Is there a bug somewhere?"
     57         return
     58     }
     59 }
     60 else
     61 {
     62     Write-Host "LocalManifestFiles is already enabled"
     63 }