我正在尝试在 C# 中使用 powershell 重新启动 VM。
首先,我尝试运行 GET-VM 命令。它在以下行给出了一个例外:
PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("System.Management.Automation", out snapEx);
在下面给定的代码中。有人可以告诉我我在哪里做错了。
异常消息:没有为 Windows PowerShell 版本 2 注册任何管理单元
我的代码:
Command command = new Command("Get-VM");
command.Parameters.Add("Name", "PIE01010299");
RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
PSSnapInException snapEx = null;
PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("System.Management.Automation", out snapEx);
Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
pipeline.Commands.Add(command);
Collection<PSObject> output = pipeline.Invoke();
runSpace.Close();
foreach (PSObject psObject in output)
{
Console.WriteLine(psObject.ToString());
}