|
| 1 | + |
| 2 | +<# |
| 3 | + .SYNOPSIS |
| 4 | + The `ChocolateySetting` DSC resource is used to set or unset Settings. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Chocolatey lets you set or unset general Settings. |
| 8 | + This resources lets you set or unset a Setting. |
| 9 | +
|
| 10 | + .PARAMETER Ensure |
| 11 | + Indicate whether the Chocolatey Setting should be enabled or disabled on the system. |
| 12 | +
|
| 13 | + .PARAMETER Name |
| 14 | + Name - the name of the Setting. |
| 15 | +
|
| 16 | + .PARAMETER Value |
| 17 | + Value - the value of the Setting, if ensure is set to present. |
| 18 | + When Ensure is absent, the setting's value is cleared. |
| 19 | +
|
| 20 | + .EXAMPLE |
| 21 | + Invoke-DscResource -ModuleName Chocolatey -Name ChocolateySetting -Method Get -Property @{ |
| 22 | + Ensure = 'Present' |
| 23 | + Name = 'webRequestTimeoutSeconds' |
| 24 | + Value = '30' |
| 25 | + } |
| 26 | +
|
| 27 | + # This example shows how to set the Setting webRequestTimeoutSeconds using Invoke-DscResource. |
| 28 | +#> |
| 29 | +[DscResource()] |
| 30 | +class ChocolateySetting |
| 31 | +{ |
| 32 | + [DscProperty()] |
| 33 | + [Ensure] $Ensure = 'Present' |
| 34 | + |
| 35 | + [DscProperty(Key)] |
| 36 | + [string] $Name |
| 37 | + |
| 38 | + [DscProperty()] |
| 39 | + [string] $Value |
| 40 | + |
| 41 | + [DscProperty(NotConfigurable)] |
| 42 | + [ChocolateyReason[]] $Reasons |
| 43 | + |
| 44 | + [ChocolateySetting] Get() |
| 45 | + { |
| 46 | + $currentState = [ChocolateySetting]::new() |
| 47 | + $currentState.Name = $this.Name |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + $setting = Get-ChocolateySetting -Name $this.Name |
| 52 | + $currentState.Value = $setting.Value |
| 53 | + if (-not [string]::IsNullOrEmpty($setting.Value)) |
| 54 | + { |
| 55 | + $currentState.Ensure = 'Present' |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + $currentState.Ensure = 'Absent' |
| 60 | + } |
| 61 | + } |
| 62 | + catch |
| 63 | + { |
| 64 | + Write-Verbose -Message ('Exception Caught:' -f $_) |
| 65 | + $Setting = $null |
| 66 | + $currentState.Ensure = 'Absent' |
| 67 | + $currentState.Reasons += @{ |
| 68 | + code = 'ChocolateySetting:ChocolateySetting:ChocolateyError' |
| 69 | + phrase = ('Error: {0}.' -f $_) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ($null -eq $Setting) |
| 74 | + { |
| 75 | + $currentState.Reasons += @{ |
| 76 | + code = 'ChocolateySetting:ChocolateySetting:SettingNotFound' |
| 77 | + phrase = ('The Setting ''{0}'' was not found.' -f $this.Name) |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + if ($this.Ensure -eq 'Present' -and $currentState.Value -eq $this.Value) |
| 83 | + { |
| 84 | + $currentState.Ensure = 'Present' |
| 85 | + $currentState.Reasons += @{ |
| 86 | + code = 'ChocolateySetting:ChocolateySetting:SettingCompliant' |
| 87 | + phrase = ('The Setting ''{0}'' is enabled with value ''{1}'' as expected.' -f $this.Name, $setting.Value) |
| 88 | + } |
| 89 | + } |
| 90 | + elseif (-not [string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent') |
| 91 | + { |
| 92 | + $currentState.Ensure = 'Present' |
| 93 | + $currentState.Reasons += @{ |
| 94 | + code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeUnset' |
| 95 | + phrase = ('The Setting ''{0}'' is Present while it''s expected to be unset (Absent). Currently set to ''{1}''.' -f $this.Name, $Setting.Value) |
| 96 | + } |
| 97 | + } |
| 98 | + elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent') |
| 99 | + { |
| 100 | + $currentState.Ensure = 'Absent' |
| 101 | + $currentState.Reasons += @{ |
| 102 | + code = 'ChocolateySetting:ChocolateySetting:SettingUnsetCompliant' |
| 103 | + phrase = ('The Setting ''{0}'' is unset as expected.' -f $this.Name) |
| 104 | + } |
| 105 | + } |
| 106 | + elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Present') |
| 107 | + { |
| 108 | + $currentState.Ensure = 'Absent' |
| 109 | + $currentState.Reasons += @{ |
| 110 | + code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeSet' |
| 111 | + phrase = ('The Setting ''{0}'' should be set.' -f $this.Name) |
| 112 | + } |
| 113 | + } |
| 114 | + elseif ([string]::isNullOrEmpty($Setting.value) -and [string]::isNullOrEmpty($this.value) -and $this.Ensure -eq 'Present') |
| 115 | + { |
| 116 | + $currentState.Ensure = 'Absent' |
| 117 | + $currentState.Reasons += @{ |
| 118 | + code = 'ChocolateySetting:ChocolateySetting:SettingIncorrectlySet' |
| 119 | + phrase = ('The Setting ''{0}'' should be set.' -f $this.Name) |
| 120 | + } |
| 121 | + } |
| 122 | + elseif ($this.Ensure -eq 'Present' -and $setting.Value -ne $this.Value) |
| 123 | + { |
| 124 | + $currentState.Ensure = 'Present' |
| 125 | + $currentState.Reasons += @{ |
| 126 | + code = 'ChocolateySetting:ChocolateySetting:SettingNotCorrect' |
| 127 | + phrase = ('The Setting ''{0}'' should be set to ''{1}'' but is ''{2}''.' -f $this.Name, $this.Value, $setting.Value) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return $currentState |
| 133 | + } |
| 134 | + |
| 135 | + [bool] Test() |
| 136 | + { |
| 137 | + $currentState = $this.Get() |
| 138 | + $currentState.Reasons.code.Where{ |
| 139 | + $_ -notmatch 'Compliant$' |
| 140 | + } |
| 141 | + |
| 142 | + if ($currentState.count -eq 0) |
| 143 | + { |
| 144 | + return $true |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + return $false |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + [void] Set() |
| 153 | + { |
| 154 | + $currentState = $this.Get() |
| 155 | + |
| 156 | + switch ($currentState.Reasons.code) |
| 157 | + { |
| 158 | + 'SettingShouldBeUnset' |
| 159 | + { |
| 160 | + # Unset the Setting |
| 161 | + Set-ChocolateySetting -Name $this.Name -Unset -Confirm:$false |
| 162 | + } |
| 163 | + |
| 164 | + 'SettingShouldBeSet$|SettingNotCorrect$' |
| 165 | + { |
| 166 | + # Configure the Setting |
| 167 | + Set-ChocolateySetting -Name $this.Name -Value $this.Value -Confirm:$false |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments