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"

No comments:

Post a Comment