Search code examples
powershellexestart-processps1ps2exe-gui

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


Solution

  • 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 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 file name or path as invoked (when calling from cmd.exe) or as a full path (when calling from PowerShell).

      • Therefore, to ensure that a full path is always used, pass the result to Convert-Path
    • Split-Path (Convert-Path -LiteralPath ([Environment]::GetCommandLineArgs()[0])) obtains 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 = 
      if ($PSScriptRoot) { # running as .ps1 file
        $PSScriptRoot 
      } 
      else {               # running as .exe
        Split-Path (Convert-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.