![IIS 6.0 Windows Server 2003 - 如何使用 powershell 遍历所有网站?](https://linux22.com/image/580981/IIS%206.0%20Windows%20Server%202003%20-%20%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20powershell%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E7%BD%91%E7%AB%99%EF%BC%9F.png)
如何使用 powershell 遍历托管在 IIS 6.0(Windows Server 2003)上的所有网站?
有没有办法使用 WmiObject 来做到这一点?
谢谢!
答案1
使用 ADSI 或 WMI 列出它们,如下所示 -
#ADSI
$iis = [ADSI]"IIS://localhost/W3SVC"
$iis.psbase.children | where { $_.schemaClassName -eq "IIsWebServer"} | select ServerComment
#WMI
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment
要添加您在评论中提到的新 HttpCustomHeader,我认为您可以使用以下技术。请彻底测试,因为我没有。
$wmiSiteArray = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2"
foreach ($site in $wmiSiteArray)
{
$path = $site.name + '/root'
$vdir = [wmi]"root\MicrosoftIISv2:IIsWebVirtualDirSetting='$path'"
$bindingClass= [wmiclass]'root\MicrosoftIISv2:HttpCustomHeader'
$newHeader = $bindingClass.CreateInstance()
$newHeader.KeyName = "CustomHeader: BlahBlah"
$newHeader.value = $null
$vdir.HttpCustomHeaders += $newHeader.PSObject.BaseObject
$vdir.Put()
}
答案2
有这样的解决方案:
Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2" | `
Where-Object { $_.name -like "W3SVC/1/*" }