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

  • Where is this script getting the ```$MyInvocation.MyCommand.Definition``` from ? Also, You are hard coding your password ? (Not very secure) – dcaz Feb 15 '22 at 14:16
  • For the password, yeah, not the best, but that script will pretty much be a one / two times use by me (Will work on the encryption if a need to use it more often). For the $MyInvocation, I only use it as a automatic variable for the script current directory... Which can explain why that don't work... But still don't know how to make it work... – Black_n_White Feb 15 '22 at 14:28

1 Answers1

6

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775