0

My PowerShell code is seeing the process run and every 60 seconds writes a message saying its running - that's working fine. When I kill the process it keeps writing the "process is running" every sixty seconds - when process has been eliminated. What is wrong with the logic that would not write an event in the App log - it works if I launch the script and the process is not running at the time of launch.

while($true) {
If (( $(Get-WmiObject Win32_Process |findstr "MoveFilesToServer") -eq $null )) 
{
    Write-EventLog -LogName Application -Source "MoveFilesToServer" -eventID 0018 -EntryType Error -Message "Move files process not running"

}
else { Write-Host 'process is running' } 
Start-Sleep -Seconds 60
}
0

1 Answer 1

1

You're in an infinite loop with while true:

$gwmiArgs = @{
    Class       = 'Win32_Process'
    Filter      = 'CommandLine LIKE "%MoveFilesToServer%"'
    ErrorAction = 'Ignore'
}
while (Get-WmiObject @gwmiArgs) {
    'Process is running.'
    Start-Sleep -Seconds 60
}

$writeEventLogArgs = @{
    LogName   = 'Application'
    Source    = 'MoveFilesToServer'
    EventID   = 18
    EntryType = 'Error'
    Message   = 'Move files process not running!'
}
Write-EventLog @writeEventLogArgs

This version will run as long as the process exists and writes to the event log when it doesn't.

Sign up to request clarification or add additional context in comments.

7 Comments

The Process Name appears as 'cmd.exe' - the MoveFilesToServer appears in the Commandline which is the reason I've used the FindStr to differentiate Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 55 5 576 2724 6940 0 choice 31 3 1508 2084 2108 0 cmd 38 4 1628 2756 0.03 6932 5 cmd 40 4 2164 3288 12324 0 cmd
first - I need to say that this is powershell script will be launched any running all the time - looping through to check for the process to see if it's running - and if not write to the event log.
when I run the script I originally created - it is delivering Process is Running. When I run the version using the @gwmArgs - and using the Write-Host to show Process is Running - it is writing an error to the Event Log - when the process is running.
Do you plan on restarting the process in the script? or do you want to infinitely populate the event log? If the process actually contains that commandline, then it will be found. @Configueroa
the process I'm checking for a run status is launched at system startup via a batch file - I would like to have it call this checker script to check itself - it will continually write to the event log - but our event management software will notify us only once - then it will be restarted by a tech support person
|

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.