関数内での書式演算子の動作が、単純なスクリプトとは異なることがわかりました。期待どおりに動作する簡単な例を次に示します。
[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
だから今、私はこれについて何を考えるべきかわかりません。