WinGetPackageResourceSample.ps1 (1861B)
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 WinGetPackage DSC resource. 7 Requires PSDesiredStateConfiguration v2 and enabling the 8 PSDesiredStateConfiguration.InvokeDscResource experimental feature 9 `Enable-ExperimentalFeature -Name PSDesiredStateConfiguration.InvokeDscResource` 10 IMPORTANT: This will install Microsoft.PowerToys. 11 #> 12 13 #Requires -Modules Microsoft.WinGet.Client, Microsoft.WinGet.DSC 14 15 using module Microsoft.WinGet.DSC 16 17 $resource = @{ 18 Name = 'WinGetPackage' 19 ModuleName = 'Microsoft.WinGet.DSC' 20 Property = @{ 21 Id = 'Microsoft.PowerToys' 22 Ensure = "Absent" 23 } 24 } 25 26 $testResult = Invoke-DscResource @resource -Method Test 27 if ($testResult.InDesiredState) 28 { 29 Write-Host "PowerToys is not installed." 30 } 31 else 32 { 33 Write-Host "PowerToys is installed." 34 } 35 36 # Default value of Ensure is present. 37 $resource = @{ 38 Name = 'WinGetPackage' 39 ModuleName = 'Microsoft.WinGet.DSC' 40 Property = @{ 41 Id = 'Microsoft.PowerToys' 42 Version = '0.65.0' 43 } 44 } 45 46 $testResult = Invoke-DscResource @resource -Method Test 47 if ($testResult.InDesiredState) 48 { 49 Write-Host "PowerToys 0.65.0 is installed." 50 } 51 else 52 { 53 Write-Host "PowerToys 0.65.0 is not installed." 54 55 Invoke-DscResource @resource -Method Set 56 } 57 58 $resource = @{ 59 Name = 'WinGetPackage' 60 ModuleName = 'Microsoft.WinGet.DSC' 61 Property = @{ 62 Id = 'Microsoft.PowerToys' 63 } 64 } 65 $testResult = Invoke-DscResource @resource -Method Test 66 if ($testResult.InDesiredState) 67 { 68 Write-Host "PowerToys latest version is installed." 69 } 70 else 71 { 72 Write-Host "PowerToys latest version is no installed." 73 74 Invoke-DscResource @resource -Method Set 75 }