WMI / PowerShell error: why does CreateSite create * two * sites all of a sudden?

I have a simple PowerShell script that uses WMI to create a website in a Vista box. Yes, I know PowerShell has an IIS provider to work with IIS 7, but this script has to support IIS 6.0 as well, so the rules that go out.

Anyway, the script worked fine, but all of a sudden (and I mean literally I made zero code changes to the script), it started creating a second, broken site for every call to the CreateNewSite Method. Below is the script. Does anyone have any idea?

$path = "C:\My Path\WebSite"
$site = "TestSite"
$hostHeader = "demo.blah.com"

$service = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsWebService"

$bindingClass = [wmiclass]'root\MicrosoftIISv2:ServerBinding'
$bindings = $bindingClass.CreateInstance()
$bindings.IP = ""
$bindings.Port = "80"
$bindings.Hostname = $hostHeader

$result = $service.CreateNewSite($site, $bindings, $path)

      

The above script just created a site called "TestSite", but now it also created a site called "SITE_1786339847" (the number changes, but it always looks like this). I went through the script executing one line at a time and no site is created until the CreateNewSite method is called. Is WMI a bug?

+1


source to share


1 answer


Oops, answered my question. I checked the original IIS 7.0 config file and found a lost virtual directory associated with site ID 1786339847. When I removed this virtual directory from the config file, the script started working correctly again.



In case anyone comes across something similar, first remove the site id for the bad site from IIS Manager before deleting it, then open C: \ Windows \ system32 \ inetsrv \ config \ applicationHost.config. Scan the file for that ID and find any orphaned references to it. Make sure you have a backup.

+1


source







All Articles