IIS 7.0 引入 PowerShell 管理模組,提供 Web-Sites、Apps、Virtual Directories、Application Pools 等命名空間。內建的 Cmdlet 讓建立與管理這些項目變得直觀易懂。
在 PowerShell 中,New-Item 常用於建立新項目。例如在檔案系統中 New-Item c:\TestDirectory 會建立資料夾。在 IIS 命名空間下,同樣使用 New-Item 建立網站,但需額外指定 -bindings 參數。
PS IIS:\Sites> New-Item iis:\Sites\TestSite -bindings @{protocol="http";bindingInformation=":80:TestSite"} -physicalPath c:\test
PS IIS:\Sites> dir
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Default Web Site 1 Started f:\inetpub\wwwroot http *:80:
TestSite 2 Started c:\test http :80:TestSite
-bindings 使用 Hash Table(雜湊表)表示,鍵值對對應 IIS 設定檔中 <binding> 元素的屬性:
<bindings> <binding protocol="http" bindingInformation=":80:TestSite" /> </bindings>
Hash Table 提供彈性,未來若 <binding> 擴充新屬性,同樣可透過鍵值對支援。
同樣使用 New-Item,路徑分別為 iis:\Sites\SiteName\AppName、iis:\Sites\SiteName\VirtualDir、iis:\AppPools\PoolName。參數如 -physicalPath、-applicationPool 等視情況指定。
PowerShell 讓 IIS 7.0 的自動化部署與管理更具腳本化優勢,適合 CI/CD 流程與批次作業。