简体   繁体   English

脚本使用 ps2exe 调用其他脚本 as.ps1 但不是 a.exe

[英]Script calls other script as .ps1 but not as a .exe using ps2exe

$password = ConvertTo-SecureString “Password+++” -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ("Admin", $password)
$FileLocale = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Write-Output $FileLocale
$AntFile = "$FileLocale\StartApps.ps1"
Write-Output $AntFile
Start-Process PowerShell.exe -ArgumentList "-command &$AntFile -UserCredential $Cred"  

Hi, that code works in.ps1, I call the other script, and he makes his job.嗨,该代码在 .ps1 中有效,我调用了另一个脚本,他完成了工作。 But when I transform it in.exe with the help of ps2exe, he doesn't do his job anymore.但是当我借助 ps2exe 将它转换为 in.exe 时,他就不再做他的工作了。 As admin or not.是否作为管理员。 It's not the first time I use that start-process, but it's the first time I use a variable as a target for the command.这不是我第一次使用该启动过程,但这是我第一次使用变量作为命令的目标。 Do anyone know what go wrong between the ps1 and exe?有谁知道ps1和exe之间的go有什么问题吗?

Thanks谢谢

While an executable compiled with ps2exe uses a .ps1 file as input , at runtime no actual .ps1 file is involved, which is why PowerShell's command-reflection variables cannot tell you anything about a running script file .虽然使用ps2exe编译的可执行文件使用.ps1文件作为输入但在运行时不涉及实际的.ps1文件,这就是为什么 PowerShell 的命令反射变量无法告诉您有关正在运行的脚本文件的任何信息。

When running an actual .ps1 file , you'd use the following about_Automatic_Variables :运行实际的.ps1文件时,您将使用以下about_Automatic_Variables

  • $PSCommandPath contains the the executing script file 's full file path. $PSCommandPath包含执行脚本文件的完整文件路径。

  • $PSScriptRoot contains the script file's full directory path (ie the full path of the directory in which the script file is located). $PSScriptRoot包含脚本文件的完整目录路径(即脚本文件所在目录的完整路径)。

In a ps2exe-compiled executable ( .exe ) , where these variables have no values, you can use the following instead :在 ps2exe 编译的可执行文件 ( .exe )中,这些变量没有值,您可以改用以下内容

  • [Environment]::GetCommandLineArgs()[0] contains the executable file 's full file path. [Environment]::GetCommandLineArgs()[0]包含可执行文件的完整文件路径。

  • Split-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0]) contains the executable file's full directory path. Split-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0])包含可执行文件的完整目录路径。


Applied to your code - assuming that a separate StartApp.ps1 file is present alongside your .exe file: [1]应用于您的代码 - 假设您的.exe文件旁边有一个单独的StartApp.ps1文件: [1]

$FileLocale = Split-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0])
$AntFile = Join-Path $FileLocale StartApps.ps1

If you want to make your code work in both invocation scenarios - the original .ps1 file directly as well as the compiled .exe file - use the following:如果你想让你的代码在两种调用场景中工作- 原始.ps1文件直接以及编译后的.exe文件 - 使用以下内容:

$FileLocale = 
  if ($PSScriptRoot) { $PSScriptRoot }
  else { Split-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0]) }

$AntFile = Join-Path $FileLocale StartApps.ps1

[1] Note that at runtime no information is available about where the original .ps1 file that served as compile-time input was originally located - only that file's content becomes part of the .exe file. [1] 请注意,在运行时,没有关于用作编译时输入原始.ps1文件最初位于何处的可用信息 - 只有该文件的内容成为.exe文件的一部分。

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

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