winget-cli

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

WinGetUserSettingsPackageManagerSample.ps1 (1895B)


      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 WinGetUserSettings DSC resource.
      7         Requires PSDesiredStateConfiguration version 2.0.6
      8 
      9         IMPORTANT: If you loaded the released modules this will modify your settings.
     10                    Use the -Restore to get back to your original settings
     11 
     12     .PARAMETER Restore
     13         Restore back to the original user settings.
     14 #>
     15 
     16 #Requires -Modules Microsoft.WinGet.Client, Microsoft.WinGet.DSC
     17 
     18 using module Microsoft.WinGet.DSC
     19 
     20 [CmdletBinding()]
     21 param (
     22     [Parameter()]
     23     [switch]
     24     $Restore
     25 )
     26 
     27 $resource = @{
     28     Name = 'WinGetUserSettings'
     29     ModuleName = 'Microsoft.WinGet.DSC'
     30     Property = @{
     31     }
     32 }
     33 
     34 # Get current settings
     35 $getResult = Invoke-DscResource @resource -Method Get
     36 Write-Host "Current Settings"
     37 $settingsBackup = $getResult.Settings
     38 $getResult.Settings | ConvertTo-Json
     39 
     40 # Test if telemetry is disabled
     41 $resource.Property = @{
     42     Settings = @{
     43         telemetry = @{
     44             disable = $false
     45         }
     46     }
     47     # If you want to check that this setting is the only setting set use [WinGetAction]::Full
     48     Action = [WinGetAction]::Partial
     49 }
     50 
     51 $testResult = Invoke-DscResource @resource -Method Test
     52 if (-not $testResult.InDesiredState)
     53 {
     54     Write-Host "Adding telemetry setting"
     55     Invoke-DscResource @resource -Method Set | Out-Null
     56 
     57     Write-Host "New settings"
     58     $getResult = Invoke-DscResource @resource -Method Get
     59     $getResult.Settings | ConvertTo-Json
     60 }
     61 else
     62 {
     63     Write-Host "Telemetry is already disabled"
     64 }
     65 
     66 if ($Restore)
     67 {
     68     $resource.Property.Settings = $settingsBackup
     69     $resource.Property.Action = [WinGetAction]::Full
     70     Invoke-DscResource @resource -Method Set | Out-Null
     71     Write-Host "Settings restored."
     72 }