Wednesday, March 25, 2009

PowerShell V2 CTP3 – Compatibility Problem - $MyInvocation

In PowerShell v1, you can use $MyInvocation.invocationname to test whether a script was dot-sourced or not.

This works in V1

if ($myinvocation.invocationname -ne ".") {
throw "Script should be dot-sourced to make this work"
}



but it not work in V2, and I cannot find a work-around :(




PS> '"invocation=" + $myinvocation.invocationname' | sc $env:temp\test.ps1
PS> &$env:temp\test.ps1
invocation=C:\Users\po\AppData\Local\Temp\test.ps1
PS> . $env:temp\test.ps1
invocation=C:\Users\po\AppData\Local\Temp\test.ps1
PS> # does it work when an alias is used?
PS> new-alias test $env:temp\test.ps1
PS> test
invocation=test
PS> . test
invocation=test



 



Update: Posted the problem on connect. MS has already closed my report with a 'fixed', so let's hope that is the case.

No comments: