PowerShell running local from published Single Exe fails (without IncludeAllContentForSelfExtract)

1k views Asked by At

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
2

There are 2 answers

3
ErikW On

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

0
ALIENQuake On

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