Skip to content

Getting ArgumentNullException exception on calling System.Management.Automation.PowerShell.Create() when publishing a single file exe #24070

@kawaiifoxx

Description

@kawaiifoxx

Prerequisites

Steps to reproduce

  1. Create a dotnet console app.
  2. Add nuget package System.Management.Automation, Microsoft.Powershell.SDK
  3. Write below code in Program.cs of the console application
var shell = PowerShell.Create()
            .AddCommand("Set-ExecutionPolicy")
            .AddParameter("Scope", "Process")
            .AddParameter("ExecutionPolicy", "Unrestricted")
            .AddScript(
                $"Write-Host 'Hello World!'");

        var results = await shell.InvokeAsync();
  1. Run dotnet publish -c Release -o .\bin\Publish --self-contained true --runtime win-x64 /p:PublishSingleFile=true
  2. then `cd .\bin\Publish
  3. run the exe file.
  4. You'll get below error:
 System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
         at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
         at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)
         at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
      --- End of stack trace from previous location ---

Expected behavior

should not throw exception.

Actual behavior

Throwing System.ArgumentNullException exception

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

Labels

In-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the product

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions