简体   繁体   English

查找从EXE包装程序启动的Powershell脚本的完整路径

[英]Find full path of a powershell script launched from an EXE wrapper

I am trying to create an .exe file that is a packaged powershell script (script.ps1) in which part of the script is to print the current location of the .exe file NOT the Script. 我正在尝试创建一个.exe文件,该文件是打包的powershell脚本(script.ps1),其中脚本的一部分是打印.exe文件而不是脚本的当前位置。

Part of the Requirement is that the .exe needs to be portable: 要求的一部分是.exe需要可移植:

So when I run File.exe or Double-click File.exe: 因此,当我运行File.exe或双击File.exe时:

If the location of the file is C:\\Hello\\File.exe , it should print "C:\\Hello\\" 如果文件的位置是C:\\ Hello \\ File.exe ,则应显示“ C:\\ Hello \\”

If the location is moved to D:\\Goodbye\\File.exe , it should print "D:\\Goodbye\\" 如果该位置移到D:\\ Goodbye \\ File.exe ,则应打印“ D:\\ Goodbye \\”

If the the file is COPIED to D:\\HELLO\\File.exe , it should ONLY print "D:\\HELLO\\" 如果文件已复制到D:\\ HELLO \\ File.exe ,则应仅打印“ D:\\ HELLO \\”


I have tried $PSScriptRoot but that only links to a temp folder 我已经尝试过$PSScriptRoot但这仅链接到一个临时文件夹


What code can I add to the script.ps1 before packaging it as an .exe to make it look for the specific location of the .exe file? 在将其打包为.exe之前,可以将哪些代码添加到script.ps1中,以使其查找.exe文件的特定位置?

Thanks 谢谢

The tool you're using to wrap your PowerShell script in an *.exe , Ps1 to Exe , does the following when the *.exe is invoked: 调用*.exe时,用于将PowerShell脚本包装在*.exe (从Ps1到Exe )的工具将执行以下操作:

  • It extracts the embedded script to a temporary file in subtree $env:TEMP . 它将嵌入式脚本提取到子树$env:TEMP的临时文件中。

  • It invokes the PowerShell executable , and passes it the path to the temporary script file via -File , along with -ExecutionPolicy Bypass . 它调用PowerShell的可执行文件 ,并将其传递的路径,通过临时脚本文件-File ,连同-ExecutionPolicy Bypass

Example command line invoked by a wrapper *.exe : 包装程序*.exe调用的示例命令行:

"powershell" -ExecutionPolicy Bypass -File C:\Users\jdoe\AppData\Local\Temp\2409.tmp\240A.ps1

You therefore need to examine the parent process of the PowerShell instance running your script in order to obtain information about the wrapper *.exe . 因此,您需要检查运行脚本的PowerShell实例的进程,以获取有关包装程序*.exe

# Get the parent process.
# (`gps` is a built-in alias of `Get-Process`)
$pp = gps -Id (Get-CimInstance win32_process -Filter "ProcessId = $PID").ParentProcessId

# Output the parent process executable's directory path:
Split-Path $pp.Path

Note: If you're still on PSv2, use Get-WmiObject instead of Get-CimInstance . 注意:如果您仍在使用PSv2,请使用Get-WmiObject而不是Get-CimInstance

I've never worked with packaged PowerShell scripts, but if the PowerShell engine is being hosted by the EXE packaging the script, then something like this should do the trick: 我从未使用过打包的PowerShell脚本,但是如果PowerShell引擎是由打包脚本的EXE托管的,则可以使用以下方法解决问题:

$FullPathToEXE = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName

$DirectoryContainingEXE = [System.IO.Path]::GetDirectoryName($FullPathToEXE)

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

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