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

标签 powershell

从 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/

相关文章:

azure - Powershell循环直到输出一行

powershell - 如何以管理员身份运行 powershell 脚本?

Powershell : The scope number '4' exceeds the number of active scopes

powershell - RoboCopy 来源

java - 将 Powershell 与 FireDaemon 结合使用

html - PowerShell 如何计算邮件中 html 表中的所有行

windows - 在 Windows 中获取一个大文件的最后 n 行或字节(如 Unix 的尾部)。避免耗时的选择

powershell - Plink和PowerShell的参数问题

powershell - 文件系统对象:MoveFolder提供随机的 “Permission Denied”错误

sql-server - 如何使用 SQL Server PowerShell 提供程序和/或 SMO 创建新登录名?