Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Create ASPX Site Within IIS Including Dedicated Application Pool

Posted on November 13, 2015November 17, 2015 By David Kittell

This script relies on this post currently as it gets a test ASPX site to make sure everything was setup correctly.

 .\IIS-Create.ps1 <Site/App Name> <Site Root Path> <Site Port> <Domain User Account> <Domain User Password>
clear
 
Function Load-WebAdmin
    {
        $webAdminModule = get-module -ListAvailable  | ? { $_.Name -eq "webadministration" }
        If ($webAdminModule -ne $null)
            {
                import-module WebAdministration
            }
    }

# Variables - Start 
#$appName = "DemoSite"
$appName = $args[0] 
#$physicalPath = "C:\inetpub\wwwroot\";
$physicalPath = $args[1]
$servername = $env:COMPUTERNAME
$sitePort = $args[2] 
$appPoolUsername = $args[3]
$appPoolPassword = $args[4] 
$appPoolNETVersion = $args[5]
#$appPoolNETVersion = "v4.0"
#$appPoolPeriodicRestart = $args[6]
#$appPoolPeriodicRestart = 100000
$appPoolPath = $("IIS:\AppPools\" + $appName)
$sitePath = $("IIS:\Sites\" + $appName)
$runscript = 1
# Variables - Stop

# Check Parameter Input - Start
if($appName -eq $null) {
    write-host "Error: App/Site Name, Argument is missing"
    $runscript = 0
}
if($physicalPath -eq $null) {
    write-host "Error: Physical Path, Argument is missing"
    $runscript = 0
}
if($sitePort -eq $null) {
    write-host "Error: Site Port, Argument is missing"
    $runscript = 0
}
if($appPoolUsername -eq $null) {
    write-host "Error: App Pool Username, Argument is missing"
    $runscript = 0
}
if($appPoolPassword -eq $null) {
    write-host "Error: App Pool Password, Argument is missing"
    $runscript = 0
}
if($appPoolNETVersion -eq $null) {
    write-host "Error: App Pool .NET Framework Version, Argument is missing"
    $runscript = 0
}
#if($appPoolPeriodicRestart -eq $null) {
    #write-host "Error: App Pool Periodic Restart, Argument is missing"
    #$runscript = 0
#}


# Check Parameter Input - Stop
 
# If appName was not passed show instructions
if (!($runscript -eq 1))
{
    write-host "`nMissing Parameters"
    write-host "`n .\IIS-Create.ps1 <Site/App Name> <Site Root Path> <Site Port> <App Pool Username> <App Pool Password> <App Pool .NET Version>`n"
}
else
{
    # Make sure the last character in the Physical Path is a \, if it isn't add a \
    if(($physicalPath.EndsWith("\")) -ne "True")
        {
            write-host $physicalPath
            $physicalPath = $physicalPath + "\"
            write-host $physicalPath
        }

    $sitePhysicalPath = $($physicalPath + $appName)

    $currentdirectorypath = Split-Path (Get-Variable MyInvocation).Value.MyCommand.Path
 
    if((Test-Path $appPoolPath$appName) -eq 0 -and (Get-WebApplication -Name $appName) -eq $null)
        {
            # Create Directories - Start
            if ( ! (test-path -pathtype container $physicalPath))
                {
                    New-Item -ItemType directory -Path $physicalPath
                }
            if ( ! (test-path -pathtype container $physicalPath$appName))
                {
                    New-Item -ItemType directory -Path $physicalPath$appName
                }
            # Create Directories - Stop
 
            # Default Page Creation - Start
            # cd $sitePhysicalPath
            # Set-Content default.htm "DemoSite Default Page"
            .\DownloadFileExtract.ps1 http://www.kittell.net/downloads/ASPX_Site_Test.zip $sitePhysicalPath 1 $sitePhysicalPath
            Remove-Item $($sitePhysicalPath + "ASPX_Site_Test.zip")
            # Default Page Creation - Stop
 
            # Create Application Pool - Start
            #write-host $appName
            New-WebAppPool -Name $appName -Force;
            #Display Default AppPool Settings
            #get-ItemProperty -Path IIS:\AppPools\DemoSite.processModel | fl *
            $appPool = get-item $appPoolPath
            $appPool.processModel.username = $appPoolUsername
            $appPool.processModel.password = $appPoolPassword
            $appPool.processModel.identityType = 3
            $appPool | set-item
            Set-ItemProperty -Path $appPoolPath managedRuntimeVersion $appPoolNETVersion
            #Set-ItemProperty -Path $appPoolPath -Name recycling.periodicRestart.requests -Value $appPoolPeriodicRestart
           
            $appPool.Stop();
            $appPool.Start();
            # Create Application Pool - Stop

            # Check Application Pool - Start
            if ((Get-WebAppPoolState -Name $appName).Value -ne "Started") {
                # Application Pool failed to start, throw error message and stop the script
                write-host "App pool $appName was created but did not start automatically."
            }  
            # Check Application Pool - Stop          
            else
            {
                # Application Pool started, continue with the script
 
            # Create New Site - Start            
            $bindingInformation = $(":" + $sitePort +":"+$servername)
          #  cd $appPoolPath
            New-Item $sitePath -bindings @{protocol="http";bindingInformation=$bindingInformation} -physicalPath $sitePhysicalPath
            # Create New Site - Stop

            # Check Web Site - Start
            if ((Get-WebsiteState -Name $appName).Value -ne "Started")
            {
                Write-Host  "Website $appName was created but did not start automatically."
            }
            # Check Web Site - Stop
 
            # IIS Site(s) - Get Status
            Write-Host "IIS Web Sites"
            get-website | select name,id,state,physicalpath, 
                @{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }} ,
                @{n="LogFile";e={ $_.logfile | select -expa directory}}, 
                @{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ';' }} 
            # IIS Site(s) - Get Status
 
            #Assign Site to App Pool - Start             
            New-WebApplication -Name $appName -ApplicationPool $appName -Site $appName -PhysicalPath $sitePhysicalPath;
            #Assign Site to App Pool - Stop
                         
            Start $("http://" + $servername + ":" + $sitePort)
            cd $currentdirectorypath
            }
             
        }
    else
        {
            Write-Host "$appName already exists";
        }
    }
Originally Posted on November 13, 2015
Last Updated on November 17, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

List Tables In Database

Posted on February 25, 2013October 26, 2015

SELECT t.name ,i.rows FROM sys.tables AS t INNER JOIN sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2 ORDER BY t.name Originally Posted on February 25, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code…

Read More

Ektron Find Replace Library Folder Names

Posted on October 23, 2013October 26, 2015

This script will rename folder paths of renamed CMS folders. SET XACT_ABORT ON BEGIN TRAN DECLARE @currtext NVARCHAR(255) DECLARE @newtext NVARCHAR(255) DECLARE @search NVARCHAR(500) DECLARE @replace VARCHAR(500) DECLARE @pos INT DECLARE @id BIGINT DECLARE @libtype_id INT — folder names SET @search = ” –string to find SET @replace = ”…

Read More

Escape Characters

Posted on April 17, 2013October 26, 2015

‘ for a single quote " for a double quote for a backslash \0 for a null character a for an alert character b for a backspace f for a form feed n for a new line r for a carriage return t for a horizontal tab v for a…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes