0

Powershell Create IIS Site

<#
.SYNOPSIS
 Create and IIS Site

.DESCRIPTION
 - This script creates and IIS based on the user input
 - The  script takes back up of the existing IIS configuration
   Restores back to the the backed up IIS Configuration if something goes wrong
 - This script is meant for education/demonstration purpose only and not meant for production use
 - This script plays by the user inputs, you could take the inputs as aruguments. For instance,
   #$siteName    = Read-Host 'What is the name of your site' 
       could be
   #$siteName    = $args&#91;0&#93;
   You could invoke the script as InstallIISSite.ps1 &#91;WebsiteName&#93; &#91;AppPoolName&#93; &#91;Port&#93; &#91;Path&#93; (&#91;domain\user&#93; &#91;password&#93;)

.Note
 - This scripts requires admin priviliges!
#>

# Add 'WebAdministration' modules to the current session. 
# The modules must be installed on the local computer or a remote computer

Import-Module WebAdministration

#Suspends the activity in a script or session for the specified period of time in secods
Start-Sleep 2 
 
# Get the user inputs for SiteName, AppPool, Port, Path for the site, user and password
$siteName    = Read-Host 'What is the name of your site?'           
$appPoolName = Read-Host 'What is the name of the application pool?'
$port        = Read-Host 'Specify the port number'
$path        = Read-Host 'Choose the path for your website'
$user        = Read-Host 'Specify user in the followng fashion domain\username'
$password    = Read-Host 'Enter password' -AsSecureString 

<# Or you can have the following

$siteName    = $args&#91;0&#93;           
$appPoolName = $args&#91;1&#93;
$port        = $args&#91;2&#93;
$path        = $args&#91;3&#93;
$user        = $args&#91;4&#93;
$password    = $args&#91;5&#93;

You may invoke the script as 
InstallIISSite.ps1 &#91;WebsiteName&#93; &#91;AppPoolName&#93; &#91;Port&#93; &#91;Path&#93; (&#91;domain\user&#93; &#91;password&#93;)
#>
 
#Backup the IIS Configuration
#Create the name for back up folder
$backupName = "$(Get-date -format "yyyyMMdd-HHmmss")-$siteName"

#"Backing up IIS config to backup named $backupName"
# Backup will be created at C:\Windows\System32\inetsrv\backup
$backup = Backup-WebConfiguration $backupName

 
try { 
    # Delete the website & app pool if already existed
    if (Test-Path "IIS:\Sites\$siteName") {
        "Removing existing website $siteName"
        Remove-Website -Name $siteName -Confirm
    }
 
    if (Test-Path "IIS:\AppPools\$appPoolName") {
        "Removing existing AppPool $appPoolName"
        Remove-WebAppPool -Name $appPoolName -Confirm
    }
 
    #Remove sites using the same port
    foreach($site in Get-ChildItem IIS:\Sites) {
        if( $site.Bindings.Collection.bindingInformation -eq ("*:" + $port + ":")){
            "Warning: Found an existing site '$($site.Name)' already using port $port. Removing it..."
             Remove-Website -Name  $site.Name -Confirm
             "Website $($site.Name) removed"
        }
    }
 
    "Create an appPool named $appPoolName with v4.0 framework"
    $pool = New-WebAppPool $appPoolName
    $pool.managedRuntimeVersion = "v4.0"
    if ($user -ne $null -AND $password -ne $null) {
	"Setting Application Pool to run under the $user"
		$pool.processmodel.identityType = 3
		$pool.processmodel.username = [string] $user
		$pool.processmodel.password = [string] $password
	}
    else
    {
    #If no user name is not specified, set the ID as NetworkService
      $pool.processModel.identityType = 2
    }
	
    $pool | Set-Item

    if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Started") {
        throw "Application pool $appPoolName was created but did not start!"
    }
 
    "Create a website $siteName pointing at $path and create with port $port"
    $website = New-Website -Name $siteName -PhysicalPath $path -ApplicationPool $appPoolName -Port $port
 
    if ((Get-WebsiteState -Name $siteName).Value -ne "Started") {
        throw "Website $siteName was created but did not start!"
    }
 
    "Website and Application Pool was created and started successfully"
} 
catch
 {
    "Oops something went wrong! Rolling back web server to its initial state. Please wait..."
     Start-Sleep 3 
     Restore-WebConfiguration $backupName
     "IIS Restore complete."
     throw
}

Leave a Reply