0

関数内での書式演算子の動作が、単純なスクリプトとは異なることがわかりました。期待どおりに動作する簡単な例を次に示します。

[string]$name = 'Scripting Guy'
[string]$statement = 'PowerShell rocks'
$s = "The {0} thinks that {1}!" -f $name, $statement
write-host $s

生産:

The Scripting Guy thinks that PowerShell rocks!

関数内では、別のことを行います。

function myFunc( [string] $iname, [string] $istatement) { 
    $s = "The {0} thinks that {1}!" -f $iname, $istatement
    write-host $s
}

[string]$name = 'Scripting Guy'
[string]$statement = 'PowerShell rocks'
myFunc($name, $statement)

生成:

The Scripting Guy PowerShell rocks thinks that !

私はそれが何をしているのかを知るためにそれで遊んでみました:

function myFunc( [string] $iname, [string] $istatement) { 
    $s = "The {0} thinks that {1}! {2} {3}" -f $iname, $istatement, "=====", $iname
    write-host $s
}

[string]$name = 'Scripting Guy'
[string]$statement = 'PowerShell rocks'
myFunc($name, $statement)

これにより、次が生成されます。

The Scripting Guy PowerShell rocks thinks that ! ===== Scripting Guy PowerShell rocks

だから今、私はこれについて何を考えるべきかわかりません。

4

1 に答える 1

3

次のように関数を呼び出す必要があります。

myFunc -iname "Scripting Guy" -istatement "Powershell Rocks!!"

また

myFunc $name $statement

使用している現在のメソッドは単一の配列オブジェクトを渡すため、要素が連続して出力されます

于 2013-09-30T15:33:01.087 に答える