本日のドハマリ

その1

hoge.ps1

Write-Host ("1:{0}" -f $MyInvocation.MyCommand.Path)

function GetCommandPath {
    $MyInvocation.MyCommand.path
}
Write-Host ("2:{0}" -f (GetCommandPath))

function GetCommandPath2 {
    $Script:MyInvocation.MyCommand.path
}
Write-Host ("3:{0}" -f (GetCommandPath2))

結果

1:C:\hoge.ps1
2:
3:C:\hoge.ps1

function スコープ内でも $MyInvocation が定義されていて、$Script スコープの変数を上書きしてしまうからですね。Write-Host ($MyInvocation | Out-String) とかやるとよく分かります。

参考 : ◆スクリプトファイルのパスを取得する(補足) - PowerShell

その2

fuga.ps1


$array = @("hogeru", "fuga", "piyo")
Write-Host ("1:{0}" -f ($array | Out-String))

function WriteArgs([string[]] $args) {
    $args | Out-String
}
Write-Host ("2:{0}" -f (WriteArgs$array))

function WriteMyArgs([string[]] $myArgs) {
    $myArgs | Out-String
}
Write-Host ("3:{0}" -f (WriteMyArgs$array))
1:hoge
fuga
piyo

2:
3:hoge
fuga
piyo

$args は自動変数だからですね。

参考 : 05.自動変数を使用して引数を受け取るには - HIRO's.NET

\( 'ω')/ウオオオオオオアアアアーーーーッッッ!!!!