Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Are you simply looking for a list of their names or do you expect some sort of site object to be passed back to be acted upon? –  pk. Apr 10 '12 at 18:46
    
Not just listing the sites, but be able to iterate through each website and add new HttpCustomHeaders to it in IIS6.0. –  user117048 Apr 11 '12 at 17:48

2 Answers 2

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()
}
share|improve this answer

This post: http://stackoverflow.com/questions/3575442/getting-all-virtual-directories-for-a-iis6-web-site-using-wmi

has this solution:

Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2" | `
     Where-Object { $_.name -like "W3SVC/1/*" }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.