我的PowerShell代码看到该进程正在运行,并且每60秒就会写入一条消息,说明该进程正在运行-一切正常。当我杀死进程时,它每60秒就会写一次“进程正在运行”-当进程被淘汰时。 不会在应用程序日志中写入事件的逻辑有什么问题-如果我启动脚本并且启动时进程未运行,则该逻辑起作用。
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)
您处于while真实的无限循环中:
$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
只要进程存在,此版本就会运行,并且在不存在时写入事件日志。