forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-ParameterDefault.ps1
62 lines (51 loc) · 2.14 KB
/
Remove-ParameterDefault.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#
.SYNOPSIS
Removes a value that would have been used for a parameter if none was specified, if one existed.
.INPUTS
An object with a ParameterName property that identifies a property to remove a default for.
.FUNCTIONALITY
Parameters
.LINK
Add-ScopeLevel.ps1
.LINK
Stop-ThrowError.ps1
.LINK
Get-Command
.LINK
about_Scopes
.EXAMPLE
Remove-ParameterDefault.ps1 epcsv nti -Scope Global
Establishes that the -NoTypeInformation param of the Export-Csv cmdlet will revert to false
(as established by the cmdlet) if not otherwise specified, globally for the PowerShell session.
.EXAMPLE
Remove-ParameterDefault.ps1 Select-Xml Namespace
Removes any namespaces used by Select-Xml when none are given explicitly.
#>
#Requires -Version 3
[CmdletBinding()] Param(
# The name of a cmdlet, function, script, or alias to remove a default parameter value from.
[Parameter(Position=0,Mandatory=$true)][ValidateNotNullOrEmpty()][Alias('CmdletName')][string] $CommandName,
# The name or alias of the parameter to remove a default value from.
[Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)][ValidateNotNullOrEmpty()][string] $ParameterName,
# The scope of this default.
[string] $Scope = 'Local'
)
Begin
{
$Scope = Add-ScopeLevel.ps1 $Scope
$cmd = Get-Command $CommandName -ErrorAction Ignore
if(!$cmd) {Stop-ThrowError.ps1 "Could not find command '$CommandName'" -Argument CommandName}
if($cmd.CommandType -eq 'Alias') {$cmd = Get-Command $cmd.ResolvedCommandName}
if($cmd.CommandType -notin 'Cmdlet','ExternalScript','Function','Script')
{Stop-ThrowError.ps1 "Command '$CommandName' ($($cmd.CommandType)) not supported" -Argument CommandName}
$defaults = Get-Variable PSDefaultParameterValues -Scope $Scope -ErrorAction Ignore
}
Process
{
if(!$defaults) {return}
$name =
try {"$($cmd.Name):$($cmd.ResolveParameter($ParameterName).Name)"}
catch {Stop-ThrowError.ps1 "Could not find parameter '$ParameterName' for cmdlet '$CommandName'" -Argument ParameterName}
Write-Verbose "Removing default parameter '$name'"
if($defaults.Value.ContainsKey($name)) {$defaults.Value.Remove($name)}
}