1

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 - enter image description here

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?

2
  • 1
    This seems like something that should be handled at the OS level. Like perhaps a scheduled task that reads the event log. Commented Nov 17, 2024 at 3:17
  • Makes sense, will try scheduled task. Thanks a lot Michael. Commented Nov 17, 2024 at 16:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.