Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell脚本检查是否在出错时运行进程_Powershell - Fatal编程技术网

使用PowerShell脚本检查是否在出错时运行进程

使用PowerShell脚本检查是否在出错时运行进程,powershell,Powershell,我的PowerShell代码看到该进程正在运行,每60秒就会写一条消息说它正在运行——这工作正常。当我终止进程时,它会每六十秒写一次“进程正在运行”——当进程被删除时。 不在应用程序日志中写入事件的逻辑有什么问题?如果我启动脚本,并且进程在启动时未运行,则该逻辑会起作用 while($true) { If (( $(Get-WmiObject Win32_Process |findstr "MoveFilesToServer") -eq $null )) { Write-EventLo

我的PowerShell代码看到该进程正在运行,每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
}

您处于一个无限循环中,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

只要进程存在,此版本就会运行,如果不存在,则写入事件日志。

进程名称显示为“cmd.exe”-MoveFileToServer出现在命令行中,这就是我使用FindStr区分句柄NPM(K)PM(K)WS(K)CPU的原因Id SI ProcessName---------------55 5 576 2724 6940 0选择31 3 1508 2084 2108 0 cmd 38 4 1628 2756 0.03 6932 5 cmd 40 4 2164 3288 12324 0 cmdfirst-我需要说,这是powershell脚本将在任何时间运行时启动-循环检查进程以查看它是否正在运行-如果没有写入事件日志。当我运行我最初创建的脚本时-它正在交付进程正在运行。当我使用@gwmArgs运行该版本时-并使用写主机显示进程正在运行-它正在将错误写入事件日志-进程正在运行。是否计划在脚本中重新启动进程?还是要无限地填充事件日志?如果进程实际包含该命令行,则会找到它@配置我正在检查运行状态的进程在系统启动时通过批处理文件启动-我希望它调用此检查器脚本进行自我检查-它将持续写入事件日志-但我们的事件管理软件只会通知我们一次-然后由技术支持人员重新启动