diff --git a/.pipelines/public-azure-pipeline.yaml b/.pipelines/public-azure-pipeline.yaml index 140ab0a..77f614a 100644 --- a/.pipelines/public-azure-pipeline.yaml +++ b/.pipelines/public-azure-pipeline.yaml @@ -72,8 +72,8 @@ stages: displayName: 'Run Tests' inputs: filePath: './build.ps1' - arguments: '-tasks hqrmtest' - pwsh: true + arguments: '-tasks ipmopsdsc1,hqrmtest' + pwsh: false - task: PublishTestResults@2 displayName: 'Publish Test Results' diff --git a/CHANGELOG.md b/CHANGELOG.md index 7339950..1830eed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Changed `ChocolateySoftware` to be class-based DSC Resource. - Changed `ChocolateyPackage` to be class-based DSC Resource. - Changed `ChocolateySource` to be a class-based DSC Resource. +- Changed `ChocolateyFeature` to be a class-based DSC Resource. ### Added diff --git a/build.yaml b/build.yaml index f494a5c..ab43224 100644 --- a/build.yaml +++ b/build.yaml @@ -5,7 +5,7 @@ BuiltModuleSubdirectory: module CopyPaths: - en-US - - DscResources + # - DscResources # - Modules Encoding: UTF8 # Can be used to manually specify module's semantic version if the preferred method of @@ -52,6 +52,10 @@ BuildWorkflow: # - Set_PSModulePath - Invoke_HQRM_Tests_Stop_On_Fail + ipmopsdsc1: | + { + Import-Module -Name PSDesiredStateConfiguration -MaximumVersion 1.99 -Passthru + } gcpol: - build_guestconfiguration_packages diff --git a/source/Classes/005.ChocolateyFeature.ps1 b/source/Classes/005.ChocolateyFeature.ps1 new file mode 100644 index 0000000..c2cf5dd --- /dev/null +++ b/source/Classes/005.ChocolateyFeature.ps1 @@ -0,0 +1,141 @@ + +<# + .SYNOPSIS + The `ChocolateyFeature` DSC resource is used to enable or disable features. + + .DESCRIPTION + Chocolatey configuration lets you enable or disabled features, but while some are + set by defaults. + This resources lets you enable or disable a feature, but also tells you if it's been + set or just left as default. + + .PARAMETER Ensure + Indicate whether the Chocolatey feature should be enabled or disabled on the system. + + .PARAMETER Name + Name - the name of the feature. + + .EXAMPLE + Invoke-DscResource -ModuleName Chocolatey -Name ChocolateyFeature -Method Get -Property @{ + Ensure = 'Absent' + Name = 'showDownloadProgress' + } + + # This example shows how to disable the feature showDownloadProgress using Invoke-DscResource. +#> +[DscResource()] +class ChocolateyFeature +{ + [DscProperty()] + [Ensure] $Ensure = 'Present' + + [DscProperty(Key)] + [string] $Name + + [DscProperty(NotConfigurable)] + [ChocolateyReason[]] $Reasons + + [ChocolateyFeature] Get() + { + $currentState = [ChocolateyFeature]::new() + $currentState.Name = $this.Name + + try + { + $feature = Get-ChocolateyFeature -Name $this.Name + $currentState.Ensure = if ($feature.enabled -eq 'true') + { + 'Present' + } + else + { + 'Absent' + } + } + catch + { + Write-Verbose -Message ('Exception Caught:' -f $_) + $feature = $null + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateySource:ChocolateySource:ChocolateyError' + phrase = ('Error: {0}.' -f $_) + } + } + + if ($null -eq $feature) + { + $currentState.Reasons += @{ + code = 'ChocolateyFeature:ChocolateyFeature:FeatureNotFound' + phrase = ('The feature ''{0}'' was not found.' -f $this.Name) + } + } + elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Present') + { + $currentState.Reasons += @{ + code = 'ChocolateyFeature:ChocolateyFeature:FeaturePresentCompliant' + phrase = ('The feature ''{0}'' is enabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly) + } + } + elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Absent') + { + $currentState.Reasons += @{ + code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeDisabled' + phrase = ('The feature ''{0}'' is enabled while it''s expected to be disabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly) + } + } + elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Absent') + { + $currentState.Reasons += @{ + code = 'ChocolateyFeature:ChocolateyFeature:FeatureAbsentCompliant' + phrase = ('The feature ''{0}'' is disabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly) + } + } + elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Present') + { + $currentState.Reasons += @{ + code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeEnabled' + phrase = ('The feature ''{0}'' is disabled but we expected it enabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly) + } + } + + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + $currentState.Reasons.code.Where{ + $_ -notmatch 'Compliant$' + } + + if ($currentState.count -eq 0) + { + return $true + } + else + { + return $false + } + } + + [void] Set() + { + $currentState = $this.Get() + + switch -Regex ($currentState.Reasons.code) + { + 'FeatureShouldBeDisabled$' + { + # Disable the feature + Disable-ChocolateyFeature -Name $this.Name -Confirm:$false + } + + 'FeatureShouldBeEnabled$' + { + # Enable the feature + Enable-ChocolateyFeature -Name $this.Name -Confirm:$false + } + } + } +} diff --git a/source/Classes/006.ChocolateySetting.ps1 b/source/Classes/006.ChocolateySetting.ps1 new file mode 100644 index 0000000..43cd164 --- /dev/null +++ b/source/Classes/006.ChocolateySetting.ps1 @@ -0,0 +1,171 @@ + +<# + .SYNOPSIS + The `ChocolateySetting` DSC resource is used to set or unset Settings. + + .DESCRIPTION + Chocolatey lets you set or unset general Settings. + This resources lets you set or unset a Setting. + + .PARAMETER Ensure + Indicate whether the Chocolatey Setting should be enabled or disabled on the system. + + .PARAMETER Name + Name - the name of the Setting. + + .PARAMETER Value + Value - the value of the Setting, if ensure is set to present. + When Ensure is absent, the setting's value is cleared. + + .EXAMPLE + Invoke-DscResource -ModuleName Chocolatey -Name ChocolateySetting -Method Get -Property @{ + Ensure = 'Present' + Name = 'webRequestTimeoutSeconds' + Value = '30' + } + + # This example shows how to set the Setting webRequestTimeoutSeconds using Invoke-DscResource. +#> +[DscResource()] +class ChocolateySetting +{ + [DscProperty()] + [Ensure] $Ensure = 'Present' + + [DscProperty(Key)] + [string] $Name + + [DscProperty()] + [string] $Value + + [DscProperty(NotConfigurable)] + [ChocolateyReason[]] $Reasons + + [ChocolateySetting] Get() + { + $currentState = [ChocolateySetting]::new() + $currentState.Name = $this.Name + + try + { + $setting = Get-ChocolateySetting -Name $this.Name + $currentState.Value = $setting.Value + if (-not [string]::IsNullOrEmpty($setting.Value)) + { + $currentState.Ensure = 'Present' + } + else + { + $currentState.Ensure = 'Absent' + } + } + catch + { + Write-Verbose -Message ('Exception Caught:' -f $_) + $Setting = $null + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:ChocolateyError' + phrase = ('Error: {0}.' -f $_) + } + } + + if ($null -eq $Setting) + { + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingNotFound' + phrase = ('The Setting ''{0}'' was not found.' -f $this.Name) + } + } + else + { + if ($this.Ensure -eq 'Present' -and $currentState.Value -eq $this.Value) + { + $currentState.Ensure = 'Present' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingCompliant' + phrase = ('The Setting ''{0}'' is enabled with value ''{1}'' as expected.' -f $this.Name, $setting.Value) + } + } + elseif (-not [string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent') + { + $currentState.Ensure = 'Present' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeUnset' + phrase = ('The Setting ''{0}'' is Present while it''s expected to be unset (Absent). Currently set to ''{1}''.' -f $this.Name, $Setting.Value) + } + } + elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent') + { + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingUnsetCompliant' + phrase = ('The Setting ''{0}'' is unset as expected.' -f $this.Name) + } + } + elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Present') + { + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeSet' + phrase = ('The Setting ''{0}'' should be set.' -f $this.Name) + } + } + elseif ([string]::isNullOrEmpty($Setting.value) -and [string]::isNullOrEmpty($this.value) -and $this.Ensure -eq 'Present') + { + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingIncorrectlySet' + phrase = ('The Setting ''{0}'' should be set.' -f $this.Name) + } + } + elseif ($this.Ensure -eq 'Present' -and $setting.Value -ne $this.Value) + { + $currentState.Ensure = 'Present' + $currentState.Reasons += @{ + code = 'ChocolateySetting:ChocolateySetting:SettingNotCorrect' + phrase = ('The Setting ''{0}'' should be set to ''{1}'' but is ''{2}''.' -f $this.Name, $this.Value, $setting.Value) + } + } + } + + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + $currentState.Reasons.code.Where{ + $_ -notmatch 'Compliant$' + } + + if ($currentState.count -eq 0) + { + return $true + } + else + { + return $false + } + } + + [void] Set() + { + $currentState = $this.Get() + + switch ($currentState.Reasons.code) + { + 'SettingShouldBeUnset' + { + # Unset the Setting + Set-ChocolateySetting -Name $this.Name -Unset -Confirm:$false + } + + 'SettingShouldBeSet$|SettingNotCorrect$' + { + # Configure the Setting + Set-ChocolateySetting -Name $this.Name -Value $this.Value -Confirm:$false + } + } + } +} diff --git a/source/Classes/007.ChocolateyPin.ps1 b/source/Classes/007.ChocolateyPin.ps1 new file mode 100644 index 0000000..7ac9b40 --- /dev/null +++ b/source/Classes/007.ChocolateyPin.ps1 @@ -0,0 +1,165 @@ + +<# + .SYNOPSIS + The `ChocolateyPin` DSC resource is used to set or remove Pins. + + .DESCRIPTION + Chocolatey lets you pin package versions so they don't get updated. + This resources lets you set or remove a Pin. + + .PARAMETER Ensure + Indicate whether the Chocolatey Pin should be enabled or disabled for this package. + + .PARAMETER Version + Version of the Package to pin. + + .EXAMPLE + Invoke-DscResource -ModuleName Chocolatey -Name ChocolateyPin -Method Get -Property @{ + Ensure = 'Present' + Name = 'putty.portable' + Version = '0.78' + } + + # This example shows how to set the Pin webRequestTimeoutSeconds using Invoke-DscResource. +#> +[DscResource()] +class ChocolateyPin +{ + [DscProperty()] + [Ensure] $Ensure = 'Present' + + [DscProperty(Key)] + [string] $Name + + [DscProperty()] + [string] $Version + + [DscProperty(NotConfigurable)] + [ChocolateyReason[]] $Reasons + + [ChocolateyPin] Get() + { + $currentState = [ChocolateyPin]::new() + $currentState.Name = $this.Name + + try + { + $pin = Get-ChocolateyPin -Name $this.Name + $currentState.Version = $pin.Version + if (-not [string]::IsNullOrEmpty($pin.Name)) + { + $currentState.Ensure = 'Present' + } + else + { + $currentState.Ensure = 'Absent' + } + } + catch + { + Write-Verbose -Message ('Exception Caught:' -f $_) + $pin = $null + $currentState.Ensure = 'Absent' + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:ChocolateyError' + phrase = ('Error: {0}.' -f $_) + } + } + + if ($null -eq $pin) + { + Write-Debug -Message ('No pin found for package {0}' -f $this.Name) + + if ($this.Ensure -eq 'Absent') + { + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinAbsentCompliant' + phrase = ('The package ''{0}'' is not pinned, as expected.' -f $this.Name) + } + } + else # expected Present + { + # Sould be set + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinShouldBeSet' + phrase = ('The package ''{0}'' wasn''t pinned but should be set. (To version ''{1}'').' -f $this.Name, $this.Version) + } + } + } + elseif ($this.Ensure -eq 'Absent') + { + Write-Debug -Message 'Present but expected Absent. Should be removed.' + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinShouldBeRemoved' + phrase = ('The package ''{0}'' is pinned to ''{1}'' but it souldn''t.' -f $this.Name, $pin.Version) + } + } + elseif ($this.Ensure -eq 'Present' -and $pin.Version -ne $this.Version) + { + Write-Debug -Message ('The package is pinned, that''s expected, but version differs (Expected: {0}, Current: {1}).' -f $currentState.Version, $this.Version) + if ([string]::IsNullOrEmpty($this.Version)) # Assumes $this.Version is NOT set (Package must be pinned, but to ANY version) + { + # Pin version not expected, so $pin.Version is whatever was pinned at the time + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinCompliant' + phrase = ('The package ''{0}'' is pinned to version ''{1}''. No desired version specified for the pin.' -f $this.Name, $pin.Version) + } + } + else + { + # Pin version mismatch + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinVersionMismatch' + phrase = ('The package ''{0}'' should be pinned to ''{1}'' but is set to ''{2}''.' -f $this.Name, $this.Version, $pin.Version) + } + } + } + elseif ($this.Ensure -eq 'Present' -and $pin.Version -eq $this.Version) + { + Write-Debug -Message 'The Pin is set to the desired version (not null)' + $currentState.Reasons += @{ + code = 'ChocolateyPin:ChocolateyPin:PinCompliant' + phrase = ('The Pin ''{0}'' is enabled with value ''{1}'' as expected.' -f $this.Name, $pin.Version) + } + } + + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + $currentState.Reasons.code.Where{ + $_ -notmatch 'Compliant$' + } + + if ($currentState.count -eq 0) + { + return $true + } + else + { + return $false + } + } + + [void] Set() + { + $currentState = $this.Get() + + switch -Regex ($currentState.Reasons.code) + { + 'PinShouldBeRemoved' + { + Write-Verbose -Message ('Removing the pin on package ''{0}''.' -f $this.Name) + Remove-ChocolateyPin -Name $this.Name -Confirm:$false + } + + 'PinVersionMismatch$|PinShouldBeSet$' + { + Write-Verbose -Message ('Setting pin on package ''{0}'' asking for version ''{0}''.' -f $this.Name, $this.Version) + Add-ChocolateyPin -Name $this.Name -Version $this.Version -Confirm:$false + } + } + } +} diff --git a/source/DscResources/ChocolateyFeature/ChocolateyFeature.psm1 b/source/DscResources/ChocolateyFeature/ChocolateyFeature.psm1 deleted file mode 100644 index 2633d8b..0000000 --- a/source/DscResources/ChocolateyFeature/ChocolateyFeature.psm1 +++ /dev/null @@ -1,89 +0,0 @@ -function Get-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name - ) - - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - - $FeatureConfig = Get-ChocolateyFeature -Name $Name - - return @{ - Ensure = @('Absent', 'Present')[[int][bool]$FeatureConfig.enabled] - Name = $FeatureConfig.Name - } -} - -function Set-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name - ) - - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - - switch ($Ensure) - { - 'Present' - { - Enable-ChocolateyFeature -Name $Name - } - 'Absent' - { - Disable-ChocolateyFeature -Name $Name - } - } -} - -function Test-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Boolean])] - param ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name - ) - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - - $EnsureResultMap = @{ - 'Present' = $false - 'Absent' = $true - } - return (Test-ChocolateyFeature -Name $Name -Disabled:($EnsureResultMap[$Ensure])) -} - -Export-ModuleMember -Function *-TargetResource diff --git a/source/DscResources/ChocolateyFeature/ChocolateyFeature.schema.mof b/source/DscResources/ChocolateyFeature/ChocolateyFeature.schema.mof deleted file mode 100644 index 8bc2514..0000000 --- a/source/DscResources/ChocolateyFeature/ChocolateyFeature.schema.mof +++ /dev/null @@ -1,10 +0,0 @@ - -[ClassVersion("1.0.0.0"), FriendlyName("ChocolateyFeature")] -class ChocolateyFeature : OMI_BaseResource -{ - [Required, Description("Specifies whether the Chocolatey Feature should be enabled or not."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; - [Key] String Name; - [Read] boolean enabled; - [Read] String SetExplicitly; - [Read] String Description; -}; diff --git a/source/DscResources/ChocolateyFeature/en-US/ChocolateyFeature.strings.psd1 b/source/DscResources/ChocolateyFeature/en-US/ChocolateyFeature.strings.psd1 deleted file mode 100644 index 621eb56..0000000 --- a/source/DscResources/ChocolateyFeature/en-US/ChocolateyFeature.strings.psd1 +++ /dev/null @@ -1 +0,0 @@ -# I am not an empty file diff --git a/source/DscResources/ChocolateyPin/ChocolateyPin.psm1 b/source/DscResources/ChocolateyPin/ChocolateyPin.psm1 deleted file mode 100644 index e15006d..0000000 --- a/source/DscResources/ChocolateyPin/ChocolateyPin.psm1 +++ /dev/null @@ -1,119 +0,0 @@ -function Get-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name, - - [Parameter()] - [System.String] - $Version - ) - - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - $null = $PSBoundParameters.remove('Ensure') - - $returnValue = @{ - Ensure = $Ensure - Name = $Name - Version = $Version - } - - $returnValue -} - - -function Set-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name, - - [Parameter()] - [System.String] - $Version - ) - - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - $null = $PSBoundParameters.remove('Ensure') - - switch ($Ensure) - { - 'Absent' - { - if ( $PSBoundParameters.ContainsKey('Version') ) - { - $PSBoundParameters.remove('Version') - } - Write-Verbose "Remove Pin for the Chocolatey Package $Name." - Remove-ChocolateyPin @PSBoundParameters - } - 'Present' - { - Write-Verbose "Add Pin to the Chocolatey Package $Name." - Add-ChocolateyPin @PSBoundParameters - } - } -} - - -function Test-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name, - - [Parameter()] - [System.String] - $Version - ) - - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - $null = $PSBoundParameters.remove('Ensure') - - switch ($Ensure) - { - 'Present' - { - $PSBoundParameters['Version'] = $Version - } - 'Absent' - { - $PSBoundParameters['Version'] = '' - } - } - return (Test-ChocolateyPin @PSBoundParameters) -} - -Export-ModuleMember -Function *-TargetResource diff --git a/source/DscResources/ChocolateyPin/ChocolateyPin.schema.mof b/source/DscResources/ChocolateyPin/ChocolateyPin.schema.mof deleted file mode 100644 index 300999a..0000000 --- a/source/DscResources/ChocolateyPin/ChocolateyPin.schema.mof +++ /dev/null @@ -1,8 +0,0 @@ - -[ClassVersion("1.0.0.0"), FriendlyName("ChocolateyPin")] -class ChocolateyPin : OMI_BaseResource -{ - [Required, Description("Specifies whether the Chocolatey Package should be added or removed."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; - [Key, Description("Name (or setting) of the Chocolatey Package to modify")] String Name; - [Write, Description("Chocolatey Package Version")] String Version; -}; diff --git a/source/DscResources/ChocolateyPin/en-US/ChocolateyPin.strings.psd1 b/source/DscResources/ChocolateyPin/en-US/ChocolateyPin.strings.psd1 deleted file mode 100644 index 621eb56..0000000 --- a/source/DscResources/ChocolateyPin/en-US/ChocolateyPin.strings.psd1 +++ /dev/null @@ -1 +0,0 @@ -# I am not an empty file diff --git a/source/DscResources/ChocolateySetting/ChocolateySetting.psm1 b/source/DscResources/ChocolateySetting/ChocolateySetting.psm1 deleted file mode 100644 index e8cb060..0000000 --- a/source/DscResources/ChocolateySetting/ChocolateySetting.psm1 +++ /dev/null @@ -1,114 +0,0 @@ -function Get-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name - ) - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - - $null = $PSBoundParameters.remove('Ensure') - $Setting = Get-ChocolateySetting @PSBoundParameters - - $returnValue = @{ - Ensure = $Ensure - Name = $Name - value = $Setting.Value - } - - $returnValue -} - - -function Set-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name, - - [Parameter()] - [System.String] - $value - ) - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -verbose:$False - - $null = $PSBoundParameters.remove('Ensure') - - switch ($Ensure) - { - #'Present' { } - 'Absent' - { - if ( $PSBoundParameters.ContainsKey('Value') ) - { - $PSBoundParameters.remove('Value') - } - $null = $PSBoundParameters.add('Unset', $true) - } - } - Write-Verbose "Setting the Chocolatey Setting $Name." - Set-ChocolateySetting @PSBoundParameters -} - - -function Test-TargetResource -{ - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] - [CmdletBinding()] - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet("Present", "Absent")] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $Name, - - [Parameter()] - [System.String] - $value - ) - $Env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') - Import-Module $PSScriptRoot\..\..\Chocolatey.psd1 -Verbose:$False - - $null = $PSBoundParameters.remove('Ensure') - - switch ($Ensure) - { - 'Present' - { - $PSBoundParameters['value'] = $Value - } - 'Absent' - { - $PSBoundParameters['value'] = '' - } - } - - return (Test-ChocolateySetting @PSBoundParameters) -} - -Export-ModuleMember -Function *-TargetResource diff --git a/source/DscResources/ChocolateySetting/ChocolateySetting.schema.mof b/source/DscResources/ChocolateySetting/ChocolateySetting.schema.mof deleted file mode 100644 index 8229345..0000000 --- a/source/DscResources/ChocolateySetting/ChocolateySetting.schema.mof +++ /dev/null @@ -1,8 +0,0 @@ - -[ClassVersion("1.0.0.0"), FriendlyName("ChocolateySetting")] -class ChocolateySetting : OMI_BaseResource -{ - [Required, Description("Specifies whether the Chocolatey Setting should be set or unset."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; - [Key, Description("Name (or setting) of the Chocolatey setting to modify")] String Name; - [Write, Description("Value to be given on the setting. This is ignored when the ensure -eq 'Absent'.")] string value; -}; diff --git a/source/DscResources/ChocolateySetting/en-US/ChocolateySetting.strings.psd1 b/source/DscResources/ChocolateySetting/en-US/ChocolateySetting.strings.psd1 deleted file mode 100644 index 621eb56..0000000 --- a/source/DscResources/ChocolateySetting/en-US/ChocolateySetting.strings.psd1 +++ /dev/null @@ -1 +0,0 @@ -# I am not an empty file diff --git a/source/en-US/about_Chocolatey.help.txt b/source/en-US/about_Chocolatey.help.txt index 0102ddd..2ae0c44 100644 --- a/source/en-US/about_Chocolatey.help.txt +++ b/source/en-US/about_Chocolatey.help.txt @@ -2,12 +2,13 @@ TOPIC about_Chocolatey SHORT DESCRIPTION - DSC resources for deployment and configuration of Chocolatey + PowerShell wrapper of choco.exe and DSC resources for deployment and configuration of Chocolatey. LONG DESCRIPTION This module contains DSC resources for deployment and configuration of Chocolatey EXAMPLES + PS C:\> Get-Command -Module Chocolatey PS C:\> Get-DscResource -Module Chocolatey NOTE: @@ -15,7 +16,7 @@ NOTE: TROUBLESHOOTING NOTE: Go to the Github repository for read about issues, submit a new issue, and read - about new releases. https://github.com/chocolatey-community/Chocolatey/ + about new releases. https://github.com/chocolatey-community/Chocolatey/releases SEE ALSO - https://github.com/chocolatey-community/Chocolatey/ diff --git a/source/examples/Resources/ChocolateyPackage/1-ChocolateyPackage_InstallPackage.ps1 b/source/examples/Resources/ChocolateyPackage/1-ChocolateyPackage_InstallPackage.ps1 index a77e14d..891f6ce 100644 --- a/source/examples/Resources/ChocolateyPackage/1-ChocolateyPackage_InstallPackage.ps1 +++ b/source/examples/Resources/ChocolateyPackage/1-ChocolateyPackage_InstallPackage.ps1 @@ -51,7 +51,9 @@ configuration Example Ensure = 'Present' Name = 'Putty' Version = 'Latest' - ChocolateyOptions = @(@{ source = 'https://chocolatey.org/api/v2/' }) + ChocolateyOptions = @{ + source = 'https://chocolatey.org/api/v2/' + } } } } diff --git a/source/examples/Resources/ChocolateyPackage/3-ChocolateyPackage_InstallPackageWithouttVersion.ps1 b/source/examples/Resources/ChocolateyPackage/3-ChocolateyPackage_InstallPackageWithouttVersion.ps1 index 2703b8b..36ee943 100644 --- a/source/examples/Resources/ChocolateyPackage/3-ChocolateyPackage_InstallPackageWithouttVersion.ps1 +++ b/source/examples/Resources/ChocolateyPackage/3-ChocolateyPackage_InstallPackageWithouttVersion.ps1 @@ -50,7 +50,9 @@ configuration Example ChocolateyPackage Putty { Ensure = 'Present' Name = 'Putty' - ChocolateyOptions = @(@{ source = 'https://chocolatey.org/api/v2/' }) + ChocolateyOptions = @{ + source = 'https://chocolatey.org/api/v2/' + } } } } diff --git a/source/private/Get-Downloader.ps1 b/source/private/Get-Downloader.ps1 index f32faf3..b868a6d 100644 --- a/source/private/Get-Downloader.ps1 +++ b/source/private/Get-Downloader.ps1 @@ -26,7 +26,8 @@ function Get-Downloader { [CmdletBinding()] [OutputType([System.Net.WebClient])] - param ( + param + ( [Parameter(Mandatory = $true)] [System.String] $url, @@ -48,12 +49,12 @@ function Get-Downloader $downloader = [System.Net.WebClient]::new() $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials - if ($defaultCreds -ne $null) + if ($null -ne $defaultCreds) { $downloader.Credentials = $defaultCreds } - if ($ignoreProxy -ne $null -and $ignoreProxy -eq 'true') + if ($null -ne $ignoreProxy -and $ignoreProxy -eq 'true') { Write-Debug "Explicitly bypassing proxy" $downloader.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy() @@ -72,11 +73,11 @@ function Get-Downloader Write-Debug "Using explicit proxy server '$ProxyLocation'." $downloader.Proxy = $proxy } - elseif (!$downloader.Proxy.IsBypassed($url)) + elseif (-not $downloader.Proxy.IsBypassed($url)) { # system proxy (pass through) $creds = $defaultCreds - if ($creds -eq $null) + if ($null -eq $creds) { Write-Debug "Default credentials were null. Attempting backup method" throw "Could not download required file from $url" diff --git a/source/public/Add-ChocolateyPin.ps1 b/source/public/Add-ChocolateyPin.ps1 index 3982d7b..8ad8b60 100644 --- a/source/public/Add-ChocolateyPin.ps1 +++ b/source/public/Add-ChocolateyPin.ps1 @@ -14,6 +14,10 @@ This allows to pin a specific Version of a Chocolatey Package. The Package with the Version to pin must be installed beforehand. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. + .EXAMPLE Add-ChocolateyPin -Name 'PackageName' @@ -25,46 +29,46 @@ #> function Add-ChocolateyPin { - [CmdletBinding( - SupportsShouldProcess = $true, - ConfirmImpact = 'High' - )] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Package')] [System.String] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] + [Parameter(ValueFromPipelineByPropertyName = $true)] [System.String] - $Version + $Version, + [Parameter(DontShow)] + [switch] + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) - process + begin { if (-not ($chocoCmd = @(Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')[0])) { throw "Chocolatey Software not found." } + } - if (!(Get-ChocolateyPackage -Name $Name)) + process + { + if (-not (Get-ChocolateyPackage -Name $Name -Exact)) { - throw "Chocolatey Package $Name cannot be found." + throw ('Chocolatey Package ''{0}'' cannot be found.' -f $Name) } - $ChocoArguments = @('pin', 'add', '-r') + $ChocoArguments = @('pin', 'add') $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - # Write-Debug "choco $($ChocoArguments -join ' ')" + Write-Verbose -Message ('choco {0}' -f ($ChocoArguments -join ' ')) if ($PSCmdlet.ShouldProcess("$Name $Version", "Add Pin")) { - $Output = &$chocoCmd $ChocoArguments + $output = &$chocoCmd $ChocoArguments # LASTEXITCODE is always 0 unless point an existing version (0 when remove but already removed) if ($LASTEXITCODE -ne 0) @@ -73,7 +77,9 @@ function Add-ChocolateyPin } else { - $output | Write-Verbose + $output | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } } } } diff --git a/source/public/Disable-ChocolateyFeature.ps1 b/source/public/Disable-ChocolateyFeature.ps1 index 42fbd5d..ff459f6 100644 --- a/source/public/Disable-ChocolateyFeature.ps1 +++ b/source/public/Disable-ChocolateyFeature.ps1 @@ -9,8 +9,9 @@ .PARAMETER Name Name of the Chocolatey Feature to disable. Some are only available in the Chocolatey for business version. -.PARAMETER NoProgress - This allows to reduce the output created by the Chocolatey Command. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. .EXAMPLE Disable-ChocolateyFeature -Name 'Bob' @@ -20,21 +21,19 @@ #> function Disable-ChocolateyFeature { - [CmdletBinding()] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Feature')] [System.String] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] - $NoProgress + [Parameter(DontShow)] + [switch] + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) process @@ -44,15 +43,20 @@ function Disable-ChocolateyFeature throw "Chocolatey Software not found." } - if (!(Get-ChocolateyFeature -Name $Name)) + if (-not (Get-ChocolateyFeature -Name $Name)) { throw "Chocolatey Feature $Name cannot be found." } $ChocoArguments = @('feature', 'disable') $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - Write-Verbose "choco $($ChocoArguments -join ' ')" + Write-Verbose -Message ('choco {0}' -f ($ChocoArguments -join ' ')) - &$chocoCmd $ChocoArguments | Write-Verbose + if ($PSCmdlet.ShouldProcess($Env:COMPUTERNAME, "$chocoCmd $($ChocoArguments -join ' ')")) + { + &$chocoCmd $ChocoArguments | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } + } } } diff --git a/source/public/Disable-ChocolateySource.ps1 b/source/public/Disable-ChocolateySource.ps1 index 4bed613..1fac610 100644 --- a/source/public/Disable-ChocolateySource.ps1 +++ b/source/public/Disable-ChocolateySource.ps1 @@ -10,8 +10,9 @@ .PARAMETER Name Name of the Chocolatey source to Disable -.PARAMETER NoProgress - This allows to reduce the output created by the Chocolatey Command. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. .EXAMPLE Disable-ChocolateySource -Name chocolatey @@ -21,20 +22,18 @@ #> function Disable-ChocolateySource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] [CmdletBinding()] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [System.String] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] - $NoProgress + [Parameter(DontShow)] + [switch] + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) process @@ -44,15 +43,17 @@ function Disable-ChocolateySource throw "Chocolatey Software not found." } - if (!(Get-ChocolateySource -Name $Name)) + if (-not (Get-ChocolateySource -Name $Name)) { throw "Chocolatey Source $Name cannot be found. You can Register it using Register-ChocolateySource." } $ChocoArguments = @('source', 'disable') $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - Write-Verbose "choco $($ChocoArguments -join ' ')" + Write-Verbose -Message ('choco {0}' -f ($ChocoArguments -join ' ')) - &$chocoCmd $ChocoArguments | Write-Verbose + &$chocoCmd $ChocoArguments | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } } } diff --git a/source/public/Enable-ChocolateyFeature.ps1 b/source/public/Enable-ChocolateyFeature.ps1 index 86d8cf6..9d11ca5 100644 --- a/source/public/Enable-ChocolateyFeature.ps1 +++ b/source/public/Enable-ChocolateyFeature.ps1 @@ -9,8 +9,9 @@ .PARAMETER Name Name of the Chocolatey Feature to disable -.PARAMETER NoProgress - This allows to reduce the output created by the Chocolatey Command. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. .EXAMPLE Enable-ChocolateyFeature -Name 'MyChocoFeatureName' @@ -20,21 +21,19 @@ #> function Enable-ChocolateyFeature { - [CmdletBinding()] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Feature')] [System.String] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] - $NoProgress + [Parameter(DontShow)] + [switch] + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) process @@ -49,10 +48,16 @@ function Enable-ChocolateyFeature throw "Chocolatey Feature $Name cannot be found." } - $ChocoArguments = @('feature', 'enable') - $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - Write-Verbose "choco $($ChocoArguments -join ' ')" + $chocoArguments = @('feature', 'enable') - &$chocoCmd $ChocoArguments | Write-Verbose + $chocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters + Write-Verbose -Message ('choco {0}' -f ($chocoArguments -join ' ')) + + if ($PSCmdlet.ShouldProcess($Env:COMPUTERNAME, "$chocoCmd $($chocoArguments -join ' ')")) + { + &$chocoCmd $chocoArguments | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } + } } } diff --git a/source/public/Enable-ChocolateySource.ps1 b/source/public/Enable-ChocolateySource.ps1 index f4aeee3..280fa8e 100644 --- a/source/public/Enable-ChocolateySource.ps1 +++ b/source/public/Enable-ChocolateySource.ps1 @@ -10,8 +10,9 @@ .PARAMETER Name Name of the Chocolatey source to Disable -.PARAMETER NoProgress - This allows to reduce the output created by the Chocolatey Command. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. .EXAMPLE Enable-ChocolateySource -Name 'chocolatey' @@ -21,21 +22,18 @@ #> function Enable-ChocolateySource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] [CmdletBinding()] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [System.String] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] + [Parameter(DontShow)] [switch] - $NoProgress - + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) process @@ -45,7 +43,7 @@ function Enable-ChocolateySource throw "Chocolatey Software not found." } - if (!(Get-ChocolateySource -id $Name)) + if (-not (Get-ChocolateySource -id $Name)) { throw "Chocolatey Source $Name cannot be found. You can Register it using Register-ChocolateySource." } @@ -54,6 +52,8 @@ function Enable-ChocolateySource $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters Write-Verbose "choco $($ChocoArguments -join ' ')" - &$chocoCmd $ChocoArguments | Write-Verbose + &$chocoCmd $ChocoArguments | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } } } diff --git a/source/public/Get-ChocolateyPin.ps1 b/source/public/Get-ChocolateyPin.ps1 index f42b8f7..0bc30c0 100644 --- a/source/public/Get-ChocolateyPin.ps1 +++ b/source/public/Get-ChocolateyPin.ps1 @@ -21,7 +21,8 @@ function Get-ChocolateyPin { [CmdletBinding()] - param ( + param + ( [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [System.String] $Name = '*' @@ -34,7 +35,7 @@ function Get-ChocolateyPin throw "Chocolatey Software not found." } - if (!(Get-ChocolateyPackage -Name $Name)) + if ($Name -ne '*' -and -not (Get-ChocolateyPackage -Name $Name)) { throw "Chocolatey Package $Name cannot be found." } @@ -42,7 +43,7 @@ function Get-ChocolateyPin # Prepare the arguments for `choco pin list -r` $ChocoArguments = @('pin', 'list', '-r') - # Write-Debug -Message "choco $($ChocoArguments -join ' ')" + Write-Verbose -Message "choco $($ChocoArguments -join ' ')" # Stop here if the list is empty if (-Not ($ChocoPinListOutput = &$chocoCmd $ChocoArguments)) @@ -58,12 +59,12 @@ function Get-ChocolateyPin if ($Name -ne '*') { - Write-Verbose 'Filtering pinned Packages' + Write-Verbose -Message 'Filtering pinned Packages' $ChocoPinListOutput = $ChocoPinListOutput | Where-Object { $_.Name -in $Name } } else { - Write-Verbose 'Returning all pinned Packages' + Write-Verbose -Message 'Returning all pinned Packages' } foreach ($Pin in $ChocoPinListOutput) diff --git a/source/public/Get-ChocolateySetting.ps1 b/source/public/Get-ChocolateySetting.ps1 index 68b0a84..45f865b 100644 --- a/source/public/Get-ChocolateySetting.ps1 +++ b/source/public/Get-ChocolateySetting.ps1 @@ -20,16 +20,15 @@ function Get-ChocolateySetting { [CmdletBinding()] - param ( - [Parameter( - ValueFromPipeline - , ValueFromPipelineByPropertyName - )] + param + ( + [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Name')] [ValidateNotNullOrEmpty()] [string[]] $Setting = '*' ) + begin { if (-not ($chocoCmd = @(Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')[0])) @@ -37,14 +36,14 @@ function Get-ChocolateySetting throw "Chocolatey Software not found." } - $ChocoConfigPath = join-path $chocoCmd.Path ..\..\config\chocolatey.config -Resolve - $ChocoXml = [xml]::new() - $ChocoXml.Load($ChocoConfigPath) + $ChocoConfigPath = Join-Path -Path $chocoCmd.Path -ChildPath '..\..\config\chocolatey.config' -Resolve + $chocoXml = [xml]::new() + $chocoXml.Load($ChocoConfigPath) } process { - if (!$ChocoXml) + if (-not $chocoXml) { throw "Error with Chocolatey config." } @@ -54,29 +53,26 @@ function Get-ChocolateySetting if ($Name -ne '*') { Write-Verbose ("Searching for Setting named {0}" -f [Security.SecurityElement]::Escape($Name)) - $SettingNodes = $ChocoXml.SelectNodes("//add[translate(@key,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$([Security.SecurityElement]::Escape($Name.ToLower()))']") + $SettingNodes = $chocoXml.SelectNodes("//add[translate(@key,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$([Security.SecurityElement]::Escape($Name.ToLower()))']") } else { Write-Verbose 'Returning all Sources configured.' - $SettingNodes = $ChocoXml.chocolatey.config.childNodes + $SettingNodes = $chocoXml.chocolatey.config.childNodes } foreach ($SettingNode in $SettingNodes) { - $SettingObject = [PSCustomObject]@{ + $SettingObject = [ordered]@{ PSTypeName = 'Chocolatey.Setting' } + foreach ($property in $SettingNode.Attributes.name) { - $SettingPropertyParam = @{ - MemberType = 'NoteProperty' - Name = $property - Value = $SettingNode.($property).ToString() - } - $SettingObject | Add-Member @SettingPropertyParam + $settingObject[$property] = '{0}' -f $SettingNode.($property) } - Write-Output $SettingObject + + [PSCustomObject]$SettingObject } } } diff --git a/source/public/Remove-ChocolateyPin.ps1 b/source/public/Remove-ChocolateyPin.ps1 index a19ac15..3f1a05a 100644 --- a/source/public/Remove-ChocolateyPin.ps1 +++ b/source/public/Remove-ChocolateyPin.ps1 @@ -9,6 +9,10 @@ .PARAMETER Name Name of the Chocolatey Package to remove the pin. +.PARAMETER RunNonElevated + Throws if the process is not running elevated. use -RunNonElevated if you really want to run + even if the current shell is not elevated. + .EXAMPLE Remove-ChocolateyPin -Name 'PackageName' @@ -17,18 +21,18 @@ #> function Remove-ChocolateyPin { - [CmdletBinding( - SupportsShouldProcess = $true, - ConfirmImpact = 'High' - )] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Package')] [System.String] - $Name + $Name, + + [Parameter(DontShow)] + [switch] + $RunNonElevated = $(Assert-ChocolateyIsElevated) ) process @@ -38,18 +42,18 @@ function Remove-ChocolateyPin throw "Chocolatey Software not found." } - if (!(Get-ChocolateyPackage -Name $Name)) + if (-not (Get-ChocolateyPackage -Name $Name)) { throw "The Pin for Chocolatey Package $Name cannot be found." } - $ChocoArguments = @('pin', 'remove', '-r') - $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - # Write-Debug "choco $($ChocoArguments -join ' ')" + $chocoArguments = @('pin', 'remove', '-r') + $chocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters + Write-Verbose -Message "choco $($chocoArguments -join ' ')" if ($PSCmdlet.ShouldProcess("$Name", "Remove Pin")) { - $Output = &$chocoCmd $ChocoArguments + $output = &$chocoCmd $chocoArguments # LASTEXITCODE is always 0 unless point an existing version (0 when remove but already removed) if ($LASTEXITCODE -ne 0) @@ -58,7 +62,9 @@ function Remove-ChocolateyPin } else { - $output | Write-Verbose + $output | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } } } } diff --git a/source/public/Set-ChocolateySetting.ps1 b/source/public/Set-ChocolateySetting.ps1 index 38f8730..c248881 100644 --- a/source/public/Set-ChocolateySetting.ps1 +++ b/source/public/Set-ChocolateySetting.ps1 @@ -28,33 +28,21 @@ function Set-ChocolateySetting { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] - [CmdletBinding( - SupportsShouldProcess - , ConfirmImpact = 'Low' - )] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] [OutputType([Void])] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Setting')] [System.String] $Name, - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - , ParameterSetName = 'Set' - )] + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Set')] [AllowEmptyString()] [System.String] $Value, - [Parameter( - ValueFromPipelineByPropertyName - , ParameterSetName = 'Unset' - )] + [Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Unset')] [switch] $Unset, @@ -86,6 +74,7 @@ function Set-ChocolateySetting { $null = $PSBoundParameters.Remove('Value') } + $null = $PSBoundParameters.remove('unset') $ChocoArguments += 'unset' } @@ -94,17 +83,15 @@ function Set-ChocolateySetting $PSBoundParameters['Value'] = $ExecutionContext.InvokeCommand.ExpandString($Value).TrimEnd(@('/', '\')) $ChocoArguments += 'set' } + $ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters - Write-Verbose "choco $($ChocoArguments -join ' ')" + Write-Verbose -Message ('choco {0}' -f ($ChocoArguments -join ' ')) if ($PSCmdlet.ShouldProcess($Env:COMPUTERNAME, "$chocoCmd $($ChocoArguments -join ' ')")) { - $cmdOut = &$chocoCmd $ChocoArguments - } - - if ($cmdOut) - { - Write-Verbose "$($cmdOut | Out-String)" + &$chocoCmd $ChocoArguments | ForEach-Object -Process { + Write-Verbose -Message ('{0}' -f $_) + } } } } diff --git a/source/public/Test-ChocolateyPin.ps1 b/source/public/Test-ChocolateyPin.ps1 index fd2bfee..85a94cc 100644 --- a/source/public/Test-ChocolateyPin.ps1 +++ b/source/public/Test-ChocolateyPin.ps1 @@ -20,43 +20,41 @@ #> function Test-ChocolateyPin { - [CmdletBinding( - DefaultParameterSetName = 'Set' - )] + [CmdletBinding()] [OutputType([Bool])] - param ( - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName)] - [System.String] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [string] $Name, - [Parameter()] - [System.String] + [Parameter(ValueFromPipelineByPropertyName = $true)] + [string] $Version ) process { - if (-not (Get-Command 'choco.exe' -CommandType Application -ErrorAction SilentlyContinue)) + if (-not (Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')) { - throw "Chocolatey Software not found." + throw 'Chocolatey Software not found.' } - if (!($Setting = Get-ChocolateyPin -Name $Name)) + if (-not ($pin = Get-ChocolateyPin -Name $Name)) { - Write-Verbose -Message "The Pin for the Chocolatey Package '$Name' cannot be found." + Write-Verbose -Message ('The Pin for the Chocolatey Package ''{0}'' cannot be found.' -f $Name) return $false } - $Version = $ExecutionContext.InvokeCommand.ExpandString($Version).TrimEnd(@('/', '\')) - if ([string]$Setting.Version -eq $Version) + if ([string]$pin.Version -eq $Version) { - Write-Verbose ("The Pin for the Chocolatey Package '{0}' is set to '{1}' as expected." -f $Name, $Version) + Write-Verbose -Message ('The Pin for the Chocolatey Package ''{0}'' is set to ''{1}'' as expected.' -f $Name, $pin.Version) return $true } else { - Write-Verbose ("The Pin for the Chocolatey Package '{0}' is NOT set to '{1}' as expected." -f $Name, $Setting.Version) - return $False + Write-Verbose ('The Pin for the Chocolatey Package ''{0}'' is NOT set to ''{1}'' but to ''{2}''.' -f $Name, $Version, $pin.Version) + return $false } } } diff --git a/source/public/Test-ChocolateySetting.ps1 b/source/public/Test-ChocolateySetting.ps1 index baeb311..8c98d9a 100644 --- a/source/public/Test-ChocolateySetting.ps1 +++ b/source/public/Test-ChocolateySetting.ps1 @@ -24,33 +24,22 @@ #> function Test-ChocolateySetting { - [CmdletBinding( - DefaultParameterSetName = 'Set' - )] + [CmdletBinding(DefaultParameterSetName = 'Set')] [OutputType([Bool])] - param ( - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - )] + param + ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Setting')] [System.String] $Name, - [Parameter( - Mandatory = $true - , ValueFromPipelineByPropertyName - , ParameterSetName = 'Set' - )] + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Set')] [AllowEmptyString()] [AllowNull()] [System.String] $Value, - [Parameter( - ValueFromPipelineByPropertyName - , ParameterSetName = 'Unset' - )] + [Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Unset')] [switch] $Unset ) @@ -62,27 +51,32 @@ function Test-ChocolateySetting throw "Chocolatey Software not found." } - if (!($Setting = Get-ChocolateySetting -Name $Name)) + if (-not ($setting = Get-ChocolateySetting -Name $Name)) { Write-Warning "Chocolatey Setting $Name cannot be found." return $false } - $Setting | Write-Verbose + + Write-Verbose -Message ('Setting ''{0}'' set to ''{1}''.' -f $setting.Name, $setting.Value) + if ($Unset) { - $Value = '' + $value = '' + } + else + { + $value = $ExecutionContext.InvokeCommand.ExpandString($value).TrimEnd(@('/', '\')) } - $Value = $ExecutionContext.InvokeCommand.ExpandString($Value).TrimEnd(@('/', '\')) if ([string]$Setting.value -eq $Value) { - Write-Verbose ("The Chocolatey Setting {0} is set to '{1}' as expected." -f $Name, $Value) + Write-Verbose -Message ("The Chocolatey Setting {0} is set to '{1}' as expected." -f $Name, $value) return $true } else { - Write-Verbose ("The Chocolatey Setting {0} is NOT set to '{1}' as expected:{2}" -f $Name, $Setting.value, $Value) - return $False + Write-Verbose -Message ("The Chocolatey Setting {0} is NOT set to '{1}' as expected:{2}" -f $Name, $Setting.value, $value) + return $false } } } diff --git a/source/public/Test-ChocolateySource.ps1 b/source/public/Test-ChocolateySource.ps1 index 258eeda..296298d 100644 --- a/source/public/Test-ChocolateySource.ps1 +++ b/source/public/Test-ChocolateySource.ps1 @@ -58,41 +58,30 @@ function Test-ChocolateySource param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] - [System.String] + [string] $Name, - [Parameter( - ValueFromPipelineByPropertyName - )] + [Parameter(ValueFromPipelineByPropertyName = $true)] + [string] $Source, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] + [Parameter(ValueFromPipelineByPropertyName = $true)] + [switch] $Disabled, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] + [Parameter(ValueFromPipelineByPropertyName = $true)] + [switch] $BypassProxy, - [Parameter( - ValueFromPipelineByPropertyName - )] - [Switch] + [Parameter(ValueFromPipelineByPropertyName = $true)] + [switch] $SelfService, - [Parameter( - ValueFromPipelineByPropertyName - )] + [Parameter(ValueFromPipelineByPropertyName = $true)] [int] $Priority = 0, - [Parameter( - ValueFromPipelineByPropertyName - )] + [Parameter(ValueFromPipelineByPropertyName = $true)] [PSCredential] $Credential, @@ -177,6 +166,7 @@ function Test-ChocolateySource } } } + Compare-Object -ReferenceObject $ReferenceSource -DifferenceObject $Source -Property $ReferenceSource.PSObject.Properties.Name } }