clear
Function Load-WebAdmin
{
$webAdminModule = get-module -ListAvailable | ? { $_.Name -eq "webadministration" }
If ($webAdminModule -ne $null)
{
import-module WebAdministration
}
}
# Variables - Start
$siteName = $args[0] # "default web site"
$appPoolName = $args[1] # "DefaultAppPool"
$action = $args[2] # 1 for stop, 0 for start
$runscript = 1
# Variables - Stop
# Check Parameter Input - Start
if($siteName -eq $null)
{
write-host "Error: Site Name, Argument is missing"
$runscript = 0
}
if($appPoolName -eq $null)
{
write-host "Error: App Pool Name, Argument is missing"
$runscript = 0
}
if($action -eq $null)
{
write-host "Error: Action, 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-Stop.ps1 <Site Name> <App Pool Name> <Action>`n"
}
else
{
if (Test-Path "IIS:\Sites\$siteName")
{
if ($action -eq 1)
{
if ((Get-WebsiteState -Name $siteName).Value -ne "Stopped")
{
stop-website -Name $siteName
echo "Stopped website '$siteName'"
}
else
{
echo "WARNING: Site '$siteName' was already stopped."
}
}
else
{
if ((Get-WebsiteState -Name $siteName).Value -ne "Started")
{
start-website -Name $siteName
echo "Started website '$siteName'"
}
else
{
echo "WARNING: Site '$siteName' was already started."
}
}
}
else
{
echo "WARNING: Could not find a site called '$siteName' to stop. Assuming this is a new install"
}
if (Test-Path "IIS:\AppPools\$appPoolName")
{
if ($action -eq 1)
{
if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Stopped")
{
Stop-WebAppPool -Name $appPoolName
echo "Stopped AppPool '$appPoolName'"
}
else
{
echo "WARNING: Site '$appPoolName' was already stopped."
}
}
else
{
if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Started")
{
start-WebAppPool -Name $appPoolName
echo "Started AppPool '$appPoolName'"
}
else
{
echo "WARNING: AppPool '$appPoolName' was already started."
}
}
}
else
{
echo "WARNING: Could not find an AppPool called '$appPoolName' to stop. Assuming this is a new install"
}
}
Reference: https://github.com/alastairtree/deploy-websites-with-powershell
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.