Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Start / Stop IIS Website and Application Pool

Posted on November 16, 2015 By David Kittell
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.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

File Exists

Posted on December 14, 2013October 26, 2015

Public Function FileExists(ByVal FileFullPath As String) _ As Boolean Dim f As New IO.FileInfo(FileFullPath) Return f.Exists End Function Originally Posted on December 14, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran on…

Read More

Mac OSX Terminal – Get Artist and Song From iTunes PlayList Export

Posted on December 28, 2016March 6, 2017

There are more elegant ways to do this but I got bored and thought I’d do something that is only done with base Mac OSX tools #!/bin/sh # Read_iTunes_PlayList.sh # # Get only the artist list – remove duplicates # Read_iTunes_PlayList.sh 1 iTunesPlayListFileName ~/Desktop # # Get Artist and Song…

Read More

PowerShell – Azure – Get VM Image List

Posted on April 6, 2016April 7, 2016

When installing a new VM in Azure it’s helpful to know your options Get-AzureVMImage | Select OS, label ( Get-AzureVMImage | where-object { $_.Label -like "Barracuda*" } ) | Fl OS, publishername, Location, logicalsizeinGB, Label, ImageFamily, ImageName ( Get-AzureVMImage | where-object { $_.Label -like "CentOS*" } ) | Fl OS,…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes