Skip to content

Instantly share code, notes, and snippets.

@CamiloBernal
Forked from alastairtree/CreateSite.ps1
Created November 12, 2020 22:17
Show Gist options
  • Save CamiloBernal/cf2feda490e11d462dfe1ff9c1b125af to your computer and use it in GitHub Desktop.
Save CamiloBernal/cf2feda490e11d462dfe1ff9c1b125af to your computer and use it in GitHub Desktop.
IIS site stop and create website/appPool utility scripts
## FYI: These scripts are now maintained in a proper repository:
## https://github.com/alastairtree/deploy-websites-with-powershell
## Intro: Simple powershell script to install (or replace) a local website and app pool
## Usage: CreateSite.ps1 [WebsiteName] [AppPoolName] [Port] [Path] ([domain\user] [password])
## Note : These scripts require local admin priviliges!
# Load IIS tools
Import-Module WebAdministration
sleep 2 #see http://stackoverflow.com/questions/14862854/powershell-command-get-childitem-iis-sites-causes-an-error
# Get SiteName and AppPool from script args
$siteName = $args[0] # "default web site"
$appPoolName = $args[1] # "DefaultAppPool"
$port = $args[2] # "80"
$path = $args[3] # "c:\sites\test"
$user = $args[4] # "domain\username"
$password = $args[5] # "password1"
if($siteName -eq $null) { throw "Empty site name, Argument one is missing" }
if($appPoolName -eq $null) { throw "Empty AppPool name, Argument two is missing" }
if($port -eq $null) { throw "Empty port, Argument three is missing" }
if($path -eq $null) { throw "Empty path, Argument four is missing" }
$backupName = "$(Get-date -format "yyyyMMdd-HHmmss")-$siteName"
"Backing up IIS config to backup named $backupName"
$backup = Backup-WebConfiguration $backupName
try {
# delete the website & app pool if needed
if (Test-Path "IIS:\Sites\$siteName") {
"Removing existing website $siteName"
Remove-Website -Name $siteName
}
if (Test-Path "IIS:\AppPools\$appPoolName") {
"Removing existing AppPool $appPoolName"
Remove-WebAppPool -Name $appPoolName
}
#remove anything already using that 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
"Website $($site.Name) removed"
}
}
"Create an appPool named $appPoolName under v4.0 runtime, default (Integrated) pipeline"
$pool = New-WebAppPool $appPoolName
$pool.managedRuntimeVersion = "v4.0"
$pool.processModel.identityType = 2 #NetworkService
if ($user -ne $null -AND $password -ne $null) {
"Setting AppPool to run as $user"
$pool.processmodel.identityType = 3
$pool.processmodel.username = $user
$pool.processmodel.password = $password
}
$pool | Set-Item
if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Started") {
throw "App pool $appPoolName was created but did not start automatically. Probably something is broken!"
}
"Create a website $siteName from directory $path on 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 automatically. Probably something is broken!"
}
"Website and AppPool created and started sucessfully"
} catch {
"Error detected, running command 'Restore-WebConfiguration $backupName' to restore the web server to its initial state. Please wait..."
sleep 3 #allow backup to unlock files
Restore-WebConfiguration $backupName
"IIS Restore complete. Throwing original error."
throw
}
## FYI: These scripts are now maintained in a proper repository:
## https://github.com/alastairtree/deploy-websites-with-powershell
## Intro: Simple powershell script to stop a local website and app pool
## Usage: StopSite.ps1 [WebsiteName] [AppPoolName]
## Note : These scripts require local admin priviliges!
# Load IIS tools
Import-Module webadministration
# Get SiteName and AppPool from script args
$siteName = $args[0] # "default web site"
$appPoolName = $args[1] # "DefaultAppPool"
echo "Stopping website and app pool on $env:COMPUTERNAME"
# Stop the website if it exists and is running, dont error if it doesn't
if (Test-Path "IIS:\Sites\$siteName") {
if ((Get-WebsiteState -Name $siteName).Value -ne "Stopped") {
stop-website -Name $siteName
echo "Stopped website '$siteName'"
} else {
echo "WARNING: Site '$siteName' was already stopped. Have you already run this?"
}
} else {
echo "WARNING: Could not find a site called '$siteName' to stop. Assuming this is a new install"
}
# Stop the AppPool if it exists and is running, dont error if it doesn't
if (Test-Path "IIS:\AppPools\$appPoolName") {
if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Stopped") {
Stop-WebAppPool -Name $appPoolName
echo "Stopped AppPool '$appPoolName'"
} else {
echo "WARNING: AppPool '$appPoolName' was already stopped. Have you already run this?"
}
} else {
echo "WARNING: Could not find an AppPool called '$appPoolName' to stop. Assuming this is a new install"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment