PowerShell + WebAdministration - 如何从webapplication获取网站?

Tod*_*ier 9 iis powershell iis-7.5

我正在编写PowerShell脚本以在IIS 7.5中执行某些管理功能.

import-module WebAdministration
Run Code Online (Sandbox Code Playgroud)

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

$app = get-webapplication -name 'MyApp'
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何获得给定应用程序的网站的名称.它似乎不是webapplication对象的属性.我想出的最好的尝试是通过测试路径获取它:

get-website | where {test-path "iis:\sites\$_.name\MyApp"}
Run Code Online (Sandbox Code Playgroud)

由于某些原因,空的.有关如何去做的任何想法?提前致谢.

mth*_*rba 18

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

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement().Attributes['name'].Value
Run Code Online (Sandbox Code Playgroud)

甚至更短:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement()['name']
Run Code Online (Sandbox Code Playgroud)


Twi*_*tem 6

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

IE

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
Run Code Online (Sandbox Code Playgroud)

或者

get-website -name '?myapp'

Name   ID State   Physical Path Bindings
----   -- -----   ------------- --------
amyapp 13 Stopped C:\anetpub    http:*:81:
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试这个

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

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

有关更多信息,请参阅链接

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

希望能帮助到你..