Hi,
Having tried, I think, every suggestion around this topic, I cannot get it to work... aarrggghhh!
I'd like to run a PowerShell script on a remote node when an alert for Java Out Of Memory occurs. This is for Tomcat in Windows. As you may know, simply restarting the service will not work, the OOM error still exists. You need to kill the Tomcat process and then start the service.
I create a PowerShell script to do this. It works perfectly when running locally on the remote node. I'll post it below.
With regard to the alert, well... talk about little error information! I have tried so may different network path commands I think I'm repeating myself now. I cannot quite fathom if the script should live on the SW server or the remote node. I have tried both without success so I'm hoping anyone that has this working similar to my scenario, how they got this working.
Network command paths I've tried:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Invoke-Command -ComputerName '${N=SwisEntity;M=Caption}' "C:\Middleware\RestartTomcat - Copy.ps1"
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Invoke-Command -File '${N=SwisEntity;M=Caption}' "C:\Middleware\RestartTomcat - Copy.ps1"
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -Command "'C:\Middleware\RestartTomcat - Copy.ps1'" "${N=SwisEntity;M=Caption}"
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -Command "C:\Middleware\RestartTomcat - Copy.ps1" "${N=SwisEntity;M=Caption}"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "\\servername\D$\selfhealing\RestartTomcat.ps1"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy bypass "\\servername\D$\selfhealing\RestartTomcat.ps1"
I've tried with 32 and 64 bit PowerShell and many more iterations of the above that I've forgotten.
I'd love to hear from anyone that has this working or can offer any advice.
PowerShell (could not add code tags for some reason, sorry)
param (
[string]$nodeName
)
# Define the Tomcat service name
$serviceName = "Tomcat8B" # Change this to your actual Tomcat service name
# Define the log file path
$logDirectory = "D:\selfhealing" # Change this to your desired log directory
$logFile = Join-Path -Path $logDirectory -ChildPath "TomcatRestart.log"
# Function to log messages
function Log-Message {
param (
[string]$message
)
# Ensure the log directory exists
if (-not (Test-Path -Path $logDirectory)) {
New-Item -Path $logDirectory -ItemType Directory | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - $message"
Add-Content -Path $logFile -Value $logEntry
}
# Log the start of the script
Log-Message "Script execution started for node: $nodeName."
# Get the PID of the Tomcat service
try {
$servicePID = (Get-WmiObject Win32_Service -ComputerName $nodeName -Filter "Name='$serviceName'").ProcessId
Log-Message "Found Tomcat service PID: $servicePID"
} catch {
Log-Message "Failed to get Tomcat service PID. Error: $_"
exit 1
}
# Check if a PID was found
if ($servicePID) {
# Kill the Tomcat process
try {
Stop-Process -Id $servicePID -Force
Log-Message "Tomcat process with PID $servicePID killed."
} catch {
Log-Message "Failed to kill Tomcat process with PID $servicePID. Error: $_"
exit 1
}
} else {
Log-Message "Tomcat process not found."
exit 1
}
# Wait for a short period to ensure the service manager updates
Start-Sleep -Seconds 5
Log-Message "Waited 5 seconds after killing the process."
# Restart the Tomcat service
try {
Start-Service -ComputerName $nodeName -Name $serviceName
if ((Get-Service -ComputerName $nodeName -Name $serviceName).Status -eq 'Running') {
Log-Message "Tomcat service restarted successfully."
} else {
Log-Message "Failed to restart Tomcat service."
exit 1
}
} catch {
Log-Message "Error restarting Tomcat service. Error: $_"
exit 1
}
# Log the end of the script
Log-Message "Script execution completed for node: $nodeName."