3

Exceptions when running a single exe file using PowerShell.

  • Computer: Windows 10 (latest patches)
  • PowerShell SDK: 7.2.4
  • Application: Console net6.0

Code:

using System.Management.Automation;

Console.WriteLine("Test PowerShell Runner!");

var psInstance = PowerShell.Create();

psInstance.AddScript("(Get-Host).Name");

Console.WriteLine("Before Invoke.");
var returnObj = psInstance.Invoke();
Console.WriteLine("After Invoke.");

Console.WriteLine("Name: " + returnObj.First().BaseObject);

psInstance.Runspace?.Close();

Console.WriteLine("Was successful run");

Proj file:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
      <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.4" />
    </ItemGroup>
</Project>

Publish profile (Rider): [Rider's profile settings - can be duplicated in VS][1] [1]: https://i.stack.imgur.com/dZFc0.png

We can not have IncludeAllContentForSelfExtract (which will run successfully) due to security reasons, we want to keep all our code hidden as much as possible.

Also noting that this will run successfully in debug/run via your IDE, it is only the published version that fails.

We have tried including the powershell dll's in the published exe's path, but no impact.

Error:

C:\Projects\PSConsoleRunner\PSConsoleRunner\bin\Release\net6.0\publish>PSConsoleRunner.exe
Test PowerShell Runner!
Before Invoke.
Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Management.Automation.ExperimentalFeature' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Management.Automation.Configuration.PowerShellConfig' threw an exception.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.IO.Path.Combine(String path1, String path2)
   at System.Management.Automation.Configuration.PowerShellConfig..ctor()
   at System.Management.Automation.Configuration.PowerShellConfig..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExperimentalFeature..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.InitialSessionState.AddVariables(IEnumerable`1 variables)
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at Program.<Main>$(String[] args) in C:\Projects\PSConsoleRunner\PSConsoleRunner\Program.cs:line 10
JMcCoard
  • 31
  • 1
  • Here is the VS project/solution: https://github.com/SCLD-JMcCoard/PSSDK-SIngleExe-Runner – JMcCoard Jun 14 '22 at 15:17
  • I just hit this. Did you ever find an answer? – Ralph Trickey Jun 25 '22 at 00:03
  • @Ralph Trickey No. we went with a terrible work around for localhost scripts. We are using a process to call powershell.exe. :( Basically not using the SDK. The SDK does work with WMI running scripts on remote hosts. – JMcCoard Jun 27 '22 at 12:55

2 Answers2

1

the error output seems to be that AddScript method is expecting a path, so my guess is this line:

psInstance.AddScript("(Get-Host).Name");

should probably be:

psInstance.AddCommand("(Get-Host).Name");

examples from docs here:

https://learn.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7.2#addcommand

ErikW
  • 386
  • 2
  • 11
  • Thank you for your help. Unfortunately this produces the same error exception as original post. I did validate that it runs fine through debug using the suggested AddCommand, just when we publish into a single file executable it fails. – JMcCoard Jun 13 '22 at 17:06
  • there might be something here that helps: https://stackoverflow.com/questions/70643435/powershell-package-missing-not-working-on-release – ErikW Jun 13 '22 at 17:33
  • Merely invoking this call: using (var runspace = RunspaceFactory.CreateRunspace()) in a super stripped down app cause the exception to throw. – JohnB Jun 13 '22 at 17:36
0

PowerShell currently doesn't support publishing as single-file. More about this here: #13540 and workaround here: #13540 (comment)

Also, you need to provide non-portable RIDs if you want a self-contained app: #18225

ALIENQuake
  • 520
  • 3
  • 12
  • 28