简体   繁体   English

$MyInvocation.MyCommand.Name 将 ps1 转换为 exe 后返回空值

[英]$MyInvocation.MyCommand.Name return null value after converting ps1 to exe

powershell script results in incorrect output after converting it to an .exe-file and executing it:将 powershell 脚本转换为 .exe 文件并执行后,会导致输出不正确:

$MyScriptName = $MyInvocation.MyCommand.Name

Above is snippet of my powershell code, which works fine and saves the script name in the $MyScriptName variable.以上是我的 powershell 代码片段,它工作正常并将脚本名称保存在$MyScriptName变量中。

Once we convert the .ps1-script to an .exe-file and execute it, the variable $MyScriptName has a null value.一旦我们将 .ps1 脚本转换为 .exe 文件并执行它,变量$MyScriptName有一个空值。

Script related variables become unavailable after converting the script to an executable file.将脚本转换为可执行文件后,脚本相关变量将不可用。 You can find information about it here on Markus Scholtes' release page on Github for the PS2EXE-GUI version (Win-PS2EXE): PS2EXE-GUI您可以在 Markus Scholtes 在 Github 上的 PS2EXE-GUI 版本 (Win-PS2EXE) 发布页面上找到有关它的信息: PS2EXE-GUI

Here's the code you need to add to your script.这是您需要添加到脚本中的代码。

if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript")
{ $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition }
else
{ $ScriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM