Skip to content

Instantly share code, notes, and snippets.

@lawrencegripper
Last active May 25, 2021 09:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lawrencegripper/a1561be7eeac433262fd to your computer and use it in GitHub Desktop.
Save lawrencegripper/a1561be7eeac433262fd to your computer and use it in GitHub Desktop.
IISGraceFullSessionMonitor
#Get the open requests being handled by IIS
$req = Get-WebRequest
while ($req.Count -gt 0)
{
#Wait if there are some
Write-Host "Waiting for request, current inflight: " $req.Count
Start-Sleep -Seconds 2
#Check again for sessions
$req = Get-WebRequest
}
Write-Host "No Sessions on Box, lets update!"
## Do your stopping here, iisreset /stop etc
#WaitForSessionsAspNet.ps1
$ErrorActionPreference = "Stop"
#Used to get active sessions for a given set of app domains
function Get-ActiveConnections
{
Param(
[string[]]$instanceId
)
if (-not $instanceId)
{
throw "Instance ID required"
}
$results = $instanceId `
| %{ Get-Counter -Counter "\asp.net applications($_)\sessions active" } `
| Select -First 1 -ExpandProperty CounterSamples `
| Measure-Object -Property CookedValue -Sum `
| Select -ExpandProperty Sum
Write-Host "Aggregated value for active sessions: " $results
return $results
}
#Use to get the InstanceName for App Domains hosted in a particular website.
#N.B Relies on Get-Websites and Get-WebAppDomain commands which require the webadministration module to be installed.
function Get-PerfCounterInstanceNameForSite
{
Param(
[string]$siteName
)
if (-not $siteName)
{
throw "siteName required"
}
$website = Get-Website | where { $_.Name -eq $siteName }
if ($website.Length -ne 1)
{
throw "Failed to find site"
}
$appDomain = Get-WebAppDomain `
| where { $_.siteId -eq $website.id } `
| Select -ExpandProperty id `
| ForEach-Object { $_ -replace "/", "_" } `
if ($appDomain.Length -lt 1)
{
Write-Host "App Domain for the site is not running, probably hasn't been hit recently by a request"
return $null
}
return $appDomain
}
#Script starts here
#Get instance names contained in the website. This is an array as one site can contain multiple app domains
$instanceIds = Get-PerfCounterInstanceNameForSite "Default Web Site"
#If there are no app domains for the site stop
if (-not $instanceIds)
{
Write-Host "App Domain isn't running for the site so there are no sessions"
return
}
#Get the number of active sessions for those app domain instances
$numberOfActiveSess = Get-ActiveConnections $instanceIds
#Loop while still active
while ($numberOfActiveSess -gt 0)
{
#Wait
Write-Host "Waiting for request, current inflight: " $numberOfActiveSess
Start-Sleep -Seconds 10
#Update to see if session count has dropped
$numberOfActiveSess = Get-ActiveConnections $instanceIds
}
Write-Host "Requests Drained from Machine"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment