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.
PowerShell $Null is an automatic variable that holds nothing but unidentified or absent value and it is also considered as an Object.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With