March 22, 2012

Move SharePoint IIS Folder to a new location

Have you ever created a SharePoint 2010 (or 2007) environment, then realized that the IIS Folder was the default one (i.e. C:\inetpub\wwwroot\wss\VirtualDirectories). But guess what?!!! you wanted it to be in another directory (i.e. D:\Vroots\wss\VirtualDirectories) because Server Support wants you to store all application data on the D Drive. So what do you do?

Problem

You unintintionally created you SharePoint web applications under the default IIS physical path (i.e. C:\inetpub\wwwroot\wss\VirtualDirectories) when it needed to be something else.

Solution

No problem! I have created a fix for you, all you need to do is run this powershell script on your server, and it will move the directory and configure everything for you, all with one File.ps1 powershell file and a couple of input parameters.
# Created by Dennis Alzoubi    
# Version 1.0
# Last Updated 3/22/2012 10:01 am
# Purpose: This Powershell snippet will move SP2010 site's physical directory to a new location

try
{
    Add-PsSnapin Microsoft.SharePoint.PowerShell;
    $SiteUrl = Read-Host "Web Application Url"
    $SPSite = new-object Microsoft.SharePoint.SPSite($SiteUrl)
    $WebApp = $SPSite.WebApplication
    #Change SPUrlZone if not in default zone
    $IISSettings = $WebApp.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
    #Point to your new directory
    $NewPath = Read-Host "New VRoots Directory"
    $OldPath = $IISSettings.Path

    Write-Host "Copy all the current VRoots Folder to " $NewPath
    xcopy $IISSettings.Path $NewPath /O /X /E /H /K

    $IISSettings.Path = $NewPath
    #Update Web Application
    $WebApp.Update()
    $SPSite.Dispose()
}
catch
{
    "Error while updating SharePoint web application IIS Settings "; $error[0]
    return;
}

try
{
    [Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
    $serverManager = New-Object Microsoft.Web.Administration.ServerManager
    $site = $serverManager.Sites | where { $_.Name -eq $IISSettings.ServerComment }
    $rootApp = $site.Applications | where { $_.Path -eq "/" }
    $rootVdir = $rootApp.VirtualDirectories | where { $_.Path -eq "/" }
    $rootVdir.PhysicalPath = $NewPath
    $serverManager.CommitChanges()
}
catch
{
    "Error while updating IIS Settings "; $error[0]
    return;
}

Write-Host "Remove " $OldPath
RD $OldPath

Write-Host "Reset IIS"
IISRESET /noforce

Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")


Note


You may want to consider changing the default IIS directory to the new directory, this way you will save yourself the trouble with new web applications, script for you:



Happy Coding!

No comments:

Post a Comment