Skip to content


Change IIS 6.0 Home Directory

The following script can be used to change all the websites on a server or group of servers to the same webroot.
#==============================================================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009
#
# NAME: Set-Webroot.ps1
#
# AUTHOR: Jason Rydstrand
# DATE  : 5/15/2009
# VERSION: 1.1
#
# COMMENT: This script is for changing the Webroot on systems.
#
# REQUIREMENTS: 1. Powershell V2
#    2. RPC Access from system script is running on.
#
# CHANGED: 1. Created functions for each step.
#     2. Added comments.
#     3. Added Powershell version checking.
#     4. Added UserValidation before Changes are made.
#     5. Added capability for more then one sytsem to change at a time.
#
# TODO: 1. Pull system from VIP (Not implimented in this version.)
#  2. Wait for system connections to drop. (Not implimented in this version.)
#  4. Do a HTTP Get to spin up IIS. (Not implimented in this version.)
#  5. Put back in VIP. (Not implimented in this version.)
#
#==============================================================================================

# Functions
#==============================================================================================

# Function to validate version of Powershell is V2

function CheckVersion
{
 Write-Host "Checking for Powershell V2"
 
 if($host.version.Major -ne "2")
 {
  Write-Host "This script requires Powershell Version 2."
  Write-Host "Please download and install Powershell V2 from"
  Write-Host "<a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb1-a958-ee6d7fe307bc&displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb1-a958-ee6d7fe307bc&displaylang=en</a>"
  break
 }
 else
 {
  Write-Host "Powershell V2 Detected."
  Write-Host " "
 }
}

# Function to build array of machines for processing.

function InputMachines
{
 Write-Host "Please input machines. Example: system1 system2 system3 "
 $machines = (Read-Host "Machines").Split(" ")
 Write-Host " "
 return $machines
}

# Function to get new Home Directory

function GetDirectory
{
 Write-Host "Please input new IIS Home Directory. Example: E:\webroot_a"
 $webroot = Read-Host "Directory"
 Write-Host " "
 return $webroot
}

function UserValidation
{
 Write-Host "The following systems will be changed:"
 Write-Host " "
 foreach($machine in $machines)
 {
  Write-Host "$machine - $webroot"
 }
 Write-Host " "
 Write-Host "Are these correct?"
 $yesno = Read-Host "Yes/No?"
 if($yesno -eq "Yes")
 {
  Write-Host "Continueing."
 }
 else
 {
  Write-Host "&lt;&lt;&lt; User did not indicate these are correct &gt;&gt;&gt;"
  break
 }
}

function ChangeVirDir
{
 foreach($machine in $machines)
 {
  $iis = Get-WmiObject -class iiswebvirtualdirsetting -namespace "root\microsoftiisv2" -computer $machine -authentication 6 -Credential $credentials
 
  # Count number of websites.
  $i = $iis.count

  # Set array start
  $j = 0

  # Loop through each website.
  while($i -gt 0)
  {
   # Set path for local object of website
   $iis[$j].Path = "$($webroot)"

   # Put local object on remote host
   $iis[$j].Put()
 
   # Increment Counters
   $j++
   $i--
  }
 }
}
#==============================================================================================
# End Functions

# Main Script Body Start
#==============================================================================================

# Check Powershell Version

CheckVersion

# Notify User what script is for.
Write-Host "This script is to switch a machine, or group of machines, IIS Home Directory."
Write-Host "The following will be done by this script:"
Write-Host "1. Pull system from VIP (Not implimented in this version.)"
Write-Host "2. Wait for system connections to drop. (Not implimented in this version.)"
Write-Host "3. Change IIS Home Directory."
Write-Host "4. Do a HTTP Get to spin up IIS. (Not implimented in this version.)"
Write-Host "5. Put back in VIP. (Not implimented in this version.)"
Write-Host " "
Write-Host "Asking for user credentials."
$credentials = Get-Credential
Write-Host " "
# Set what machine to hit
$machines = InputMachines

# Set path you want Home Directory to be.
$webroot = GetDirectory

# Inform user of what systems will be changed to what directory.

UserValidation

# Make change

ChangeVirDir

#==============================================================================================
# End of Main Script Body

Posted in Powershell, V2.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.