Sure. These are my first pair of PowerShell scripts, a batch file, plus a Windows Scheduler task that runs every 5 minutes. It does it's job and has kept us running.
ReadDynamicsEventLog2.ps1
# David Morinello 04/19/2016
$Event = get-eventlog -logname Dynamics -EntryType Error -After (Get-Date).AddMinutes(-5) | where-object {$_.Message -like "*Exception from HRESULT*"}
If ($Event.length -gt 0) {
#Write-Output "Event Exists"
cmd.exe /c '\Automations\RestartGPWebServices.bat'
}
#Else {"No Event"}
Exit
--------------------------------------------
RestartGPWebServices.bat
Net Stop DynGPWebService
Timeout /t 10 /nobreak
REM Alternate killer of the GP Web Service (DPM)
Echo Off
FOR /F "tokens=3" %%A IN ('sc queryex DynGPWebService ^| findstr PID') DO (SET pid=%%A)
IF "!pid!" NEQ "0" (
taskkill /F /PID %pid%
)
Timeout /t 10 /nobreak
Net Start DynGPWebService
Timeout /t 10 /nobreak
Powershell.exe -executionpolicy remotesigned -File C:\Automations\RestartGPWebService_Email.ps1
----------------------------------------
RestartGPWebService_Email.ps1
# David Morinello 04/19/2016
send-mailmessage -to "David.Morinello <David.Morinello@eMAILSERVER.COM.com>" -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"
send-mailmessage -to "David.Morinello <DMorinelloii@yahoo.com>" -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"
#send-mailmessage -to "John.Steyer <John.Steyer@eMAILSERVER.COM.com>" -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"
send-mailmessage -to "CorpAppsAlerts@eMAILSERVER.COM.opsgenie.net" -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"
----------------------
Windows Task Scheduler Task
Calls a PowerShell script ReadDynamicsEventLog2.ps1 to check the Dynamics Event log for a "Handle is invalid" error. PS Script calls RestartGPWebServices.bat batch if true. Emails are handled by RestartGPWebService_Email.ps1