作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据about_Parameters_Default_Values文档,
The $PSDefaultParameterValues preference variable lets you specify custom default values for any cmdlet or advanced function.
但是,实际上,这似乎不适用于脚本高级功能(请参阅下面脚本中的示例部分)。
同时使用Script
和Local
范围,核心PowerShell Cmdlet的默认值(即Write-Host
)工作正常,但没有一个脚本本身的参数是从 $PSDefaultParameterValues
接收的,无论是否定义了默认值,或者是否是公共(public)参数。
<#
.Synopsis
Test the handling of $PSDefaultParameterValues.
.Parameter UnspecifiedParam
A parameter with no default.
.Parameter DefaultParam
A parameter with a default.
.Example
Test-DefaultParam.ps1
Name Value
---- -----
InitialPSBoundParameters {}
InitialUnspecifiedParam
InitialDefaultParam DefinedDefaultValue
Script:PSDefaultParameterValues:Write-Host
WARNING: UnspecifiedParam default was not received
WARNING: DefaultParam default was not received
WARNING: Verbose default was not received
PSDefaultParameterValues {Test-DefaultParam:Verbose, Test-DefaultParam:DefaultParam, Test-DefaultParam:UnspecifiedParam}
PSDPVUnspecifiedParam Script:PSDefaultParameterValues:UnspecifiedParam
PSDPVDefaultParam Script:PSDefaultParameterValues:DefaultParam
PSDPVVerbose True
PSDPVWriteHost Script:PSDefaultParameterValues:Write-Host
PSBoundParameters {}
UnspecifiedParam
DefaultParam DefinedDefaultValue
Local:PSDefaultParameterValues:Write-Host
WARNING: UnspecifiedParam default was not received
WARNING: DefaultParam default was not received
WARNING: Verbose default was not received
PSDefaultParameterValues {Test-DefaultParam:Verbose, Test-DefaultParam:DefaultParam, Test-DefaultParam:UnspecifiedParam}
PSDPVUnspecifiedParam Local:PSDefaultParameterValues:UnspecifiedParam
PSDPVDefaultParam Local:PSDefaultParameterValues:DefaultParam
PSDPVVerbose True
PSDPVWriteHost Local:PSDefaultParameterValues:Write-Host
PSBoundParameters {}
UnspecifiedParam
DefaultParam DefinedDefaultValue
.Link
https://technet.microsoft.com/library/hh847819.aspx
#>
[CmdletBinding()] Param(
[string]$UnspecifiedParam,
[string]$DefaultParam = 'DefinedDefaultValue'
)
$psdpv = @{}
$PSDefaultParameterValues.GetEnumerator() |
? Name -like 'Test-DefaultParam:*' |
% {[void]$psdpv.Add($_.Name,$_.Value)}
if(!$psdpv.Count)
{
[ordered]@{
InitialPSBoundParameters = $PSBoundParameters
InitialUnspecifiedParam = $UnspecifiedParam
InitialDefaultParam = $DefaultParam
}
$Script:PSDefaultParameterValues = @{
'Test-DefaultParam:UnspecifiedParam' = 'Script:PSDefaultParameterValues:UnspecifiedParam'
'Test-DefaultParam:DefaultParam' = 'Script:PSDefaultParameterValues:DefaultParam'
'Test-DefaultParam:Verbose' = $true
'Write-Host:Object' = 'Script:PSDefaultParameterValues:Write-Host'
}
Test-DefaultParam.ps1
$Local:PSDefaultParameterValues = @{
'Test-DefaultParam:UnspecifiedParam' = 'Local:PSDefaultParameterValues:UnspecifiedParam'
'Test-DefaultParam:DefaultParam' = 'Local:PSDefaultParameterValues:DefaultParam'
'Test-DefaultParam:Verbose' = $true
'Write-Host:Object' = 'Local:PSDefaultParameterValues:Write-Host'
}
Test-DefaultParam.ps1
}
else
{
Write-Verbose "PSDefaultParameterValues exist"
Write-Host
if(!$UnspecifiedParam) {Write-Warning 'UnspecifiedParam default was not received'}
if($DefaultParam -eq 'DefinedDefaultValue') {Write-Warning 'DefaultParam default was not received'}
if($VerbosePreference -eq 'SilentlyContinue') {Write-Warning 'Verbose default was not received'}
[ordered]@{
PSDefaultParameterValues = $psdpv
PSDPVUnspecifiedParam = $PSDefaultParameterValues['Test-DefaultParam:UnspecifiedParam']
PSDPVDefaultParam = $PSDefaultParameterValues['Test-DefaultParam:DefaultParam']
PSDPVVerbose = $PSDefaultParameterValues['Test-DefaultParam:Verbose']
PSDPVWriteHost = $PSDefaultParameterValues['Write-Host:Object']
PSBoundParameters = $PSBoundParameters
UnspecifiedParam = $UnspecifiedParam
DefaultParam = $DefaultParam
}
}
最佳答案
是的,它可以:
PS> Set-Content .\Test\FileName.ps1 @'
>>> [CmdletBinding()]
>>> param(
>>> $ParameterName='DefaultValue'
>>> )
>>> $ParameterName
>>> '@
PS> .\Test\FileName.ps1
DefaultValue
PS> $PSDefaultParameterValues['FileName.ps1:ParameterName']='DifferentValue'
PS> .\Test\FileName.ps1
DifferentValue
关于powershell - $PSDefaultParameterValues 可以将默认参数值发送到脚本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292518/
根据about_Parameters_Default_Values文档, The $PSDefaultParameterValues preference variable lets you spec
根据about_Parameters_Default_Values文档, The $PSDefaultParameterValues preference variable lets you spec
我是一名优秀的程序员,十分优秀!