gpt4 book ai didi

powershell - $PSDefaultParameterValues 可以将默认参数值发送到脚本吗?

转载 作者:行者123 更新时间:2023-12-02 21:11:13 33 4
gpt4 key购买 nike

根据about_Parameters_Default_Values文档,

The $PSDefaultParameterValues preference variable lets you specify custom default values for any cmdlet or advanced function.

但是,实际上,这似乎不适用于脚本高级功能(请参阅下面脚本中的示例部分)。

同时使用ScriptLocal范围,核心PowerShell Cmdlet的默认值(即Write-Host)工作正常,但没有一个脚本本身的参数是从 $PSDefaultParameterValues 接收的,无论是否定义了默认值,或者是否是公共(public)参数。

测试-DefaultParam.ps1
<#
.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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com