【问题标题】:PowerShell + WebAdministration - How to get website from webapplication?PowerShell + WebAdministration - 如何从 Web 应用程序获取网站?
【发布时间】:2011-09-21 17:29:26
【问题描述】:

我正在编写一个 PowerShell 脚本来执行 IIS 7.5 中的某些管理功能。

import-module WebAdministration

在某些情况下,我知道要使用的 Web 应用程序的名称,但不知道它所在的网站。获取应用程序很容易:

$app = get-webapplication -name 'MyApp'

但我不知道如何获取给定应用程序的网站名称。它似乎不是 webapplication 对象的属性。我能想到的最好办法是尝试通过测试路径获取它:

get-website | where {test-path "iis:\sites\$_.name\MyApp"}

出于某种原因,它出现了空。关于如何解决这个问题的任何想法?提前致谢。

【问题讨论】:

    标签: iis powershell iis-7.5


    【解决方案1】:

    这是获取站点名称的方法:

    $siteName = (Get-WebApplication -name 'YourApp').GetParentElement().Attributes['name'].Value
    

    甚至更短:

    $siteName = (Get-WebApplication -name 'YourApp').GetParentElement()['name']
    

    【讨论】:

    • 这在 Powershell 7 + WindowsCompatibility 中不起作用:InvalidOperation:方法调用失败,因为 [Deserialized.Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application] 不包含名为“GetParentElement”的方法.
    【解决方案2】:

    我还没有深入研究原因,但是如果您在字符串中的任何位置使用星号或问号,它就像通配符一样工作并返回。

    I.E.

    get-website -name '*myapp'
    
       Name      ID   State    Physical Path        Bindings
       ----      --   -----    -------------        --------
    
       myapp     12   Started  C:\inetpub\wwwroot   http:*:80:
      amyapp     13   Stopped  C:\anetpub\          http:*:81:
     aamyapp     14   Stopped  C:\another\place     http:172.198.1.2:80:host.header.com
    

    get-website -name '?myapp'
    
    Name   ID State   Physical Path Bindings
    ----   -- -----   ------------- --------
    amyapp 13 Stopped C:\anetpub    http:*:81:
    

    【讨论】:

      【解决方案3】:

      试试这个

      $app = get-webapplication -name 'MyApp'
      foreach($a in $app)
      {
      $a.Attributes[0].Value;
      }
      

      这将为您提供 Web 应用程序的所有名称,有时您可以为单个 Web 应用程序拥有多个名称。

      更多信息见链接

      http://nisanthkv.blog.com/2012/07/06/name-of-web-applications-using-powershell/

      希望对你有帮助..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 2010-11-04
        相关资源
        最近更新 更多