function Restart-AlkamiAppPool { <# .SYNOPSIS Restarts an application pool and opens the user's default browser to warm it up #> [CmdletBinding()] Param() DynamicParam { # Define the Paramater Attributes $ParameterName = "AppPoolName" $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute.Mandatory = $true $ParameterAttribute.Position = 0 $AttributeCollection.Add($ParameterAttribute) # Generate and add the ValidateSet $arrSet = $appTierApplications | ForEach-Object {$_.WebAppName} $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) # Create the dynamic parameter $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) return $RuntimeParameterDictionary } begin { # Bind the parameter to a variable $AppPoolName = $PsBoundParameters[$ParameterName] } process { try { $logLead = (Get-LogLeadName); $mgr = New-Object Microsoft.Web.Administration.ServerManager $appPool = $mgr.ApplicationPools[$appPoolName] $processId = $appPool.WorkerProcesses.ProcessId Write-Output ("$logLead : Recycling application pool {0}" -f $AppPoolName) $appPool.Recycle() | Out-Null $serviceEndpoint = ($appTierApplications | Where-Object {$_.WebAppName -eq $AppPoolName} | Select-Object -First 1).Endpoint $site = "http://localhost/$AppPoolName/$serviceEndpoint" $browserId = Open-UrlInDefaultBrowser $site Start-Sleep -Seconds 15 try { if ($null -ne (Get-Process -Id $processId -ErrorAction SilentlyContinue)) { Write-Verbose ("$logLead : Stopping old application pool process ID {0}" -f $processId) Stop-Process -Id $processId -ErrorAction SilentlyContinue -Force } } catch { Write-Warning ("$logLead : An unexpected exception occurred while stopping the old application pool process, which is probably due to the process previously exiting") } try { if ($null -ne (Get-Process -Id $browserId -ErrorAction SilentlyContinue)) { Write-Verbose ("$logLead : Stopping browser process ID {0}" -f $browserId) Stop-Process -Id $browserId } } catch { ## If this doesn't work the user can close their own browser Write-Warning "Unable to stop the browser process ID. Manual intervention of browser closing will have to occur." } Write-Output ("$logLead : Completed restart of application pool {0}" -f $AppPoolName) } finally { if ($null -ne $mgr) { $mgr = $null } } } }