GlobalPSDefaultParamValue.psm1
<#
.SYNOPSIS Add and Remove PSDefaultParameterValues to Global Variable .DESCRIPTION Add and Remove PSDefaultParameterValues to Global Variable .NOTES Author : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.0.0 Date : 2022.04.24 Update : 1.0.0 SG - 2022.04.24 Initial Script .LINK http://www.FocusedIT.co.uk #> function Add-PSDefaultParamValue{ <# .SYNOPSIS Add PSDefaultParameterValues to Global Variable .DESCRIPTION Add PSDefaultParameterValues to Global Variable .EXAMPLE Add-PSDefaultParameterValue -Command "Get-Process" -Parameter "Name" -Value "powershell" .NOTES Author : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.0.0 Date : 2022.04.24 Update : 1.0.0 SG - 2022.04.24 Initial Script .LINK http://www.FocusedIT.co.uk #> [CmdletBinding(SupportsShouldProcess)] Param( [Alias("Name")] [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Command , [Alias("Param")] [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Parameter , #[Alias("","")] [Parameter(Position=2,Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Value , [Parameter(Position=3,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [switch]$SkipValidation #, ) begin{ $Output = @() } process{ if(!($SkipValidation)){ try{ if(!(Get-Command $Command -ErrorAction SilentlyContinue)){ throw "Command not found: $Command" } }catch{ Write-Error $Global:Error[0] -ErrorAction Stop } try{ if(!(((Get-Command $Command).parameters | Select-Object -ExpandProperty Keys) -match ("^"+$Parameter+"$"))){ throw "Parameter ($Parameter) not valid for command: $Command" } }catch{ Write-Error $Global:Error[0] -ErrorAction Stop } } Write-Verbose "Setting Default Parameter:" Write-Verbose "Command: $Command" Write-Verbose "Parameter: $Parameter" Write-Verbose "Value: $Value" #$global:PSDefaultParameterValues["$($Command):$($Parameter)"]="$Value" if($PSCmdlet.ShouldProcess("$($Command):$($Parameter)")){ $global:PSDefaultParameterValues.Add("$($Command):$($Parameter)","$($Value)") } $Obj = New-Object PSObject $Obj | Add-Member NoteProperty "Method" "Add" $Obj | Add-Member NoteProperty "Command" $Command $Obj | Add-Member NoteProperty "Parameter" $Parameter $Obj | Add-Member NoteProperty "Value" $Value $Output += $Obj } end{ $Output } } function Remove-PSDefaultParamValue{ <# .SYNOPSIS Remove PSDefaultParameterValues from Global Variable .DESCRIPTION Remove PSDefaultParameterValues from Global Variable .EXAMPLE Remove-PSDefaultParamValue -Command "Get-Process" -Parameter "Name" .NOTES Author : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.0.0 Date : 2022.04.24 Update : 1.0.0 SG - 2022.04.24 Initial Script .LINK http://www.FocusedIT.co.uk #> [CmdletBinding(SupportsShouldProcess)] Param( [Alias("Name")] [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Command , [Alias("Param")] [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Parameter #, ) begin{ $Output = @() } process{ try{ if(!($Value = $global:PSDefaultParameterValues["$($Command):$($Parameter)"])){ throw "Default Value not found: $($Command):$($Parameter)" } }catch{ Write-Error $Global:Error[0] -ErrorAction Stop } Write-Verbose "Removing Default Parameter:" Write-Verbose "Command: $Command" Write-Verbose "Parameter: $Parameter" Write-Verbose "Value: $Value" if($PSCmdlet.ShouldProcess("$($Command):$($Parameter)")){ $global:PSDefaultParameterValues.Remove("$($Command):$($Parameter)") } $Obj = New-Object PSObject $Obj | Add-Member NoteProperty "Method" "Remove" $Obj | Add-Member NoteProperty "Command" $Command $Obj | Add-Member NoteProperty "Parameter" $Parameter $Obj | Add-Member NoteProperty "Value" $Value $Output += $Obj } end{ $Output } } |