|
| 1 | +# Copyright (c) 2017 Chocolatey Software, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +<# |
| 16 | +.Description |
| 17 | +Returns the configuration for cChocoFeature. |
| 18 | +
|
| 19 | +.Example |
| 20 | +Get-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present' |
| 21 | +#> |
| 22 | +function Get-TargetResource |
| 23 | +{ |
| 24 | + [CmdletBinding()] |
| 25 | + [OutputType([System.Collections.Hashtable])] |
| 26 | + param |
| 27 | + ( |
| 28 | + [parameter(Mandatory = $true)] |
| 29 | + [System.String] |
| 30 | + $FeatureName, |
| 31 | + |
| 32 | + [ValidateSet('Present','Absent')] |
| 33 | + [System.String] |
| 34 | + $Ensure='Present' |
| 35 | + ) |
| 36 | + |
| 37 | + Write-Verbose "Starting cChocoFeature Get-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure" |
| 38 | + |
| 39 | + $returnValue = @{ |
| 40 | + FeatureName = $FeatureName |
| 41 | + Ensure = $Ensure |
| 42 | + } |
| 43 | + |
| 44 | + $returnValue |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +<# |
| 49 | +.Description |
| 50 | +Performs the set for the cChocoFeature resource. |
| 51 | +
|
| 52 | +.Example |
| 53 | +Get-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present' |
| 54 | +
|
| 55 | +#> |
| 56 | +function Set-TargetResource |
| 57 | +{ |
| 58 | + [CmdletBinding(SupportsShouldProcess=$true)] |
| 59 | + param |
| 60 | + ( |
| 61 | + [parameter(Mandatory = $true)] |
| 62 | + [System.String] |
| 63 | + $FeatureName, |
| 64 | + |
| 65 | + [ValidateSet('Present','Absent')] |
| 66 | + [string] |
| 67 | + $Ensure='Present' |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | + Write-Verbose "Starting cChocoFeature Set-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure." |
| 72 | + |
| 73 | + if ($pscmdlet.ShouldProcess("Choco feature $FeatureName will be ensured $Ensure.")) |
| 74 | + { |
| 75 | + if ($Ensure -eq 'Present') |
| 76 | + { |
| 77 | + Write-Verbose "Enabling choco feature $FeatureName." |
| 78 | + choco feature enable -n $FeatureName |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + Write-Verbose "Disabling choco feature $FeatureName." |
| 83 | + choco feature disable -n $FeatureName |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +<# |
| 90 | +.Description |
| 91 | +Performs the test for cChocoFeature. |
| 92 | +
|
| 93 | +.Example |
| 94 | +Test-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present' |
| 95 | +#> |
| 96 | +function Test-TargetResource |
| 97 | +{ |
| 98 | + [CmdletBinding()] |
| 99 | + [OutputType([System.Boolean])] |
| 100 | + param |
| 101 | + ( |
| 102 | + [parameter(Mandatory = $true)] |
| 103 | + [System.String] |
| 104 | + $FeatureName, |
| 105 | + |
| 106 | + [ValidateSet('Present','Absent')] |
| 107 | + [System.String] |
| 108 | + $Ensure='Present' |
| 109 | + ) |
| 110 | + |
| 111 | + Write-Verbose "Starting cChocoFeature Test-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure." |
| 112 | + |
| 113 | + $result = $false |
| 114 | + $feature = Get-ChocoFeature -FeatureName $FeatureName | Where-Object {$_.State -eq "Enabled"} |
| 115 | + |
| 116 | + if (($Ensure -eq 'Present' -and ([bool]$feature)) -or ($Ensure -eq 'Absent' -and !([bool]$feature))) |
| 117 | + { |
| 118 | + Write-Verbose "Test-TargetResource is true, $FeatureName is $Ensure." |
| 119 | + $result = $true |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + Write-Verbose "Test-TargetResource is false, $FeatureName is not $Ensure." |
| 124 | + } |
| 125 | + |
| 126 | + return $result |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +<# |
| 131 | +.Description |
| 132 | +Query chocolatey features. |
| 133 | +#> |
| 134 | +function Get-ChocoFeature |
| 135 | +{ |
| 136 | + [OutputType([PSCustomObject])] |
| 137 | + param( |
| 138 | + [string] |
| 139 | + $FeatureName |
| 140 | + ) |
| 141 | + choco feature -r | ConvertFrom-Csv -Delimiter "|" -Header Name, State, Description | Where-Object {$_.Name -eq $FeatureName} |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +Export-ModuleMember -Function *-TargetResource |
| 148 | + |
0 commit comments