Showing posts with label IIS Application Pool. Show all posts
Showing posts with label IIS Application Pool. Show all posts

Saturday, July 26, 2014

Create virtual directories and application pools using PowerShell scripts

Purpose

This script will create an application pool if its does not exists and a virtual directory for the site using PowerShell.

Scenario

Suppose we have a website(in this case we have test.maulikdhorajia.com). And we have to create 100 virtual directories in the same website with same structure. But some Virtual Directories are already there and some needs to be created. Some of them uses framework 2.0 and some 4.0. Some of them are having 32 bit enabled and some of them don't need it. Using this script we can do it with saving a lot of time in doing manual process.

### Author : Maulik Dhorajia
### Purpose : This script will create an application pool if its doesnot exists and will create a virtual directory for the site using PowerShell.

# IMPORT WebAdministration Module
Import-Module WebAdministration

# Current Folder Declarations
$currentFolder = Split-Path -parent $MyInvocation.MyCommand.Definition
write-host "Folder name : " $currentFolder

# Create a virtual directory and application pool using PowerShell.
function CreateAppPoolAndVirtualDirectory( $iisAppPoolName, $iisAppPoolDotNetVersion , $iisAppName, $enable32Bit)
{
    # Set up the directory path
    $directoryPath = "$currentFolder\$iisAppName"
    Write-Host "Path: $directoryPath"
    
    # Validation
    if(($iisAppPoolName -eq $null) -or ($iisAppPoolName -eq ""))
    {
        Write-Host "AppPool name is required." -foregroundcolor "Red"
        return
    }
    if(($iisAppName -eq $null) -or ($iisAppName -eq ""))
    {
        Write-Host "Application name is required." -foregroundcolor "Red"
        return
    }
    if(($directoryPath -eq $null) -or ($directoryPath -eq ""))
    {
        Write-Host "Directory path is required." -foregroundcolor "Red"
        return
    }
    
    # Conditional Missing Records
    if(($iisAppPoolDotNetVersion -eq $null) -or ($iisAppPoolDotNetVersion -eq ""))
    {
        $iisAppPoolDotNetVersion = "v4.0"
    }
    
    # Navigate to the app pools root
    cd IIS:\AppPools\

    # Check if the app pool exists
    if (!(Test-Path $iisAppPoolName -pathType container))
    {
        #create the app pool
        $appPool = New-Item $iisAppPoolName
        # Assign App Pool Version
        $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
        # Enable 32-Bit "True" or "False"
        $appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -Value $enable32Bit
        Write-Host "$iisAppPoolName : Pool created successfully." -foregroundcolor "green"
    }

    #navigate to the sites root
    cd IIS:\Sites\
        
    # IIS Site
    $iissite = "iis:\sites\test.maulikdhorajia.com\$iisAppName"
    $subSite = Get-ChildItem "iis:\sites\test.maulikdhorajia.com\" | ? {$_.Name -eq $iisAppName -and $_.Type -eq "application"}
    
    if( $subSite -eq $null)
    {
        # Create the app
        $iisApp = New-Item $iissite -type Application -physicalPath $directoryPath
        Set-ItemProperty $iissite -Name "applicationPool" -Value $iisAppPoolName                
        Write-Host $iisAppName " : Application created successfully." -foregroundcolor "green"
    }
    else
    {
        Write-Host $iisAppName " : Application already exists!" -foregroundcolor "DarkMagenta"
    }
}

# CREATE VIRTUAL DIRECTORY
CreateAppPoolAndVirtualDirectory "Site1_AppPool" "" "Site1" "FALSE"
CreateAppPoolAndVirtualDirectory "Site2_AppPool" "v4.0" "Site2" "FALSE"
# USE FRAMEWORK 2.0
CreateAppPoolAndVirtualDirectory "Site3_AppPool" "v2.0" "Site3" "FALSE"
# ENABLE 32 BIT ON APP POOL
CreateAppPoolAndVirtualDirectory "Site4_AppPool" "v4.0" "Site4" "TRUE" 
# SAME APP POOL NAME
CreateAppPoolAndVirtualDirectory "Site1_AppPool" "v4.0" "Site5" "FALSE"

write-Host "End of script"

Thursday, December 22, 2011

PowerShell - Set-SPCustomLayoutsPage to change the custom error page.

Goal

SharePoint site contains many out of the box Page layouts doing some important tasks. The list is mentioned below. Our goal is to change the Error page and point it to our custom ones. There are many help available on internet for this task but they are missing one thing in most of the blogs. The most important task is to recycle to IIS Application Pool after running the Set-SPCustomLayoutsPage. In this blog I have tried and kept a whole working example for the new comers to change the path and recycle IIS Application Pool using PowerShell.

  • AccessDenied (/_layouts/AccessDenied.aspx)
  • Confirmation (/_layouts/Confirmation.aspx)
  • Error (/_layouts/Error.aspx)
  • Login (/_layouts/Login.aspx)
  • RequestAccess (/_layouts/ReqAcc.aspx)
  • Signout (/_layouts/SignOut.aspx)
  • WebDeleted (/_layouts/WebDeleted.aspx)

Custom Error Page

Create a page "MySiteCustomError.aspx" on "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\" location. Paste the below given code.Use the below given body.
<html>
    <head>Error</head>
    <body>Page Not found</body>
</html>

Powershell

Create a PowerShell script with any "MySiteCustomError.ps1".
#MySiteCustomError.aspx - Custom error Page
#Error.aspx - Default error Page

write-host "Setup the custom Error Page"
Set-SPCustomLayoutsPage -Identity "Error" -RelativePath "/_layouts/MySiteCustomError.aspx" -WebApplication "http://sp2010:50000/"

# Function for recycling
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
function RecycleAppPools()
{
        $serv = new-object Microsoft.Web.Administration.ServerManager
 
        $serv.ApplicationPools | ? { $_.Name -like "SharePoint - 50000" } | % { $_.Recycle() }
}

write-host "Recycle the IIS"
RecycleAppPools

write-host "Script Completed!"


Test Case

  1. My Site > My Content > Blog
  2. I went to a blog and just changed an ID in querystring to a blog which was not there "http://sp2010:50000/personal/administrator/Blog/Lists/Posts/Post.aspx?ID=3". You can see the error which will pop up. This is the default error page in layouts.
  3. Run my "MySiteCustomError.ps1" in "Sharepoint 2010 Management Shell".(Note : Please change the URL and the Application Pool Name before running the script.)
  4. Refresh the "http://sp2010:50000/personal/administrator/Blog/Lists/Posts/Post.aspx?ID=3" and you can see the picture given below.

Hope this was much simpler and easier. Please leave me a message if you need any help.