C# InitialSessionState ImportPSSnapin deprecated - Alternative?

Viewed 124

I'm using the InitialSessionState class to prepare the Powershell Runspace and preload some modules.

        iss = InitialSessionState.CreateDefault();
        iss.ImportPSModule(new[] { "SomeModules" });
        //iss.ImportPSSnapIn("Microsoft.Exchange.Management.PowerShell.SnapIn", out psEx);
        iss.ThrowOnRunspaceOpenError = true;
        rs = RunspaceFactory.CreateRunspace(iss);
        rs.Open();

Unfortunately, it's not possible to preload a Powershell Snapin (like Exchange Management Shell) because the method has been marked as deprecated: https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate.importpssnapin?view=powershellsdk-7.0.0

Is there any alternative for this method? Loading the Snapin as a module does not work.

1 Answers

As already stated, the Add-PSSnapin has been declared as deprecated. Therefore, it's not possible to use it within the InitialSesstionState object anymore. But you can use an own Powershell instance and preload the corresponding Snapin: .NET Core 5.0 Runspace.Open() doesn't work

Related