Start/Stop a website in IIS 7
chirayu
Posts: 79
Script to start/stop a website with the given name in IIS 7.
Brief notes on how to use this script
It searches the system root folder to find Microsoft.Web.Administration -> creates ServerManager object -> gets site we want to start/stop -> starts/stops the site.
Brief notes on how to use this script
- Add 'websiteToStartOrStop' variable in Deployment Manager UI and set it to the website name you want to start/stop
- If you want to stop a website, uncomment the last two lines and comment out the two lines before it that start a website.
It searches the system root folder to find Microsoft.Web.Administration -> creates ServerManager object -> gets site we want to start/stop -> starts/stops the site.
# Inputs: # REQUIRED # $websiteToStartOrStop = "Default Web Site" if ($websiteToStartOrStop -eq $null) { # Fail the script and deployment if can't start website. throw "websiteToStartOrStop variable must be set." } # Search for Microsoft.Web.Administration.dll in file system $directoryToSearch = $env:systemroot Write-Host "Searching for Microsoft.Web.Administration.dll in '$directoryToSearch'..." $webDllDirectory = Get-ChildItem -Path $directoryToSearch -Filter Microsoft.Web.Administration.dll -Recurse -ErrorAction 'silentlycontinue' ` | where { $_.DirectoryName.Contains("SysWOW64") -or $_.DirectoryName.Contains($env:windir + "\assembly")} ` | select -First 1 if ($webDllDirectory -eq $null) { throw "Could not find 'Microsoft.Web.Administration.dll'. It is required to start/stop '$websiteToStartOrStop' website." } # Load the dll (console output of the command is deleted) [System.Reflection.Assembly]::LoadFrom($webDllDirectory.DirectoryName.toString() + "\Microsoft.Web.Administration.dll") | Out-Null $serverManager = new-object Microsoft.Web.Administration.ServerManager # From all sites, select the site we want to start $site = $serverManager.Sites | where {$_.Name -eq $websiteToStartOrStop} if ($site -eq $null) { # Fail the script and exit if cannot find website throw "Could not find website with name: $websiteToStartOrStop" } Write-Host "Starting website with name: $websiteToStartOrStop" $site.Start() # Write-Host "Stopping website with name: $websiteToStartOrStop" # $site.Stop()