我试图在远程服务器上重新启动iis应用程序池。首先,我想停止应用程序池,但我得到了
无法转换" {Stop-WebAppPool -Name" BaymentPool" }"的价值 type" System.String"输入 " System.Management.Automation.ScriptBlock"
例外
public void StopAppPool()
{
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
Command invokeCmd = new Command("Invoke-Command");
invokeCmd.Parameters.Add("ComputerName","IISC02");
invokeCmd.Parameters.Add("ScriptBlock","{ Stop-WebAppPool -Name \"BaymentPool\" }");
pipeline.Commands.Add(invokeCmd);
Collection<PSObject> output = pipeline.Invoke();
foreach (PSObject psObject in output)
{
Process process = (Process)psObject.BaseObject;
Console.WriteLine("Process name: " + process.ProcessName);
}
}
我无法理解我做错了什么。我知道我需要单独添加每个参数,我认为我正在做,但它仍然抱怨。
答案 0 :(得分:3)
scriptblock参数应为ScriptBlock类型
invokeCmd.Parameters.Add("ScriptBlock", ScriptBlock.Create("{ Stop-WebAppPool -Name \"BaymentPool\" }"));
检查结果的代码不正确,转换为Process对象的代码将不起作用。看看这里有关如何检查调用结果的示例: http://blogs.msdn.com/b/kebab/archive/2014/04/28/executing-powershell-scripts-from-c.aspx
答案 1 :(得分:2)
一个简单的解决方案可能是:
var serverManager = ServerManager.OpenRemote("000.000.000.000"); // Ip Address of Remote server
var appPool = serverManager.ApplicationPools["MyAppPool"];
if (appPool == null) return;
if (appPool.State == ObjectState.Stopped)
{
appPool.Start();
}
else
{
appPool.Recycle();
}