如何在C#中将Microsoft.SystemCenter.VirtualMachineManager添加到PowerShell SnapIn [英] How to add Microsoft.SystemCenter.VirtualMachineManager to PowerShell SnapIn in C#
本文介绍了如何在C#中将Microsoft.SystemCenter.VirtualMachineManager添加到PowerShell SnapIn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我不确定是否应该在此论坛中发布.如果没有,请移动它,但是找不到适合我问题的地方.
无论如何,我有一个PowerShell脚本,它使用一些提供的参数在Hyper-V上创建了虚拟机.不过,我有一些问题.实际上,当我将脚本加载到文件中时,我可以使其运行无误,然后执行以下操作:
Hi folks,
I'm not sure wether I should've posted in this forum. If not, please move it, but I didn't found any place that suits my question.
Anyway, I've a PowerShell script that creates a Virtual Machine on Hyper-V using some provided arguments. I've some problems, though. In fact, I can put the script to run with no errors when I load it into a file, then do:
public static string RunScript(string scriptText)
{
try
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
推荐答案
任何人?
这篇关于如何在C#中将Microsoft.SystemCenter.VirtualMachineManager添加到PowerShell SnapIn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文