Question:
Added reference: PowerShellStandard.Library
Repro inside a default .net-core
project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// ... using System.Management.Automation; using System.Collections.ObjectModel; // ... public static void Main(string[] args) { Collection 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.
Answer:
Looking through the source of the PowerShellStandard Library I noticed the following lines:
1 2 3 4 |
public static System.Management.Automation.PowerShell Create ( System.Management.Automation.Runspaces.InitialSessionState initialSessionState ) { return default(System.Management.Automation.PowerShell); } public static System.Management.Automation.PowerShell Create ( ) { return default(System.Management.Automation.PowerShell); } public static System.Management.Automation.PowerShell Create ( System.Management.Automation.RunspaceMode runspace ) { return default(System.Management.Automation.PowerShell); } |
Those will always return the default
of the sealed PowerShell class and that will always be null
.
This makes it that PowerShell isn’t (fully) supported in PowerShellStandard Library 5.1.
Same is for RunspaceFactory.CreateRunspace
.