Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Change the Home Directory of an IIS 6 website using Powershell 2 and WMI August 3, 2010

Filed under: Powershell,Systems Administration,Web,Windows — Rupert Bates @ 4:13 pm

This function will set the home directory of an IIS 6 website using Powershell 2 (it will not work with v1).

You need to pass it the site description (from the Web Site tab in IIS), the new path and the server your website is running on (this can be a machine name or an IP address).

You wouldn’t believe how long it took me to get this working.

Function SetHomeDirectory($SiteDescription, $NewPath, $serverName)
 $query = "ServerComment = '" + $SiteDescription + "'"
 $webserver = Get-WMIObject -class IIsWebServerSetting -namespace "root\microsoftiisv2" -Filter $query -computer $serverName -authentication 6
 $nameQuery = "Name = '" + $webserver.Name + "/root'"
 $webdir = Get-WMIObject -class IIsWebVirtualDirSetting -namespace "root\microsoftiisv2" -Filter $nameQuery -computer $serverName -authentication 6
 $webdir.Path = $NewPath
 Set-WmiInstance -InputObject $webdir
SetHomeDirectory "mysite.co.uk" "d:\websites\mysite" "myServer"

Update environment variables in Powershell May 20, 2010

Filed under: Powershell,Systems Administration — Rupert Bates @ 9:27 am

To update an environment variable in Powershell in a way that lasts beyond the current session you can use the SetEnvironmentVariable method:

[Environment]::SetEnvironmentVariable(“theVariableToSet”, “theNewValue, “User”)

The final parameter determines the scope of the variable and can be one of Process, User or Machine.

So to add to your path you can use the following:

$path = [environment]::GetEnvironmentVariable(“path”,”User”) + “C:\Program Files\Git\bin;”
[Environment]::SetEnvironmentVariable(“path”, $path, “User”)