我想要一些简单的东西:列出所有网站(现在只在我自己的 IIS7 上,但稍后在 serverfarm 上)。我想使用 powershell-4 命令,例如 Get-WebBinding 或 Get-Website,因为 pShell 很容易在其他服务器上远程执行。
我想从 Intranet 网页触发 pShell,以显示每个服务器场主机的实时域绑定概述。
该脚本在 Powershell 窗口本身中工作正常,从 C# -WIN 形式调用它也可以工作,但从网页(MVC5)调用它只返回页面所在的站点,而不是所有站点.. 发生了什么?
我使用 URL“ http://localwebsite/getsites ”直接从浏览器调用它。
C#代码:
[ Route("getsites") ]
public string test(string machine)
{
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var pipeline = runspace.CreatePipeline();
//string script = @"Get-Item ""IIS:\sites\*""";
string script = @"Import-Module WebAdministration; Get-WebBinding";
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
var results = pipeline.Invoke();
runspace.Close();
var stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
//results has just 1 element
stringBuilder.AppendLine(obj.ToString());
}
var result = stringBuilder.ToString();
//result contains just the current site instead of all 10 sites
return result;
}