出现异常:运行简单PowerShell命令时出现CLR/System.Management.Automation.Run



我正在尝试运行一个示例C#程序,该程序应该调用一个简单的PowerShell命令。不幸的是,我收到以下错误消息:

Exception has occurred: CLR/System.Management.Automation.Runspaces.PSSnapInException
An unhandled exception of type 'System.Management.Automation.Runspaces.PSSnapInException' occurred in System.Management.Automation.dll: 'System error.'
   at System.Management.Automation.Runspaces.PSSnapInHelpers.LoadPSSnapInAssembly(PSSnapInInfo psSnapInInfo)
   at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at ttps3.Program.Main(String[] args) in Z:projectseasybellttps3Program.cs:line 21

我在C#SDK 5和System.Management.Automation 7.1.3 上运行

System.Management.Automation仅在非常有限的场景中可用。您可以安装Microsoft.PowerShell.SDK nuget软件包。

Microsoft.PowerShell.SDK

Microsoft.PowerShell.SDK是一个元包,它将PowerShell SDK的所有组件合并到一个NuGet包中。独立的.NET应用程序可以使用Microsoft.PowerShell.SDK运行任意PowerShell功能,而不依赖于任何外部PowerShell安装或库。

系统管理自动化

System.Management.Automation包是PowerShell SDK的核心,在NuGet上主要作为Microsoft.PowerShell.SDK的资产存在。但是,它也可以直接用作较小托管场景和版本目标模块的包。

具体而言,当出现以下情况时,System.Management.Automation包可能是PowerShell功能的首选提供商:

  • 您只希望使用PowerShell语言功能(在System.Management.Automation.Language命名空间中(,如PowerShell解析器、AST和AST访问者API(例如用于PowerShell的静态分析(
  • 您只希望执行Microsoft.PowerShell.Core模块中的特定命令,并且可以在使用CreateDefault2工厂方法创建的会话状态下执行这些命令

PowerShellStandard.Library和其他PowerShell引用程序集

这些引用程序集不包括运行时,并且需要与引用程序集版本匹配的全局powershell安装。

相关内容

最新更新