1

我想要一些简单的东西:列出所有网站(现在只在我自己的 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;
}
4

1 回答 1

0

有趣的!Get-WebBinding 不返回列表中的最后一个站点,而只返回 c# 代码运行的站点的名称!!我使用 [Route("getsites")] 直接从浏览器调用代码

考试:

Import-Module WebAdministration 
Get-WebBinding | Out-File "e:\temp\out-file.txt"

使用浏览器调用它(使用上面的 mvc-snippet)

http://localwebsiteName.nl/getsites

输出:

protocol                                 bindingInformation                     
--------                                 ------------------                     
http                                     *:80:localwebsiteName.nl   

我仍然不明白为什么,因为 IIS 应用程序池在我拥有所有管理员权限的个人域帐户下运行.. 有人知道吗?

Final update: This behaviour appears a limitation in WebAdminstration package. 
the same Ps-script returns ALL websites (as expected) when executed remotely.
Call seq:
browser > local MVC > pShell > remote pShell > local MVC > browser

我为什么要打扰?因为现在我可以通过添加服务器名称并启用 pShell 远程处理来添加服务器

于 2016-02-03T16:59:55.623 回答