gpt4 book ai didi

powershell - 确定 PowerShell 脚本是否已点源

转载 作者:行者123 更新时间:2023-12-03 23:23:51 25 4
gpt4 key购买 nike

从 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 test.ps1
$myinvocation.line

PS C:\scripts\test> ./test.ps1
./test.ps1

PS C:\scripts\test> . ./test.ps1
. ./test.ps1

您还可以检查 .invocationname 属性。如果脚本是点源的,它将只是一个点。如果不是,则是 ./scriptname.ps1

关于powershell - 确定 PowerShell 脚本是否已点源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com