PoshCode Archive  Artifact [b0a4db455e]

Artifact b0a4db455effb4e39f7874e045a22860819fec2ca10acf66cc0b790bf2795bc6:

  • File Restart-IISAppPool.ps1 — part of check-in [0579fd5f88] at 2018-06-10 13:10:47 on branch trunk — Restarts local or remote IIS AppPools (user: Joel Bennett size: 3066)

# encoding: ascii
# api: powershell
# title: Restart-IISAppPool
# description: Restarts local or remote IIS AppPools
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Restart-IISAppPool
# x-poshcode-id: 2465
# x-archived: 2016-08-26T03:17:29
# x-published: 2011-01-18T11:25:00
#
#
function Restart-IISAppPool {
   [CmdletBinding(SupportsShouldProcess=$true)]
   #.Synopsis
   #  Restarts an IIS AppPool
   #.Parameter ComputerName
   #  The name of a web server to restart the AppPool on
   #.Parameter Pool
   #  The name of the AppPool to recycle (if you include wildcards, results in an initial call to list all app pools)
   param(
      [Parameter(ValueFromPipelineByPropertyName=$true)]
      [Alias("CN","Server","__Server")]
      [String]$ComputerName
   ,
      [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory= $true)]
      [Alias("Name","Pool")]
      [String[]]$AppPool
   ,
      [Parameter()]
      [System.Management.Automation.Credential()]
      $Credential = [System.Management.Automation.PSCredential]::Empty
   )
   
   begin {
      if ($Credential -and ($Credential -ne [System.Management.Automation.PSCredential]::Empty)) {
         $Credential = $Credential.GetNetworkCredential()
      }
      Write-Debug ("BEGIN:`n{0}" -f ($PSBoundParameters | Out-String))

      $Skip = $false
      ## Need to test for AppPool existence (it's not defined in BEGIN if it's piped in)
      if($PSBoundParameters.ContainsKey("AppPool") -and $AppPool -match "\*") {
        $null = $PSBoundParameters.Remove("AppPool")
        $null = $PSBoundParameters.Remove("WhatIf")
        $null = $PSBoundParameters.Remove("Confirm")

        Get-WmiObject IISApplicationPool -Namespace root\MicrosoftIISv2 -Authentication PacketPrivacy @PSBoundParameters | 
        Where-Object { @(foreach($pool in $AppPool){ $_.Name -like $Pool -or $_.Name -like "W3SVC/APPPOOLS/$Pool" }) -contains $true } |
        Restart-IISAppPool
        $Skip = $true
      }
      $ProcessNone = $ProcessAll = $false;
   }
   process {
      Write-Debug ("PROCESS:`n{0}" -f ($PSBoundParameters | Out-String))
   
      if(!$Skip) {
        $null = $PSBoundParameters.Remove("AppPool")
        $null = $PSBoundParameters.Remove("WhatIf")
        $null = $PSBoundParameters.Remove("Confirm")
         foreach($pool in $AppPool) {
            Write-Verbose "Restarting $Pool"
            if($PSCmdlet.ShouldProcess("Would restart the AppPool '$Pool' on the '$(if($ComputerName){$ComputerName}else{'local'})' server.", "Restart $AppPool?", "Restarting IIS App Pools on $ComputerName")) {
               # if($PSCmdlet.ShouldContinue("Do you want to restart $Pool?", "Restarting IIS App Pools on $ComputerName", [ref]$ProcessAll, [ref]$ProcessNone)) {
                 Invoke-WMIMethod Recycle -Path "IISApplicationPool.Name='$Pool'" -Namespace root\MicrosoftIISv2 -Authentication PacketPrivacy @PSBoundParameters
               # }
            }
         }
      }
   }

}