Setting up IIS in PowerShell
I work on multiple branches of the same code base, which is simple enough to handle, except for handling websites in IIS. I could run separate copies of the code on different ports, but that makes me have to remember what port corresponds to what branch, and the procedure to re-root IIS is simple enough -it’s just tedious. A bit of searching revealed that Power Shell is capable of modifying the IIS metabase via ADSI or WMI. The only difficult thing is getting the new config to stick – for some odd reason, I have to make the change twice to make it stick. Anyway, here’s the code that I use to change the path for an IIS VDir – in principle, it’d be easy to extend this to do things like create an application or set security on the site.
function moveIISRoot([string] $site_name, [string]$topath)
{
# look for a site with display name = $site_name
write-host "Re-rooting $site_name at $topath"
$siteroot = get-wmiobject IIsWebServerSetting -namespace "root\MicrosoftIISv2" | Where-Object {$_.ServerComment -eq $site_name} | Select-Object Name
$objIIS = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/" + $siteroot.Name + "/Root")
# I don’t know why, but you have to do this twice to get it to stick $objIIS.Put(“Path”,$topath) $objIIS.PSBase.CommitChanges() #write-host “Done once: “, $objIIS.Path
$objIIS.Put(“Path”,$topath)
$objIIS.PSBase.CommitChanges()
#write-host “Done twice “, $objIIS.Path
}
— Gordon Weakliem
Comment
Commenting is closed for this article.
On Automatic Coding Standards If You're Lost, Don't Take Me With You