Compiled EXE does not run (used to work)

Visual Studio CODE
I have previously compiled sources OK.
Tried today and compile log shows completion and output of EXE (Failed to locate Import-Xaml warning).
When I run output EXE in command-line, it does nothing.
++
I stepped back to basics:

  • Created a PowerShell with two lines:
    Write-Host “Hello World”
    “Hello World” | Out-Host
    Compiled taking all defaults. Default PSD1 too
    Ran, but no output.

Windows 10. .Net 4.6.2, 4.7.2, .4.8, 5.0.x, Core SDK 3.1.411 all loaded/available. Latest Powershell Pro Tools.

Any tips or clues to diagnose, greatfully accepted?

+++

Starting PowerShell Pro Tools host from: c:\Users\aprob.vscode\extensions\ironmansoftware.powershellprotools-5.29.4\out\windows\PowerShellProTools.Host.exe}
Process started
[4/08/2021 11:27:47 PM] connect

Connecting to PowerShell process 14448
[4/08/2021 11:27:49 PM] Opening a remote runspace.

[4/08/2021 11:27:51 PM] Loading PowerShell Pro Tools module
[4/08/2021 11:27:51 PM] Executing command: [System.Reflection.Assembly]::LoadFrom(‘c:\Users\aprob.vscode\extensions\ironmansoftware.powershellprotools-5.29.4\out\windows\PowerShellProTools.VSCode.dll’)

[4/08/2021 11:27:52 PM] Executing command: Import-Module ‘c:\Users\aprob.vscode\extensions\ironmansoftware.powershellprotools-5.29.4\out\windows\PowerShellProTools.VSCode.dll’

[4/08/2021 11:27:52 PM] Executing command: $Env:PSModulePath += ‘;c:\Users\aprob.vscode\extensions\ironmansoftware.powershellprotools-5.29.4\out\windows…\Modules’

[4/08/2021 11:27:52 PM] Configuring runspace scheduler.

[4/08/2021 11:27:52 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:53 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:53 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:53 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:53 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:53 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:54 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:55 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:56 PM] Executing command:
Get-Runspace | ForEach-Object {
$h = $.GetType().GetProperty(‘Host’, [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($
)
if ($h.Name -eq ‘Visual Studio Code Host’)
{
$_.Id
}
}

[4/08/2021 11:27:56 PM] Found VS Code runspace: 5
[4/08/2021 11:27:56 PM] Executing command:
$RS = Get-Runspace -Id 5
$RS.Events.SubscribeEvent($null, ‘PowerShell.OnIdle’, ‘PowerShell.OnIdle’, $null, {
try {
$PSPCommand = [PowerShellProTools.CommandQueue]::GetCommand()
if ($PSPCommand)
{
$PSPResults = Invoke-Expression $PSPCommand.Value
[PowerShellProTools.CommandQueue]::SetResults($PSPResults, $PSPCommand)
}
} catch { [PowerShellProTools.CommandQueue]::SetResults($_.ToString(), $PSPCommand) }
}, $true, $false)

[4/08/2021 11:27:56 PM] Scheduling command for main runspace: Import-Module ‘c:\Users\aprob.vscode\extensions\ironmansoftware.powershellprotools-5.29.4\out\windows\PowerShellProTools.VSCode.dll’ -Scope Global

Connected to PowerShell process.
Started PowerShell Pro Tools process.
[4/08/2021 11:28:57 PM] Scheduling command for main runspace: $Commands = Get-Command ‘Write-Host’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

[4/08/2021 11:29:05 PM] Scheduling command for main runspace: $Commands = Get-Command ‘O’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

[4/08/2021 11:29:06 PM] Scheduling command for main runspace: $Commands = Get-Command ‘Ou’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

[4/08/2021 11:29:07 PM] Scheduling command for main runspace: $Commands = Get-Command ‘Out-’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

[4/08/2021 11:29:07 PM] Scheduling command for main runspace: $Commands = Get-Command ‘Out-H’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

INFO: Checking license

INFO: OutputPath is c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out

INFO: Bundling c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\Hello-World.ps1

INFO: Parsing file c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\Hello-World.ps1.

INFO: Bundling XAML

INFO: Failed to locate Import-Xaml

INFO: Packaging C:\Users\aprob\AppData\Local\Temp\Hello-World.ps1

INFO: Creating temp directory: C:\Users\aprob\AppData\Local\Temp\534acf5cd2b9454c92067c84a6c797e5

INFO: Packaging modules…

INFO: Checking dotnet version.

INFO: Checking dotnet version.

INFO: 5.0.205

INFO: 5.0.205

INFO: Creating package project.

INFO: Using .NET Framework version: net462

INFO: Determining projects to restore…
Restored C:\Users\aprob\AppData\Local\Temp\534acf5cd2b9454c92067c84a6c797e5\Hello-World.csproj (in 154 ms).

INFO: Determining projects to restore…
Restored C:\Users\aprob\AppData\Local\Temp\534acf5cd2b9454c92067c84a6c797e5\Hello-World.csproj (in 154 ms).

INFO: Packaging C:\Users\aprob\AppData\Local\Temp\Hello-World.ps1 → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

INFO: Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore…
All projects are up-to-date for restore.
Hello-World → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:05.47

INFO: Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore…
All projects are up-to-date for restore.
Hello-World → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:05.47

Strange. I tried this myself and see the output.

That said, I’m also seeing the “Failed to locate Import-Xaml” call.

Can you check in Event Viewer to see if the executable is crashing in the background? I would imagine you would see a .NET Runtime error referring to Hello-World.exe

Thanks for tip.

Windows Event log shows issue with kernelbase.dll

Faulting application name: Hello-World.exe, version: 1.0.0.0, time stamp: 0x98c3d186

Faulting module name: KERNELBASE.dll, version: 10.0.19041.1110, time stamp: 0xc830c52d

Exception code: 0xe0434352

Fault offset: 0x0012b4b2

Faulting process id: 0x36bc

Faulting application start time: 0x01d789f47fbbf78e

Faulting application path: C:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll

Report Id: de8096ee-f934-41c5-b615-f687d88c1c1c

Faulting package full name:

Faulting package-relative application ID:

Application: Hello-World.exe

Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception.

Exception Info: System.ComponentModel.Win32Exception

Exception Info: System.Management.Automation.Host.HostException

at Microsoft.PowerShell.ConsoleControl+<>c.<.cctor>b__105_1()

at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].CreateValue()

at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].LazyInitValue()

at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_Value()

at Microsoft.PowerShell.ConsoleControl.GetActiveScreenBufferHandle()

at Microsoft.PowerShell.ConsoleControl.UpdateLocaleSpecificFont()

at Microsoft.PowerShell.ConsoleShell.Start(System.Management.Automation.Runspaces.RunspaceConfiguration, System.String, System.String, System.String, System.String[])

at Microsoft.PowerShell.ConsoleShell.Start(System.Management.Automation.Runspaces.RunspaceConfiguration, System.String, System.String, System.String[])

at PowerShellToolsPro.Packager.ConsoleHost.Program.Main(System.String[])

Tried many commands to fix:
sfc /scannow

DISM /Online /Cleanup-Image /CheckHealth

<img width=“411” height=“182” style=“width:4.2812in;height:1.8958in” id=“Picture_x0020_6” src="//cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/flex032/uploads/universaldashboard/original/2X/5/5e46f8962716a7706a17aa465f9a3dffc889d4bb.png" alt="Graphical user interface, text, application

Description automatically generated">

Not sure, but maybe Microsoft have broken something with an update?

image002.jpg

image003.png

@andypprobert - Are you using the DisableQuickEdit setting in PoshTools? It’s off by default but based on the stack trace it could cause this problem.

Re-packaged with DisableQuickEdit = $true
Same issue.
Also, copied EXE to another endpoint - same issue.

Running Windows 10, in VMWare guest.

@{
    Root = 'c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\Hello-World.ps1'
    OutputPath = 'c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out'
    Package = @{
        Enabled = $true
        Obfuscate = $false
        HideConsoleWindow = $false
        DotNetVersion = 'v4.6.2'
        FileVersion = '1.0.0'
        FileDescription = ''
        ProductName = ''
        ProductVersion = ''
        Copyright = ''
        RequireElevation = $false
        ApplicationIconPath = ''
        PackageType = 'Console'
        DisableQuickEdit = $true # Avoid .Net crash?
    }
    Bundle = @{
        Enabled = $true
        Modules = $true
        # IgnoredModules = @()
    }
}

image

image

++++++

[6/08/2021 9:20:17 AM] Scheduling command for main runspace: Get-Variable | Out-PoshToolsVariable -PassThru | ConvertTo-Json -Depth 1 -WarningAction SilentlyContinue

INFO: Checking license

INFO: OutputPath is c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out

INFO: Bundling c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\Hello-World.ps1
INFO: Parsing file c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\Hello-World.ps1.

INFO: Bundling XAML

INFO: Failed to locate Import-Xaml

INFO: Packaging C:\Users\aprob\AppData\Local\Temp\Hello-World.ps1
INFO: Creating temp directory: C:\Users\aprob\AppData\Local\Temp\28834808ae0e49379b04552d275b2bf1

INFO: Packaging modules…

INFO: Checking dotnet version.
INFO: Checking dotnet version.

INFO: 5.0.205

INFO: 5.0.205

INFO: Creating package project.

INFO: Using .NET Framework version: net462

INFO: Determining projects to restore…
Restored C:\Users\aprob\AppData\Local\Temp\28834808ae0e49379b04552d275b2bf1\Hello-World.csproj (in 161 ms).

INFO: Determining projects to restore…
Restored C:\Users\aprob\AppData\Local\Temp\28834808ae0e49379b04552d275b2bf1\Hello-World.csproj (in 161 ms).

INFO: Packaging C:\Users\aprob\AppData\Local\Temp\Hello-World.ps1 → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

INFO: Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore…
All projects are up-to-date for restore.
Hello-World → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.18

INFO: Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore…
All projects are up-to-date for restore.
Hello-World → c:\Users\aprob\Documents\GitHub\Tools\Test-POSH\out\Hello-World.exe

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.18

[6/08/2021 9:23:39 AM] Scheduling command for main runspace: $Commands = Get-Command ‘INFO:’ -ErrorAction SilentlyContinue; if ($Commands) { try { [System.Management.Automation.ProxyCommand]::Create((New-Object System.Management.Automation.CommandMetaData $Commands)) } catch { } } else { ‘’ }

I’m going to add some better error handling and reporting into this so we can catch the exception and display the error code. It’s currently throwing this exception:

The Win32 internal error “{0}” 0x{1:X} occurred while retrieving the handle for the active console output buffer. Contact Microsoft Customer Support Services.

If we catch it in the PS host we can see what the error code is and get some more information. I’m using a windows build in between the two builds you’ve shared. I tried running this from cmd, Windows PowerShell, PS7 and Windows terminal and haven’t been able to reproduce.

EDIT: 5.30.1 now provides better error handling in this scenario. Can you try packaging again and let me know what the error reports in the console window?

I have also tried .net 472 480 PowerShell 7.0.6 or something like that.

Also .necoreapp31 - the others logged error. This variant silently did nothing.

I am running Malwarebytes which has AMSI monitor.

Shouldn I unload that to test too?

Andrew Probert

Snr.Engineer, CISSP

0451 502 181

You can certainly try that. Antivirus has been notoriously picky about PowerShell scripts compiled into executables. Can you also try version 5.30.1 of PoshTools to see if it presents a different error message?

Tried this version. Now there is no error showing in Windows event log. Exe is run from CMD.EXE window. No output - silent failure.

Changed 1st line to
Throw “test is a throw”

No output.

Toggled quickedit=$true - still no output.

Currently PowerShell 5.0.205. Tried .net462 and .net472 - no change

Are you double clicking the exe to run it? If so, can you try running it from the command line?

I have only been running from Windows command line.

Can you open a support ticket for this? I’d like to get on a call to take a look. Email support@ironmansoftware.com

Please be sure to post the result/answer here, for those of us that are curious.

1 Like

After some research, it looks like I’ve stumbled upon the root cause and will now need to figure out a solution.

It appears that the .NET SDK that PoshTools uses is setting some flags incorrectly which is causing a console to fail to allocate and thus causing this issue. The PowerShell team had a similar problem last year: Calling CreateFile() API returns ERROR_INVALID_HANDLE in .NET 5-preview.8 · Issue #41477 · dotnet/runtime · GitHub

I’m currently not reproducing this with .NET 6.0 previews but will check some .NET 5.0 releases to see if the problem is present there. I have received a binary from @andypprobert and running it on my machine exhibits the issue so it’s definitely about the build process and not the machine it is run on.

Some possible workaround would be to attempt a different .NET SDK. You would need uninstall the latest and try an older version (3.1) or a newer one (6.0 preview).

I think we should be able to resolve this to adjust the flags used within the PoshTools build process but I want to be careful as that can introduce other issues. I’ll continue to update this thread as progress is made or a mitigate is implemented in PoshTools.

Confirming that using .Net 6 preview works around the issue.

1 Like