powershellwinformsstart-processiexpress

PowerShell based GUI form not loading ( Closing with a Flash) when called from Other PowerShell wrapper compiled in EXE through IExpress


I am currently working on creating a GUI-based application that involves calling Start-Process with specific parameters. The application works as expected when executed in both PowerShell.exe and ISE. However, I am encountering a problem when attempting to wrap the scripts (TEST1.PS1 + TEST2.PS1 + Cred Module) into an executable using IExpress.

When I run the executable, I encounter a flashing screen with no visible output before the window closes. Interestingly, I don't receive any error messages in the output or the Event Viewer.

Here's a simplified version of the code I'm using:

TEST1.PS1

    $userwho1 = whoami
    Import-Module "$PSScriptRoot\CredentialManager.psm1"
    $credential = Get-StoredCredential "MYADMIN"
    $fileExists = Test-Path -Path "$PSScriptRoot\TEST2.ps1"
    if ($fileExists) {
    Write-Host "The file exists: $PSScriptRoot\TEST2.ps1"
    $Release = "$PSScriptRoot\TEST2.ps1"
   } else {
    Write-Host "The file does not exist."
   }
    Start-Process -FilePath 
  'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' 
    -WindowStyle hidden -ArgumentList "-executionpolicy bypass -File 
   $Release $userwho1" -Credential $credential -WorkingDirectory 
    'C:\Windows\System32'

Here is a screenshot confirming that the script is in place and detected at runtime.

Path confirmation

My expectation is that Start-Process should call TEST2.PS1 (a user form) with the privileges of the MYADMIN domain user.

Additional Information:

PowerShell version: 5.1 I suspect that the issue might be related to how I'm wrapping the scripts using IExpress. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated. Thank you!


Solution

  • I recommend adding in extra Batch file to run the Powershell file that runs the actual code.

    Also, may be unrelated, but you can use an empty Batch file and execute it as the Post-Install Command.

    An another "also", you seem to use the credentials module from $PSScriptRoot, so make sure if the module is actually contained in that directory. If you want to import the CredentialManager.psm1 from your current work directory (most of the time the directory from where the Powershell is contained in temporarily) use $PWD.Path/CredentialManager.psm1.