我有一个 Powershell 脚本,它存储在一个名为“script”的字符串中,内容如下:
get-user | out-file C:\Users\user\Desktop\user.txt -append
我的 C# 代码:
RunspaceConfiguration runConfig = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
runConfig.AddPSSnapIn("VMWare.View.Broker", out psEx);
Runspace runspace = RunspaceFactory.CreateRunspace(runConfig);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
Collection<PSObject> results = new Collection<PSObject>();
results = pipeline.Invoke();
runspace.Close();
如果我调试代码,我会得到以下异常:
No snap-ins have been registered for Windows Powershell Version 2
如果我手动运行脚本并添加管理单元,它工作正常