Update IIS virtual directory physical paths

Powershell

This is for cases where we have an IIS webdav server that has virtual directories corresponding to each user. If a particular site in the organization changes the file server where its user home folders reside, it becomes necessary to mass update any physical paths that refer to the old server.

Import-Module WebAdministration
$collectCampus = get-webvirtualdirectory | where {$_.Physicalpath -like "\\foo\users\*"}
foreach ($i in $collectCampus){
  $strPhysicalPath = [string] $i.Physicalpath
  $strPhysicalPath = $strPhysicalPath -replace "foo", "bar" #server name swap
  $strPhysicalPath = $strPhysicalPath -replace "users", "userdata" #share name swap (working around backslash)
  $strName = [string] $i.Path
  $strName = $strName -replace "/", "" #removing leading / from Path value
  $strIISPath = "IIS:\Sites\Default Web Site\" + $strName
  #SAMPLE: Set-ItemProperty 'IIS:\Sites\Default Web Site\DemoVirtualDirectory' -Name physicalPath -Value C:\inetpub\newDemoVirtualDirectory
  Set-ItemProperty $strIISPath -Name physicalPath -Value $strPhysicalPath
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s