I needed to execute a script that should send an email whenever AppPool Recycle event occurs in the server - came with below script that check whether AppPool recycled in every 10 minutes.
$startTime = Get-Date
$appPoolName = "cm-local.website"
function Check-AppPoolRecycle {
$events = Get-WinEvent -LogName "System" -FilterXPath "*[System[(EventID=5074)]]" |
Where-Object { $_.Message -like "*$appPoolName*" -and $_.TimeCreated -ge $startTime } |
Select-Object TimeCreated, Id, Message
foreach ($event in $events) {
Write-Output "The application pool '$appPoolName' has been recycled at $($event.TimeCreated)"
#Send Email
}
}
# Monitor the app pool recycle events
while ($true) {
Check-AppPoolRecycle
Start-Sleep -Seconds 600 # Adjust the interval as needed
}
I put the script in Persistent session using following script -
$scriptPath = "master:/sitecore/system/Modules/PowerShell/Script Library/AppPoolRecycle"
Start-ScriptSession -Path $scriptPath -Id "PersistentSession1" -Interactive
I could see that it's running the Persistent Session in PowerShellBackground Session Manager -

The problem is when AppPool Recycle happens, the session doesn't persistent - it goes away. How do I make the session persistent regardless of AppPool recycle?