Ps WebAdministration 仅从 MVC 调用 returns 关于当前网站的信息

Ps WebAdministration called from MVC only returns info about the current website

我想要一些简单的东西:列出所有网站(现在只在我自己的 IIS7 上,但稍后会在服务器场上)。 我想使用 powershell-4 命令,例如 Get-WebBinding 或 Get-Website,因为 pShell 很容易在其他服务器上远程执行。

我想从 Intranet 网页触发 pShell,以显示服务器场每个主机的实时域绑定概览。


脚本在 Powershell window 本身中工作正常,从 C# -WIN 形式调用它也可以工作,但从网页 (MVC5) 调用它 returns 仅在托管页面的站点上,而不是所有网站.. 发生了什么事??

我使用 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;
}

有意思! Get-WebBinding 没有 return 列表中的最后一个站点,只有运行 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 远程处理来添加服务器