2011-09-21 38 views
9

我正在寫一個PowerShell腳本來執行IIS 7.5中的某些管理功能。PowerShell + WebAdministration - 如何從Web應用程序獲取網站?

import-module WebAdministration 

在某些情況下,我知道了web應用程序我想,但不是網站,它是根據工作的名稱。獲取應用程序很容易:

$app = get-webapplication -name 'MyApp' 

但我無法弄清楚如何獲得給定的應用程序的網站的名稱。它似乎不是web應用程序對象的屬性。最好的我可以拿出來試圖通過測試路徑:

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

由於某種原因出現空。有關如何去做這件事的任何想法?提前致謝。

回答

13

這是你如何能得到網站名稱:

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

或者更短:

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

賓果。你是英雄。 –

3

我沒有挖成爲什麼,但如果你使用星號或問號任何地方在字符串中它像通配符一樣工作並返回。

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: 
相關問題