Red Gate forums :: View topic - Start/Stop a website in IIS 7
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

Deployment Manager PowerShell Scripts forum

Start/Stop a website in IIS 7

Search in Deployment Manager PowerShell Scripts forum
Post new topic   Reply to topic
Jump to:  
Author Message
chirayu



Joined: 17 Sep 2012
Posts: 62

PostPosted: Thu Aug 22, 2013 11:46 am    Post subject: Start/Stop a website in IIS 7 Reply with quote

Script to start/stop a website with the given name in IIS 7.

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.

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

Code:

# 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()

    _________________
    Chirayu Shishodiya
    Software Engineer - Deployment Manager
    Red Gate
    Back to top
    View user's profile Send private message
    Display posts from previous:   
    Reply to topic All times are GMT + 1 Hour
    Page 1 of 1

     
    You cannot post new topics in this forum
    You cannot reply to topics in this forum
    You cannot edit your posts in this forum
    You cannot delete your posts in this forum
    You cannot vote in polls in this forum


    Powered by phpBB © 2001, 2005 phpBB Group