PowerShell Tip of the Week: Restart Web Application remotely

In todays short article I want to share few simple one-liner scripts that I’m using for Web Applications. They might be useful for your daily administration and if you want to create automated reports.

Web Application pool

I often need to check application pools status and depends on the output perform some actions. To use examples from this article you need WebAdministration module commands. Below you can find how to check status, restart or start them on remote machines. All examples are based on Invoke-Command.

#Servers list - ensure that all servers are up and running
$Servers = Get-Content "D:\scripts\servers.txt"

#Check single web application status
icm -cn $Servers { (Get-WebAppPoolState | Where-Object {$_.ItemXPath -match "OAuth"}).value }

#Check web applications status where status is different than 'Started'
icm -cn $Servers { Get-ChildItem IIS:\AppPools | Where-Object {$_.state -ne "Started"} }

#Check web applications status where status is different than 'Started' and start them
icm -cn $Servers { Get-ChildItem IIS:\AppPools | Where-Object {$_.state -ne "Started"} | Start-WebAppPool -Passthru }

#Start single application pool and pass result
icm -cn $Servers { Start-WebAppPool -Name "OAuth" -Passthru }

#Restart single application pool
icm -cn $Servers { Restart-WebAppPool -Name "OAuth" }


Report example

Some time ago I used Web Administration commands in one of my daily reports. You can combine several different commands to create similiar report. In this case I wanted to get Boot up time, ADFS service status, Application Pools general status, and SecureChannel.

Final version was a little bit more advanced but I hope that it might be helpful to some of you:

    #=======================================================================
    $Servers =  "ADFS01",
                "ADFS02",
                "ADFS03", 
                "ADFS04",
                "ADFS05",
                "ADFS06",
                "ADFS07",
                "ADFS08",
                "ADFS09"

    $Results = @()
    #=======================================================================
    Try{
        $Results = icm -cn $Servers {
		
                 $Stats = @{} | Select BootUp,ADFS,AppPools,SecureChannel
                 Import-Module WebAdministration; 
                 $APs = dir IIS:\AppPools | Select @{n='Server';e={[Environment]::GetEnvironmentVariable("computername")}},Name,State
                 $CheckAPs = $APs | ?{$_.State -ne 'Started'}
                 If ( $CheckAPs ) { $Stats.AppPools = "Failed" } Else { $Stats.AppPools = "Started"}
                 $Services = Get-Service adfssrv
                 $Stats.ADFS = ($Services | ? { $_.Name -eq "adfssrv"}).status
                 $SecureChannel = nltest.exe /sc_verify:$env:USERDNSDOMAIN | Where-Object {$_ -match "Trusted DC Name"} | ForEach {$_.trim().Substring(18)}
                 $Stats.SecureChannel = $SecureChannel
                 $Boot = (Get-CimInstance -ClassName win32_operatingsystem).lastbootuptime
                 $Stats.Bootup = $Boot
                 $Stats
		
        } -Erroraction Stop | Select @{n='ServerName';e={$_.pscomputername}},Bootup,ADFS,AppPools,SecureChannel
    }
    Catch [System.Exception]{
	    Write-host "Error" -backgroundcolor red -foregroundcolor yellow
        $_.Exception.Message
    }
    #=======================================================================
    $Results | Out-GridView -Title "Results"

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.