Home

LINQPad7 unable import Powershell Hyper-V module to execute script

Let's say this code:

string _scr = ("Get-VM");
var _ps = PowerShell.Create();
_ps.AddScript(_scr);
Collection<PSObject> _cObj = _ps.Invoke();

if (_ps.HadErrors)
{
    Console.WriteLine(_ps.Streams.Error[0].ToString());
}

foreach (PSObject _vm in _cObj)
{
    Console.WriteLine(_vm);
}

Is able to run in Visual Studio 2022 .NET Core 6.0 project (Install NuGet: Microsoft.PowerShell.SDK):

Also able to run in LINQPad 5(By reference another assembly(install NuGet: Microsoft.PowerShell.5.1.ReferenceAssemblies):

But confirmed unable to run in my latest LINQPad 7.4.2:(Install NuGet: Microsoft.PowerShell.SDK)

After investigation, this seems related to Import-Module, if we don't import any module, code will execute perfectly fine in LINQPad 7:

But code still unable to run in LINQPad 7 even if we write Import-Module Hyper-V.

Comments

Sign In or Register to comment.