Note: The script is functional; however, the underlying issue was that PeakHMI was shutting down due to mistakenly detecting the removal of the USB key. This occurred because the computer's USB ports were entering sleep mode.
This script manages the operation of a program related to PeakHMI by checking its current mode and acting accordingly. Here's a summary of what it does:
Check if PeakHMI is in Runtime Mode:
It checks if the process named "Runtime" is running.
If "Runtime" is detected, the script prints a message indicating that PeakHMI is in Runtime mode and no further action is needed. The script then exits.
Check if PeakHMI is in Program Mode:
If "Runtime" is not running, it checks if the process named "Configure" is running.
If "Configure" is detected, the script attempts to stop this process. If successful, it prints a message that PeakHMI is being restarted, waits for 2 seconds, and continues.
If stopping the "Configure" process fails, an error message is printed, and the script exits.
Start the PeakHMI Program:
After handling the above cases, the script tries to start the "Configure.exe" program using its specified path.
If the program starts successfully, a message is printed indicating that PeakHMI is now starting.
If the program fails to start, an error message is printed, and the script exits.
This ensures that if PeakHMI is in the correct mode, no action is taken. If it's not in Runtime mode, the script attempts to restart it.
Call RunScriptSilently.vbs for Task Scheduler
C:\Scripts\RunScriptSilently.vbs
Set objShell = CreateObject("WScript.Shell")
objShell.Run "powershell.exe -ExecutionPolicy Bypass -File ""C:\Scripts\ManagePeakHMIProcess.ps1""", 0, False
C:\Scripts\ManagePeakHMIProcess.ps1
$runtimeProcessName = "Runtime" # Process name for Runtime mode
$configureProcessName = "Configure" # Process name for Configure mode
$programPath = "C:\Program Files (x86)\Everest\Configure.exe" # Full path to the program
# Check if Runtime is running
$runtimeProcess = Get-Process -Name $runtimeProcessName -ErrorAction SilentlyContinue
if ($runtimeProcess) {
Write-Host "PeakHMI is in Runtime mode. No action required."
exit 0
}
# If Runtime is not running, check for Configure
$configureProcess = Get-Process -Name $configureProcessName -ErrorAction SilentlyContinue
if ($configureProcess) {
try {
# Kill the Configure process
Stop-Process -Id $configureProcess.Id -Force
Write-Host "PeakHMI is in program mode. Restarting PeakHMI."
# Wait for 2 seconds
Start-Sleep -Seconds 2
} catch {
Write-Host "Failed to stop PeakHMI in program mode. Error: $_"
exit 1
}
}
# Start Configure.exe
try {
Start-Process $programPath
Write-Host "PeakHMI is not running. Starting PeakHMI now."
} catch {
Write-Host "Failed to start PeakHMI. Error: $_"
exit 1
}