【问题标题】:Ps WebAdministration called from MVC only returns info about the current website从 MVC 调用的 Ps WebAdministration 仅返回有关当前网站的信息
【发布时间】:2016-02-02 16:59:10
【问题描述】:

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

【问题讨论】:

  • 如果 PowerShell 可以做到,C# 也可以,为什么还要绕圈子:List all websites in IIS c#
  • 谢谢,但是 .. 在下一步中,我想在多台服务器上远程运行 PScript :) 我正在开发整个服务器场的实时域列表 .. 可在 1 个方便的 Intranet 上使用页。使用 PS 似乎最轻量级,但我遇到了这个问题
  • 您的所有服务器都在同一个 Active Directory 域中吗?
  • 嘿,不,他们不在办公室域中,他们需要从外部访问..
  • 假设您设置了 WinRM/PowerShell 远程处理位以远程运行这些 powershell 位?

标签: c# asp.net asp.net-mvc powershell web-administration


【解决方案1】:

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

直接从浏览器调用代码

测试:

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

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

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 远程处理来添加服务器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多