【问题标题】:C# InitialSessionState ImportPSSnapin deprecated - Alternative?不推荐使用 C# InitialSessionState ImportPSSnapin - 替代方案?
【发布时间】:2021-10-29 04:35:40
【问题描述】:

我正在使用 InitialSessionState 类来准备 Powershell 运行空间并预加载一些模块。

        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();

很遗憾,无法预加载 Powershell 管理单元(如 Exchange 命令行管理程序),因为该方法已被标记为已弃用:https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate.importpssnapin?view=powershellsdk-7.0.0

这种方法有什么替代方法吗?无法将 Snapin 作为模块加载。

【问题讨论】:

    标签: c# visual-studio powershell


    【解决方案1】:

    如前所述,Add-PSSnapin 已被声明为已弃用。因此,不能再在 InitialSessionState 对象中使用它。 但是您可以使用自己的 Powershell 实例并预加载相应的 Snapin:.NET Core 5.0 Runspace.Open() doesn't work

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 2021-10-23
      • 2021-03-28
      • 2012-03-03
      • 1970-01-01
      • 2022-01-20
      • 2022-09-25
      • 2015-10-11
      相关资源
      最近更新 更多