Home > Software design >  Script calls other script as .ps1 but not as a .exe using ps2exe
Script calls other script as .ps1 but not as a .exe using ps2exe

Time:02-16

$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. But when I transform it in .exe with the help of ps2exe, he doesn't do his job anymore. 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 ?

Thanks

CodePudding user response:

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.

When running an actual .ps1 file, you'd use the following about_Automatic_Variables:

  • $PSCommandPath contains the the executing script file's full file path.

  • $PSScriptRoot contains the script file's full directory path (i.e. the full path of the directory in which the script file is located).

In a ps2exe-compiled executable (.exe), where these variables have no values, you can use the following instead:

  • [Environment]::GetCommandLineArgs()[0] contains the executable file's full file path.

  • Split-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0]) contains the executable file's full directory path.


Applied to your code - assuming that a separate StartApp.ps1 file is present alongside your .exe file:[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:

$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.

  • Related