我在param脚本开头有了以下块
param(
[string]$command,
[string]$version = "1.1.0"
)
Run Code Online (Sandbox Code Playgroud)
这是好的,只是我需要$version给不成为一个位置参数,因此,如果您键入
.\script.ps1 run argument
Run Code Online (Sandbox Code Playgroud)
然后$args应该包含"参数",$version应该是"1.1.0".我知道我可以使用C#Cmdlet来实现它,但如果我能将它作为单个脚本提供,那将会更方便.
在PowerShell 1.0中,不可能是AFAIK.如果您不希望它是位置的,则需要删除$ version参数.
在PowerShell 2.0中,您可以通过创建一个高级函数来获得所需的内容,在该函数中您可以在Parameter属性中指定其他信息,例如:
function foo {
param(
[Parameter(Mandatory=$true, Position = 0)]
[string]
$command,
[Parameter()]
[string]
$version = "1.1.0",
[Parameter(ValueFromRemainingArguments = $true)]
$remainingArgs
)
Process {
$OFS = ','
"command is $command"
"version is $version"
"remainingArgs are $remainingArgs"
}
}
foo run argument
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2106 次 |
| 最近记录: |