Powershell – IIS 6.0 Windows Server 2003 – how to iterate through all websites using powershell

iis-6powershell

How can I iterate through all the websites hosted on an IIS 6.0 (Windows Server 2003) using powershell?

Is there a way to do it using WmiObject ?

Thanks!

Best Answer

List them using ADSI or WMI as follows --

#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

To add a new HttpCustomHeader as you've mentioned in your comment, I think you can use the following technique. Please test thoroughly for I did not.

$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()
}