Powershell Win-PS2EXE-尊重生成的可执行文件的位置
将这些设置用于程序: 这样,当单击Powershell Win-PS2EXE-尊重生成的可执行文件的位置,powershell,Powershell,将这些设置用于程序: 这样,当单击exe文件时,控制台将显示 该代码: $inf_file = "$PSScriptRoot\setup-files\install.inf" write-host """$inf_file""" timeout 10 假设新可执行文件的路径是W:\Apps\Install Scheme.exe 这意味着$inf\u文件在这里W:\Apps\setup files\install.inf 当我点击转换的exe文件时,我得到了这个 是否有任何方法可以获取W:
exe
文件时,控制台将显示
该代码:
$inf_file = "$PSScriptRoot\setup-files\install.inf"
write-host """$inf_file"""
timeout 10
假设新可执行文件的路径是W:\Apps\Install Scheme.exe
这意味着$inf\u文件在这里W:\Apps\setup files\install.inf
当我点击转换的exe文件时,我得到了这个
是否有任何方法可以获取W:\Apps\setup files\install.inf的正确路径,以便可执行文件在单击时识别自身的位置
我认为,$PSScriptRoot
可以工作
我不知道如何解决这个问题,因为exe文件最终将取决于知道它的位置。以下是可以实现这一点的代码
Function Get-PSScriptPath {
<#
.SYNOPSIS
Returns the current filepath of the .ps1 or compiled .exe with Win-PS2EXE.
.DESCRIPTION
This will return the path of the file. This will work when the .ps1 file is
converted with Win-PS2EXE
.NOTES
Author: Ste
Date Created: 2021.05.03
Tested with PowerShell 5.1 and 7.1.
Posted here: https://stackoverflow.com/q/60121313/8262102
.PARAMETER None
NA
.INPUTS
None. You cannot pipe objects to Get-PSScriptPath.
.OUTPUTS
Returns the current filepath of the .ps1 or compiled .exe with Win-PS2EXE.
.EXAMPLE (When run from a .ps1 file)
PS> Get-PSScriptPath
PS> C:\Users\Desktop\temp.ps1
.EXAMPLE (When run from a compiled .exe file with Win-PS2EXE.
PS> Get-PSScriptPath
PS> C:\Users\Desktop\temp.exe
#>
if ([System.IO.Path]::GetExtension($PSCommandPath) -eq '.ps1') {
$psScriptPath = $PSCommandPath
} else {
# This enables the script to be compiles and get the directory of it.
$psScriptPath = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
}
return $psScriptPath
}
Get-PSScriptPath
函数获取PSScriptPath{
获取PSScriptPath
PS>C:\Users\Desktop\temp.ps1
.EXAMPLE(使用Win-PS2EXE从编译的.exe文件运行时)。
PS>获取PSScriptPath
PS>C:\Users\Desktop\temp.exe
#>
if([System.IO.Path]::GetExtension($PSCommandPath)-eq.ps1'){
$psScriptPath=$PSCommandPath
}否则{
#这样可以编译脚本并获取其目录。
$PSScript路径=[System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
}
返回$psScriptPath
}
获取PSScriptPath
以下是可以实现这一点的代码
Function Get-PSScriptPath {
<#
.SYNOPSIS
Returns the current filepath of the .ps1 or compiled .exe with Win-PS2EXE.
.DESCRIPTION
This will return the path of the file. This will work when the .ps1 file is
converted with Win-PS2EXE
.NOTES
Author: Ste
Date Created: 2021.05.03
Tested with PowerShell 5.1 and 7.1.
Posted here: https://stackoverflow.com/q/60121313/8262102
.PARAMETER None
NA
.INPUTS
None. You cannot pipe objects to Get-PSScriptPath.
.OUTPUTS
Returns the current filepath of the .ps1 or compiled .exe with Win-PS2EXE.
.EXAMPLE (When run from a .ps1 file)
PS> Get-PSScriptPath
PS> C:\Users\Desktop\temp.ps1
.EXAMPLE (When run from a compiled .exe file with Win-PS2EXE.
PS> Get-PSScriptPath
PS> C:\Users\Desktop\temp.exe
#>
if ([System.IO.Path]::GetExtension($PSCommandPath) -eq '.ps1') {
$psScriptPath = $PSCommandPath
} else {
# This enables the script to be compiles and get the directory of it.
$psScriptPath = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
}
return $psScriptPath
}
Get-PSScriptPath
函数获取PSScriptPath{
获取PSScriptPath
PS>C:\Users\Desktop\temp.ps1
.EXAMPLE(使用Win-PS2EXE从编译的.exe文件运行时)。
PS>获取PSScriptPath
PS>C:\Users\Desktop\temp.exe
#>
if([System.IO.Path]::GetExtension($PSCommandPath)-eq.ps1'){
$psScriptPath=$PSCommandPath
}否则{
#这样可以编译脚本并获取其目录。
$PSScript路径=[System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
}
返回$psScriptPath
}
获取PSScriptPath
要提供一个实用、简洁的替代方案(PSv3+),始终将脚本路径报告为完整路径:
一艘班轮:
$scriptDir = if (-not $PSScriptRoot) { Split-Path -Parent (Convert-Path ([environment]::GetCommandLineArgs()[0])) } else { $PSScriptRoot }
注释表格:
$scriptDir = if (-not $PSScriptRoot) { # $PSScriptRoot not defined?
# Get the path of the executable *as invoked*, via
# [environment]::GetCommandLineArgs()[0],
# resolve it to a full path with Convert-Path, then get its directory path
Split-Path -Parent (Convert-Path ([environment]::GetCommandLineArgs()[0]))
}
else {
# Use the automatic variable.
$PSScriptRoot
}
要提供一个实用、简洁的备选方案(PSv3+),始终将脚本路径报告为完整路径,请执行以下操作:
一艘班轮:
$scriptDir = if (-not $PSScriptRoot) { Split-Path -Parent (Convert-Path ([environment]::GetCommandLineArgs()[0])) } else { $PSScriptRoot }
注释表格:
$scriptDir = if (-not $PSScriptRoot) { # $PSScriptRoot not defined?
# Get the path of the executable *as invoked*, via
# [environment]::GetCommandLineArgs()[0],
# resolve it to a full path with Convert-Path, then get its directory path
Split-Path -Parent (Convert-Path ([environment]::GetCommandLineArgs()[0]))
}
else {
# Use the automatic variable.
$PSScriptRoot
}
问:那么你想让新的.exe在运行时“记住”$PSScriptRoot的值"当你运行PS2EXE/Win-PS2EXE时?问:你有什么理由不能在你的PS脚本中使用Get Location
?没错。我想让exe也使用path变量,这样它的行为也会一样。我在主页上找到了答案。请检查下面的答案。问:你想让新的.exe运行吗在运行时“记住”$PSScriptRoot的值“当你运行PS2EXE/Win-PS2EXE时?问:你有什么理由不能在你的PS脚本中使用Get Location
?没错。我希望exe
也使用path变量,这样它就可以以同样的方式运行。我在主页上找到了答案。检查下面的答案。