我已经创建供内部使用,允许某些用户来启动和停止链接到有QA测试环境中虚拟机的asp.net web应用程序,网页背后的代码运行启动所选服务器一旦一个按钮是一个PowerShell脚本按在一个ASP.net页面上。在C#中运行powershell,没有多少运气!
我已经reserched从这个网站implimented代码的很多,但我来面对一些问题。
每次我点击主网页上的按钮从PowerShell脚本反馈的错误说“你不能叫一个空值表达式的方法。”唯一的问题是如果我从像这样的“。\ script \ test.ps1'W7EA9'”这样的PowerShell提示符运行它,它工作正常。
这是调用PowerShell脚本类。
public String Startserver(String Servername)
{
String scriptText =". \\scripts\\test.ps1 " + Servername + "";
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// execute the script
Collection<PSObject> results = new Collection<PSObject>();
try
{
results = pipeline.Invoke();
}
catch (Exception ex)
{
results.Add(new PSObject((object)ex.Message));
}
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
//return scriptText;
}
,这里是它试图运行
Param ($server = $arg[0])
$Core = get-wmiobject -namespace root\virtualization -class Msvm_Computersystem -filter "ElementName = '$server'"
$status = $Core.RequestStateChange(2) `
这可能是真正的财产以后明显的,但我只是没有看到它和任何帮助将是巨大的PowerShell脚本。
感谢
克里斯
后面的代码将尝试在服务器中运行powershell命令。这是你想要的? – Shoban 2011-02-28 15:20:46
您确定将正确的ServerName传递给该函数吗? – vissi 2011-02-28 15:25:52
嗨,我希望运行该网站上的服务器上的脚本,我已调试我的机器上的应用程序和服务器名称传递到该字符串之前它传递给PowerShell – ChrisLupson 2011-02-28 15:44:12