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
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.