2016-03-22 56 views
0

我想列出(在Powershell中)所有的網站從IIS的名稱包含關鍵字「英國」。然後,我想用另一個替換返回的網站的物理路徑,例如「C:\ Sites \ Sitex」。Powershell在IIS中的所有網站的列表與一個條件,並更改像物理路徑的屬性

我在這裏做錯了什麼?

到目前爲止,我試過這個: get-website | select name,physicalpath | where { $_.name -like "*UK*" } 它可以正常顯示我的IIS中包含關鍵字「UK」的名稱的所有網站。 但是,當我這樣做: get-website | select physicalpath | where { $_.name -like "*UK*" } 它什麼也沒有顯示!

我想這可能是因爲我不能在我的「where」子句中選擇一個不存在的字段! (在SQL中,這可以工作)。所以,我會嘗試將最初的查詢包裝到一個表中,然後只從該表中選擇一列。 Powershell中是否存在嵌套查詢?請幫助語法。 也歡迎其他解決方案。

然後,我會做這樣的事情

的foreach($在$網站網站) {

的Set-ItemProperty ???? -name physicalpath - 值 「C:\網站\ SITEX」

}

與語法的任何幫助,請與哪些參數應該設置-ItemProperty後寫。 非常感謝提前!

================= 也試圖這樣的並行:

Import-Module WebAdministration 

dir IIS:\Sites # lists all sites 

dir IIS:\Sites | ForEach-Object { 

    # web site name $_Name 


    if($_.Name -like "*UK*") { 
    $_.Name 
    $_.PhysicalPath = "C:\Inetpub" 

    } 


} 

沒有錯誤,它似乎並沒有任何工作。 if($_.Name -like "*UK*")的行爲不如預期,因爲它會輸出其他名稱中不含英國的網站。順便提一下,我正在使用Powershell_ISE.exe。

回答

0

我沒有爲此答案導入Web管理模塊。

我使用iis manager的配置代碼生成功能來生成在foreach循環中使用的語句(稍後在回答中引用鏈接),我自定義的腳本的其餘部分。

$searchWildCard="*UK*" 
$targetDirectory="<INSERT_NEW_DIRECTORY_PATH_HERE>" 

#Select the entire site object since i might make other config changes too. 
$sites=get-website | Where-Object { $_.name -like $searchWildCard } 

foreach ($site in $sites) { 
$filter=[string]::Format("system.applicationHost/sites/site[@name='{0}']/application[@path='/']/virtualDirectory[@path='/']",$site.name) 

    # note the backticks below 
    Set-WebConfigurationProperty ` 
    -pspath 'MACHINE/WEBROOT/APPHOST' ` 
    -filter $filter ` 
    -name "physicalPath" ` 
    -value $targetDirectory 
} 

# I tested without performing an iisreset. 
# One definitely needs to refresh the 
#iis manager ui after running the script if it was open during execution. 

#iisreset 

腳本註釋:

腳本指出具體考慮的意見。

腳本將所有站點指向與您的foreach 示例相同的目錄。

測試於:

視窗10

Powershell的5

SO參考:

code generation using iis management ui

SO相關:

changing-the-physical-path-of-an-iis-website-on-a-remote-machine-via-powershell

+0

謝謝,艾瑪! – dear1

相關問題