How do I recycle an IIS App pool with Powershell?

Posted by Ralph Willgoss on Geeks with Blogs See other posts from Geeks with Blogs or by Ralph Willgoss
Published on Sun, 25 Nov 2012 16:36:07 GMT Indexed on 2012/11/25 17:05 UTC
Read the original article Hit count: 390

Filed under:
Reference implementation of a Powershell script to recycle app pools, in response to Rick's post:
http://www.west-wind.com/weblog/posts/2012/Oct/02/A-tiny-Utility-to-recycle-an-IIS-Application-Pool


#    File: RecycleAppPool.ps1
#    Author: Ralph Willgoss
#    Date: 2nd Oct 2012

#    Reference:
#    http://stackoverflow.com/questions/198623/how-do-i-recycle-an-iis-apppool-with-powershell
#
#    Alternative is to create a Process and run the inbuilt vbs:
#    C:\WINDOWS\system32\iisapp.vbs => "IIsApp /a DefaultAppPool /r"
#  
#    Windows 2003 & II6 C:\WINDOWS\system32>cscript.exe iisapp.vbs /a StaticDataAppPool /r
#    Windows 2008 IIS7 [tbd]
# =============================================================================
#    Iniatialise
=============================================================================
param ( )

=============================================================================
#   Main
=============================================================================

Write-OutPut ""
Write-OutPut "Starting Recycling App Pool"
Write-OutPut ""

$appPoolName = "StaticDataAppPool" #$args[0]
$appPool = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool"
          | Where-Object { $_.Name -eq "W3SVC/APPPOOLS/$appPoolName" }
          
$appPool.Recycle()

Write-OutPut ""
Write-OutPut "Finished Recycling App Pool"
Write-OutPut ""

© Geeks with Blogs or respective owner