确定 PowerShell 脚本是否已点源 [英] Determine if PowerShell script has been dot-sourced
问题描述
从 PowerShell 脚本中,我如何确定脚本是否是点源的,即它已被调用
<预><代码>..\myscript.ps1而不是
.\myscript.ps1
注意一个有趣的博客文章(也)关于这个:http://poshoholic.com/2008/03/18/powershell-deep-dive-using-myinvocation-and-invoke-expression-to-support-dot-sourcing-and-direct-invocation-in-shared-powershell-scripts/
检查 $myinvocation.line它将显示用于调用脚本的行.
PS C:\scripts\test>GC测试.ps1$myinvocation.linePS C:\scripts\test>./test.ps1./test.ps1PS C:\scripts\test>../test.ps1../test.ps1
您还可以检查 .invocationname 属性.如果脚本是点源的,它将只是一个点.如果不是,则是 ./scriptname.ps1
From a PowerShell script, how can I determine if the script has been dot-sourced, i.e. it has been called with
. .\myscript.ps1
rather than
.\myscript.ps1
NOTE an interesting blog post (also) about this: http://poshoholic.com/2008/03/18/powershell-deep-dive-using-myinvocation-and-invoke-expression-to-support-dot-sourcing-and-direct-invocation-in-shared-powershell-scripts/
Check $myinvocation.line It will show the line that was used to call the script.
PS C:\scripts\test> gc test.ps1
$myinvocation.line
PS C:\scripts\test> ./test.ps1
./test.ps1
PS C:\scripts\test> . ./test.ps1
. ./test.ps1
You can also check the .invocationname property. If the script was dot-sourced, it will just be a dot. If not, is will be ./scriptname.ps1
这篇关于确定 PowerShell 脚本是否已点源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!