Overview
This PowerShell script is designed to stop an IIS Application Pool and Website, retrying multiple times if necessary. If the Application Pool name is not provided, it defaults to using the Website name.
Features
- Stops the specified IIS Application Pool and Website.
 - Retries up to 10 times with a 5-second wait between attempts.
 - Ensures that both the Application Pool and Website are fully stopped before exiting.
 - Provides console output for tracking progress.
 
Usage
Parameters
$webSiteName(Required) – The name of the IIS website to stop.$appPoolName(Optional) – The name of the application pool to stop. If not provided, it defaults to the website name.
Execution
To run the script, use the following command in PowerShell:
.\stop-iis.ps1 -webSiteName "MyWebsite" -appPoolName "MyAppPool"
or if the Application Pool name is the same as the Website name:
.\stop-iis.ps1 -webSiteName "MyWebsite"
How It Works
- The script checks if 
$appPoolNameis provided; if not, it assigns$webSiteNameto$appPoolName. - It initializes retry logic with a maximum of 10 attempts and a 5-second delay.
 - It checks the current state of the Application Pool and Website.
 - If either is not stopped, the script attempts to stop them.
 - It waits for 5 seconds and rechecks the states.
 - If both are stopped, it exits successfully; otherwise, it retries up to 10 times.
 - If after 10 attempts the services are still running, it logs a warning.
 
Modifications
To adjust retry attempts or wait time, modify these lines:
$maxAttempts = 10  # Change the number of retries
$waitTime = 5  # Change the wait time in seconds
Prerequisites
- PowerShell with administrative privileges.
 - IIS installed with the 
WebAdministrationmodule. 
Troubleshooting
- Ensure PowerShell is running as an administrator.
 - Verify IIS is installed and running.
 - Check if the specified website and application pool exist.
 
Author
This script was designed for developers managing IIS instances and requiring automated shutdown with retry mechanisms.
Full script: stop_app.ps1
param ($webSiteName, $appPoolName)
import-module WebAdministration
if (-not $appPoolName) {
    $appPoolName = $webSiteName
}
$maxAttempts = 10
$attempt = 0
$waitTime = 5  # Seconds
while ($attempt -lt $maxAttempts) {
    $appPoolState = (Get-WebAppPoolState -Name $appPoolName).Value
    $webSiteState = (Get-WebsiteState -Name $webSiteName).Value
    
    if ($appPoolState -ne 'Started') {
        Write-Output ('Starting Website Pool: {0}' -f $webSiteName)
        Start-Website -Name $webSiteName
    }
    
    if ($webSiteState -ne 'Started') {
        Write-Output ('Starting Application Pool: {0}' -f $appPoolName)
        Start-WebAppPool -Name $appPoolName
    }
    
    Start-Sleep -Seconds $waitTime
    
    # Check again after waiting
    $appPoolState = (Get-WebAppPoolState -Name $appPoolName).Value
    $webSiteState = (Get-WebsiteState -Name $webSiteName).Value
    
    if ($appPoolState -eq 'Started' -and $webSiteState -eq 'Started') {
        Write-Output 'Both Application Pool and Website are Started.'
        break
    }
    
    $attempt++
}
if ($attempt -eq $maxAttempts) {
    Write-Output 'Reached maximum attempts. The Application Pool or Website may not be fully Started.'
}
		
		
			
