Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param
- (
- [Parameter(Mandatory, HelpMessage = "Enter a comma separated list of servers to query for IIS websites.")]
- [string[]] $Servers
- )
- function GetPoolList([string[]]$serverList)
- {
- # Create PSSession for each server in the list.
- $PSSessions = $serverList | ForEach-Object {
- New-PSSession -ComputerName $_
- }
- $i = 1 # The display list will start at #1
- $lookup = [ordered]@{} # New lookup table with the key as the number that is displayed to the user for choosing.
- # We do an OrderedDictionary above so that when we perform a 'foreach' on it, the items inside will be processed in the correct order.
- # ... otherwise the displayed list will be backwards.
- foreach ($session in $PSSessions)
- {
- $webSites = @(
- Invoke-Command -Session $session -ScriptBlock {
- Import-Module IISAdministration
- Get-IISSite | Select-Object Name -Unique | Sort-Object Name | Select-Object -ExpandProperty Name
- }
- )
- foreach ($site in $webSites)
- {
- # $i is turned into a string here, because an ordered dictionary can be indexed by numbers.
- # If we didn't stringify it, all of the user's choices would actually indicate the next website in the list
- # e.g. - 1, 3, 6 -- would be recycle the 2nd, 4th, and 7th websites in the list.
- [void]$lookup.Add("$i", [pscustomobject]@{
- Site = $site
- ListDisplay = ("{0}: {1} - {2}" -f $i, $session.ComputerName, $site) # This is the display line that will be presented to the user
- Session = $session
- })
- $i++
- }
- }
- , $lookup # return the lookup table
- }
- Write-Host "`nChoose a website from the following list:`n" -f Yellow
- $allSitesInfo = GetPoolList -serverList $Servers
- foreach ($kvp in $allSitesInfo.GetEnumerator())
- {
- Write-Host $kvp.Value.ListDisplay -f Cyan
- }
- # The list websites and servers have now been displayed.
- [string[]]$RecyclePools = (read-host "`nSelect website numbers to recycle (separated by comma)") -split ',' | Get-Unique
- # We explicitly define the entered numbers above as "strings", otherwise PowerShell might try to convert them back to Int32.
- # Also, we do Get-Unique above just in case somebody puts the same number multiple times (we don't need to recycle that much...)
- Write-Host "`n" # Just an extra line break
- # For each number they've entered...
- # ... retrieve the website to get each AppPool and then recycle them.
- foreach ($number in $RecyclePools)
- {
- $siteToRecycle = $allSitesInfo[$number].Site
- $sessionToUse = $allSitesInfo[$number].Session
- Write-Host "Recycling all app pools for $siteToRecycle on $($sessionToUse.ComputerName)..." -f Green
- Invoke-Command -Session $sessionToUse -ScriptBlock {
- $iisSite = Get-IISSite -Name $using:siteToRecycle
- foreach ($appPool in $iisSite.Applications)
- {
- $(Get-IISAppPool -Name $appPool.ApplicationPoolName).Recycle()
- }
- }
- }
- foreach ($obj in $allSitesInfo.GetEnumerator())
- {
- $obj.Value.Session | Remove-PSSession
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement