Advertisement
Yevrag35

Recyle AppPool Interactive

Oct 29th, 2019
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param
  3. (
  4.     [Parameter(Mandatory, HelpMessage = "Enter a comma separated list of servers to query for IIS websites.")]
  5.     [string[]] $Servers
  6. )
  7.  
  8. function GetPoolList([string[]]$serverList)
  9. {
  10.     # Create PSSession for each server in the list.
  11.     $PSSessions = $serverList | ForEach-Object {
  12.         New-PSSession -ComputerName $_
  13.     }
  14.  
  15.     $i = 1                   # The display list will start at #1
  16.     $lookup = [ordered]@{}   # New lookup table with the key as the number that is displayed to the user for choosing.
  17.     # We do an OrderedDictionary above so that when we perform a 'foreach' on it, the items inside will be processed in the correct order.
  18.     # ... otherwise the displayed list will be backwards.
  19.     foreach ($session in $PSSessions)
  20.     {
  21.         $webSites = @(
  22.             Invoke-Command -Session $session -ScriptBlock {
  23.                 Import-Module IISAdministration
  24.                 Get-IISSite | Select-Object Name -Unique | Sort-Object Name | Select-Object -ExpandProperty Name
  25.             }
  26.         )
  27.  
  28.         foreach ($site in $webSites)
  29.         {
  30.             # $i is turned into a string here, because an ordered dictionary can be indexed by numbers.
  31.             # If we didn't stringify it, all of the user's choices would actually indicate the next website in the list
  32.             # e.g. - 1, 3, 6 -- would be recycle the 2nd, 4th, and 7th websites in the list.
  33.             [void]$lookup.Add("$i", [pscustomobject]@{
  34.                 Site = $site
  35.                 ListDisplay = ("{0}: {1} - {2}" -f $i, $session.ComputerName, $site) # This is the display line that will be presented to the user
  36.                 Session = $session
  37.             })
  38.             $i++
  39.         }
  40.     }
  41.  
  42.     , $lookup   # return the lookup table
  43. }
  44.  
  45. Write-Host "`nChoose a website from the following list:`n" -f Yellow
  46. $allSitesInfo = GetPoolList -serverList $Servers
  47. foreach ($kvp in $allSitesInfo.GetEnumerator())
  48. {
  49.     Write-Host $kvp.Value.ListDisplay -f Cyan
  50. }
  51. # The list websites and servers have now been displayed.
  52.  
  53. [string[]]$RecyclePools = (read-host "`nSelect website numbers to recycle (separated by comma)") -split ',' | Get-Unique
  54. # We explicitly define the entered numbers above as "strings", otherwise PowerShell might try to convert them back to Int32.
  55. # Also, we do Get-Unique above just in case somebody puts the same number multiple times (we don't need to recycle that much...)
  56.  
  57. Write-Host "`n" # Just an extra line break
  58.  
  59. # For each number they've entered...
  60. # ... retrieve the website to get each AppPool and then recycle them.
  61. foreach ($number in $RecyclePools)
  62. {
  63.     $siteToRecycle = $allSitesInfo[$number].Site
  64.     $sessionToUse = $allSitesInfo[$number].Session
  65.  
  66.     Write-Host "Recycling all app pools for $siteToRecycle on $($sessionToUse.ComputerName)..." -f Green
  67.  
  68.     Invoke-Command -Session $sessionToUse -ScriptBlock {
  69.         $iisSite = Get-IISSite -Name $using:siteToRecycle
  70.         foreach ($appPool in $iisSite.Applications)
  71.         {
  72.             $(Get-IISAppPool -Name $appPool.ApplicationPoolName).Recycle()
  73.         }
  74.     }
  75. }
  76.  
  77. foreach ($obj in $allSitesInfo.GetEnumerator())
  78. {
  79.     $obj.Value.Session | Remove-PSSession
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement