Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`PowerShell.Create()` returning null

Added reference: PowerShellStandard.Library

Repro inside a default .net-core project:

// ...

using System.Management.Automation;
using System.Collections.ObjectModel;

// ...

public static void Main(string[] args)
{
    Collection<PSObject> output;
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddScript("$test = Get-Date; $test");
        output = ps.Invoke();
    }

// ...

I've tried it with or without the using block, but I end up with the same result: the Create method is not creating a PowerShell object, but it's also not throwing an exception.

Is this a common issue with the PowerShell .net-standard library? Is there a workaround or another way to solve my problem?

Additional info, this is also happening with the RunspaceFactory class CreateRunspace method as I was exploring a workaround with managing runspaces myself.

like image 607
Maximilian Burszley Avatar asked Aug 07 '18 18:08

Maximilian Burszley


People also ask

What is $null in PowerShell?

PowerShell $Null is an automatic variable that holds nothing but unidentified or absent value and it is also considered as an Object.

How to check if a variable is empty in PowerShell?

NULL is used to represent empty or undefined. Variable hold null until you assign any value to it. In this example, we will discuss PowerShell $null, check if a variable is empty, or if not null. When you type $null on the console, it returns empty.

How do I redirect pipeline data to $null in PowerShell?

The Out-Null command is the built-in way to redirect pipeline data to $null. You can assign the results of a command to $null for the same effect as using Out-Null.

How to spot if the value is null in a script?

Many times when writing a script we need to spot if the value is Null but it doesn’t visible in the console. To spot if the variable is going to hold a null value, we need to use the special characters or something that we can identify that the variable holds a null value.


1 Answers

PowerShellStandard is a reference library, it's intended for projects that will be loaded into the same AppDomain as PowerShell. In those scenarios the required assemblies are already loaded, so it would be a waste to pull down the entire SDK.

In order to host PowerShell (or otherwise use PowerShell API's from outside a PowerShell session) you need to reference the PowerShell SDK (for PowerShell Core) or the GAC assemblies (for Windows PowerShell).

To reference the SDK, you need a nuget.config file in the base directory of your project that adds the PowerShell myget as a source. Here's an example

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="dotnet-core" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="powershell-core" value="https://powershell.myget.org/F/powershell-core/api/v3/index.json" />
  </packageSources>
</configuration>

And add a reference to the SDK in your csproj

<ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.3" />
</ItemGroup>

Update 04/15/2021

You don't need the nuget config anymore, the SDK is now available on nuget.

like image 69
Patrick Meinecke Avatar answered Nov 04 '22 12:11

Patrick Meinecke