Posted On November 2, 2020

PowerShell: Start IIS Sites & App Pools

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Start IIS Sites & App Pools

Before running the below script, it’s necessary to mark certain IIS App pools and websites to automatically start. Hence, those services are expected to be up at all times. Here’s are some some quick instructions on how to enable that:

Run InetMgr.exe > Navigate to Application Pools > right-click an App Pool item > Advanced Settings…

Set Start Mode = ‘AlwaysRunning’ > OK

$servers='IRV-WEBSERVER05'
$results=[PSCustomObject]@{}
Invoke-Command -ComputerName $Servers {
    Import-Module -Name WebAdministration
    $sites=Get-Website|Where-Object serverAutoStart -eq $true
    foreach ($site in $sites) {
        switch ($site) {
            {(Get-WebAppPoolState -Name $_.applicationPool).Value -eq 'Stopped'} {
                write-host "$($_.applicationPool) is stopped. Restarting it..."
                try{
                    Start-WebAppPool -Name $_.applicationPool -ea Stop
                    $errors='none'
                }catch{
                    $errors=$_
                    Write-Warning $errors
                    }
                }finally{
                    $results+=@{
                        computername=$env:computername
                        item=$_.applicationPool
                        errorMessage=$errors
                        }
                }
            {$_.State -eq 'Stopped'} {
                write-host "$($site.Name) is stopped. Restarting it..."
                try{
                    Start-Website -Name $site.Name -ea Stop
                    $errors='none'
                }catch{
                    $errors=$_
                    Write-Warning $errors
                }finally{
                    $results+=@{
                        computername=$env:computername
                        item=$site.Name
                        errorMessage=$errors
                        }
                }              
            }
        }
    }
}

write-host ($results|out-string).trim()

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Create New Hybrid On Prem Active Directory User with Office 365 Integration

# createNewHybridUser_v0.0.1.ps1# .Description: this script automates the creation of a user account in a hybrid…

How To Move WordPress Site To Kubernetes Cluster

a. Create backups of source files and database - Logon to Current Hosting Provider to…

Basic CSS: Add Borders Around Your Elements

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}.smaller-image {width: 100px;}</style><h2 class="red-text">CatPhotoApp</h2><main><p…