forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSSession-Cleanup.ps1
14 lines (13 loc) · 947 Bytes
/
PSSession-Cleanup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$scriptBlock = {
while ([Dataplat.Dbatools.Connection.ConnectionHost]::PSSessionCountExpired -gt 0) {
$session = $null
$session = [Dataplat.Dbatools.Connection.ConnectionHost]::PSSessionPurgeExpired()
if ($null -ne $session) { $session | Remove-PSSession }
}
}
Register-DbaMaintenanceTask -Name "pssession_cleanup" -ScriptBlock $scriptBlock -Delay (New-TimeSpan -Minutes 1) -Priority Low -Interval (New-TimeSpan -Minutes 1)
# Cleans up local references in the current runspace. All actual termination logic is handled by the task above
$script:pssession_cleanup_timer = New-Object System.Timers.TImer
$script:pssession_cleanup_timer.Interval = 60000
$null = Register-ObjectEvent -InputObject $script:pssession_cleanup_timer -EventName elapsed -SourceIdentifier dbatools_Timer -Action { Get-PSSession | Where-Object State -Like Closed | Remove-PSSession } -ErrorAction Ignore
$script:pssession_cleanup_timer.Start()