$MyInvocation.MyCommand.Path 返回 NULL [英] $MyInvocation.MyCommand.Path returning NULL
问题描述
我有以下代码作为较长脚本的开头:
I have the following code as the beginning of a longer script:
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
& $ScriptPath\build_functions.ps1
& $ScriptPath\build_builddefs.ps1
这个想法是获取正在运行的脚本的路径并使用该路径调用一些支持脚本.但是,当我单独测试它以确保它可以工作时(通过突出显示该块并仅运行该代码),我收到以下错误:
The idea is to get the path of the script being run and use that path to call some supporting scripts. However when I went to test this out in isolation to make sure it could work (by highlighting that block and running just that code), I got the following error:
Split-Path: Cannot bind argument to parameter 'Path' because it is null.
有趣的是,当我运行整个脚本时,它似乎单独运行这些文件.关于 ISE 如何处理运行选择而不是完整脚本,我是否遗漏了什么?运行选择时它不会建立文件系统上下文吗?
Interestingly enough, when I run the entire script it seems to run these files separately. Is there something I'm missing about how the ISE handles running a selection rather than the full script? Does it not establish a file system context when you run a selection?
推荐答案
$MyInvocation
是一个 自动变量在脚本运行时填充,然后如果您在powershell控制台
中执行$MyInvocation.MyCommand.Path
或ISE
未填充;
$MyInvocation
is an automatic variable populated at script run time, then if you execute $MyInvocation.MyCommand.Path
in a powershell console
or ISE
isn't populated;
这就是为什么在您的测试中 $ScriptPath
没有值 ($null
)
that's why in your test the $ScriptPath
has no value ($null
)
这篇关于$MyInvocation.MyCommand.Path 返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!