PSChocoConfig.psm1

function Clear-ChocoConfig {
    <#
        .SYNOPSIS
        Unsets the chosen configuration item
 
        .DESCRIPTION
        This command wraps 'choco config' to make setting configuration items easier. Dynamically generates names from configuration file.
 
        .PARAMETER Name
 
        The name of the configuration item to change
 
        .EXAMPLE
        Clear-ChocoConfig -Name proxy
 
        Sets the proxy configuration setting to a blank default value
    #>

    [cmdletBinding(SupportsShouldProcess,ConfirmImpact="High",HelpUri="https://github.com/steviecoaster/PSChocoConfig/wiki/Clear-ChocoConfig")]
    Param (
        [Parameter(Mandatory,Position=0)]
        [ArgumentCompleter({
            param($Command,$Parameter,$WordTocomplete,$CommandAst,$FakeBoundParams)
            $results = Get-ChocoConfig | Select -ExpandProperty key

            If($WordTocomplete){

                $results.Where{ $_ -match "^$WordTocomplete"}
                
            }

            Else {

                $results

            }
        })]
        [String]

        $Name

    )

    process {

        If($PSCmdlet.ShouldProcess("$Name","Removing value: $Value")){
        
            $choco = choco config unset --name="'$Name'"
            Write-Host "$($choco[-1])"-ForegroundColor Yellow
        
        }
    }

}
function Get-ChocoConfig {
    <#
        .SYNOPSIS
        Converts choco's configuration file into a powershell object
         
        .PARAMETER ChocolateyConfig
        The chocolatey config to load. Defaults to $Env:ChocolateyInstall\config\chocolatey.config
         
        .PARAMETER ConfigurationItem
        Return only specified Configuration Item(s).
         
        .EXAMPLE
        Get-ChocoConfig
         
        .EXAMPLE
        Get-ChocoConfig -ConfigurationItem proxy
         
    #>

    [cmdletBinding(HelpUri="https://github.com/steviecoaster/PSChocoConfig/wiki/Get-ChocoConfig")]
    Param(
        [Parameter(ValueFromPipeline,Position=0)]
        [string]
        [ValidateScript({Test-Path $_})]
        $ChocolateyConfig = "$env:ChocolateyInstall\config\chocolatey.config",

        [Parameter(Position=1)]
        [String[]]
        $ConfigurationItem
    
    )

    process {
        
        [xml]$Config = Get-Content $ChocolateyConfig
        
        if($ConfigurationItem){
            Foreach($c in $ConfigurationItem){

                $config.chocolatey.config.add | Where-Object { $_.Key -eq $c }
            
            }

        }

        else{

            $config.chocolatey.config.add

        }
    }

}
function Get-ChocoFeature {
    <#
        .SYNOPSIS
        Retrieve feature settings from chocolatey config file
 
        .PARAMETER ChocolateyConfig
        The config file to load. Defaults to $env:ChocolateyInstall\config\chocolatey.config
 
        .PARAMETER Feature
        The feature(s) you with to query for information
 
        .EXAMPLE
        Get-ChocoFeature
 
        Returns all features and their values
 
        .EXAMPLE
        Get-ChocoFeature -Feature useBackgroundService
 
        Retrieves current setting of useBackgroundService feature
    #>

    [cmdletBinding(HelpUri="https://github.com/steviecoaster/PSChocoConfig/wiki/Get-ChocoFeature")]
    Param(
        [Parameter(ValueFromPipeline,Position=0)]
        [String]
        [ValidateScript({Test-Path $_})]
        $ChocolateyConfig = "$env:ChocolateyInstall\config\chocolatey.config",

        [Parameter(Position=1)]
        [String[]]
        $Feature
    )

    process {

        [xml]$config = Get-Content $ChocolateyConfig
        
        if($Feature){

            Foreach($f in $Feature) {

                $config.chocolatey.features.feature | Where-Object { $_.Name -eq $f } 

            }

        }

        else{

            $config.chocolatey.features.feature

        }

    }

}
function Set-ChocoConfig {
    <#
        .SYNOPSIS
        Sets the specified configuration item to the provided value
 
        .DESCRIPTION
        Wraps 'choco config' to make setting configuration values easier. Dynamically generates name values from chocolatey config file.
 
        .PARAMETER Value
        The value to set of the Name of the configuration item.
 
        .PARAMETER Name
        Name of the configuration item. Dynamically generated from the configuration file for tab-completion.
 
        .EXAMPLE
 
        Set-ChocoConfig -Name proxy -Value 'https://awesome.proxy.local'
    #>

    [cmdletBinding(SupportsShouldProcess,ConfirmImpact="High",HelpUri="https://github.com/steviecoaster/PSChocoConfig/wiki/Set-ChocoConfig")]
    Param (

        [Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName,ValueFromPipeline)]
        [ArgumentCompleter({
            param($Command,$Parameter,$WordTocomplete,$CommandAst,$FakeBoundParams)
            $results = Get-ChocoConfig | Select -ExpandProperty key

            If($WordTocomplete){

                $results.Where{ $_ -match "^$WordTocomplete"}
                
            }

            Else {

                $results

            }
        })]
        [String]
        $Name,

        [Parameter(Mandatory,Position=1)]
        [AllowEmptyString()]
        [String]
        $Value
    )

    

    process {

        If($PSCmdlet.ShouldProcess("$Name","Setting value: $Value")){
        
            $choco = choco config set --name="'$Name'" --value="'$Value'"
            Write-Host "$($choco[-1])"-ForegroundColor Yellow
        
        }
    }

}
function Set-ChocoFeature {
     <#
        .SYNOPSIS
        Sets the specified feature to the provided state.
 
        .DESCRIPTION
        Wraps 'choco config' to make setting configuration values easier. Dynamically generates name values from chocolatey config file.
 
        .PARAMETER Value
        The value to set of the Name of the configuration item.
 
        .PARAMETER Name
        Name of the feature. Dynamically generated from the configuration file for tab-completion.
    #>

    [cmdletBinding(SupportsShouldProcess,ConfirmImpact="High",HelpUri="https://github.com/steviecoaster/PSChocoConfig/wiki/Set-ChocoFeature")]
    Param (
        
        [Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName,ValueFromPipeline)]
        [ArgumentCompleter({
            param($Command,$Parameter,$WordTocomplete,$CommandAst,$FakeBoundParams)
            $results = Get-Chocofeature | Select -ExpandProperty Name

            If($WordTocomplete){

                $results.Where{ $_ -match "^$WordTocomplete"}
                
            }

            Else {

                $results

            }
        })]
        [String]
        $Name,

        [Parameter(Mandatory,Position=1)]
        [String]
        [ValidateSet('Enabled','Disabled')]
        $State
    )

    process {

        Switch($State){

            'Enabled' { $command = "enable" }
            'Disabled' { $command = "disable" }

        }

        If($PSCmdlet.ShouldProcess("Ensuring feature $Name is set to $State")){
        
            $choco = choco feature $command --name="'$Name'"
            Write-Host "$($choco[-1])"-ForegroundColor Yellow
        
        }
    }

}