PoshCode Logo PowerShell Code Repository

Restart-IISAppPool by Joel Bennett 5 years ago (modification of post by Joel Bennett view diff)
View followups from kkkkkkk and RecycleAppPool | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2466"></script>download | new post

Restarts local or remote IIS AppPools

  1. function Restart-IISAppPool {
  2.    [CmdletBinding(SupportsShouldProcess=$true)]
  3.    #.Synopsis
  4.    #  Restarts an IIS AppPool
  5.    #.Parameter ComputerName
  6.    #  The name of an IIS web server where the AppPool resides
  7.    #.Parameter AppPool
  8.    #  The name of the AppPool to recycle (if you include wildcards, results in an initial call to list all app pools)
  9.    #.Parameter Credential
  10.    #  Credentials to connect to the IIS Server
  11.    #.Example
  12.    #  Restart-IISAppPool "Classic .NET AppPool" -Cn WebServer1
  13.    #
  14.    #  Description
  15.    #  -----------
  16.    #  Restart the "Classic .NET AppPool" on WebServer1
  17.    #.Example
  18.    #  Restart-IISAppPool *Classic* -Confirm
  19.    #
  20.    #  Description
  21.    #  -----------
  22.    #  Restart all of the app pools with "Classic" in the name on the local IIS server, prompting for each one
  23.    param(
  24.       [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory= $true, Position=0)]
  25.       [Alias("Name","Pool")]
  26.       [String[]]$AppPool
  27.    ,
  28.       [Parameter(ValueFromPipelineByPropertyName=$true, Position=1)]
  29.       [Alias("CN","Server","__Server")]
  30.       [String]$ComputerName
  31.    ,
  32.       [Parameter()]
  33.       [System.Management.Automation.Credential()]
  34.       $Credential = [System.Management.Automation.PSCredential]::Empty
  35.    )
  36.    
  37.    begin {
  38.       if ($Credential -and ($Credential -ne [System.Management.Automation.PSCredential]::Empty)) {
  39.          $Credential = $Credential.GetNetworkCredential()
  40.       }
  41.       Write-Debug ("BEGIN:`n{0}" -f ($PSBoundParameters | Out-String))
  42.  
  43.       $Skip = $false
  44.       ## Need to test for AppPool existence (it's not defined in BEGIN if it's piped in)
  45.       if($PSBoundParameters.ContainsKey("AppPool") -and $AppPool -match "\*") {
  46.         $null = $PSBoundParameters.Remove("AppPool")
  47.         $null = $PSBoundParameters.Remove("WhatIf")
  48.         $null = $PSBoundParameters.Remove("Confirm")
  49.         Write-Verbose "Searching for AppPools matching: $($AppPool -join ', ')"
  50.  
  51.         Get-WmiObject IISApplicationPool -Namespace root\MicrosoftIISv2 -Authentication PacketPrivacy @PSBoundParameters |
  52.         Where-Object { @(foreach($pool in $AppPool){ $_.Name -like $Pool -or $_.Name -like "W3SVC/APPPOOLS/$Pool" }) -contains $true } |
  53.         Restart-IISAppPool
  54.         $Skip = $true
  55.       }
  56.       $ProcessNone = $ProcessAll = $false;
  57.    }
  58.    process {
  59.       Write-Debug ("PROCESS:`n{0}" -f ($PSBoundParameters | Out-String))
  60.    
  61.       if(!$Skip) {
  62.         $null = $PSBoundParameters.Remove("AppPool")
  63.         $null = $PSBoundParameters.Remove("WhatIf")
  64.         $null = $PSBoundParameters.Remove("Confirm")
  65.          foreach($pool in $AppPool) {
  66.             Write-Verbose "Processing $Pool"
  67.             if($PSCmdlet.ShouldProcess("Would restart the AppPool '$Pool' on the '$(if($ComputerName){$ComputerName}else{'local'})' server.", "Restart ${Pool}?", "Restarting IIS App Pools on $ComputerName")) {
  68.                Write-Verbose "Restarting $Pool"
  69.                # if($PSCmdlet.ShouldContinue("Do you want to restart $Pool?", "Restarting IIS App Pools on $ComputerName", [ref]$ProcessAll, [ref]$ProcessNone)) {
  70.                  Invoke-WMIMethod Recycle -Path "IISApplicationPool.Name='$Pool'" -Namespace root\MicrosoftIISv2 -Authentication PacketPrivacy @PSBoundParameters
  71.                # }
  72.             }
  73.          }
  74.       }
  75.    }
  76. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me