powershell - Win-PS2EXE-尊重生成的可执行文件的位置

标签 powershell

对程序 Win-PS2EXE 进行以下设置:

这样一来,当单击exe文件时,控制台将显示出来。

Win-PS2EXE

这段代码:

$inf_file = "$PSScriptRoot\setup-files\install.inf"
write-host """$inf_file"""
timeout 10

我们说新可执行文件的路径是W:\Apps\Install Scheme.exe
这意味着$inf_file在这里W:\Apps\setup-files\install.inf
当我单击转换后的exe文件时,我得到了。

enter image description here

有什么方法可以获取W:\Apps\setup-files\install.inf的正确路径,以便可执行文件在单击时可以识别其自身的位置。

我以为$PSScriptRoot可以工作。

我不知道如何解决此问题,因为exe文件最终将取决于知道其位置。

最佳答案

这是可以完成此任务的代码。

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

See the bottom of this page.

PS2EXE-GUI:使用GUI 将PowerShell脚本“转换”为EXE文件
https://gallery.technet.microsoft.com/PS2EXE-GUI-Convert-e7cb69d5

关于powershell - Win-PS2EXE-尊重生成的可执行文件的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60121313/

相关文章:

PowerShell 不能使用匹配的枚举类型?

powershell - 在Powershell中从左到右抓取文件中每行的前10个字符

c# - 如何在 Windows 窗体应用程序中运行 PowerShell 命令?

c# - PowerShell 在 NavigateComplete2 事件触发时崩溃

powershell - 从 PowerShell 调用 CreateProcess

用于字符串中可选等号分隔符的 Powershell 正则表达式

powershell - Powershell添加不需要的小数(加法)

powershell - 遍历哈希表中的日期?

powershell - 如何在没有附带堆栈跟踪的情况下在 PowerShell 中显示 'naked' 错误消息?

Powershell 服务帐户密码更改登录失败