-1

公式の Microsoftチュートリアルに従って、1 つの必須の位置引数と、ValueFromRemainingArguments. 私の試み:

function main {
    Param(
        [String]
        [Parameter(Mandatory = $true, Position = 0)]
        $FOO,
        [String[]]
        [Parameter(Position = 1, ValueFromRemainingArguments)]
        $BAR
    )
    
    Write-Host mandatory arg: $FOO
    Write-Host additional args: $BAR

}

&main $args[0] $args

スクリプトを実行しようとすると、次の出力が得られます。

PS C:\ps_scripts> .\script.ps1 foo bar bar2 bar3
mandatory arg: foo
additional args: foo bar bar2 bar3

期待される出力:

PS C:\ps_scripts> .\script.ps1 foo bar bar2 bar3
mandatory arg: foo
additional args: bar bar2 bar3

目的の出力を生成する方法は?

引数をコンマで区切ると:

PS C:\ps_scripts> .\script.ps1 foo bar,bar2,bar3

出力は次のとおりです。

PS C:\ps_scripts> .\script.ps1 foo bar,bar2,bar3
mandatory arg: foo
additional args: foo System.Object[]
4

1 に答える 1

0

最後の行は次のように変更する必要があります。

&main $args[0] $args[1]
于 2020-10-31T22:06:14.760 に答える