:loop
>nul timeout /t 600 /nobreak
powershell -ExecutionPolicy Unrestricted -c "Get-Process -Name programm | Where-Object -FilterScript {$_.Responding -eq $false}"
if not errorlevel 1 goto loop
这不起作用,我认为错误级别是问题,但我无法解决。 我想检查过程是否正在回答。如果没有,我想在超时后再次检查该过程。
在此先感谢您的帮助。
答案 0 :(得分:0)
几乎所有应用程序和实用程序都将在退出代码时设置 他们完成/终止。设置的退出代码确实有所不同, 通常,代码
0
(错误)将指示成功完成。
…
由CMD.EXE
运行外部命令时,它将检测到 可执行文件的 Return 或 Exit Code 并将ERRORLEVEL
设置为 比赛。在大多数情况下,ERRORLEVEL
与 Exit相同 代码,但在某些情况下它们可能会有所不同。
它是PowerShell
的退出代码,如以下示例所示:
errorlevel
1):==> powershell -noprofile -c "Get-Process -Name invalid_programm"
Get-Process : Cannot find a process with the name "invalid_programm". Verify the process name and call the cmdlet again.
At line:1 char:1
+ Get-Process -Name invalid_programm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (invalid_programm:String) [Get-Process],ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
==> echo errorlevel=%errorlevel%
errorlevel=1
errorlevel
0)==> powershell -noprofile -c "return 1"
1
==> echo errorlevel=%errorlevel%
errorlevel=0
exit
keyword明确设置)errorlevel
因此,您可以使用以下代码段(应始终成功完成):
==> powershell -noprofile -c "exit 2"
==> echo errorlevel=%errorlevel%
errorlevel=2
将以上内容重写为单行代码(使用别名),您可能会遇到以下情况:
try {
$x = @(Get-Process -Name programm -ErrorAction Stop |
Where-Object -FilterScript {-not $_.Responding})
exit $x.Count
} catch {
exit 5000 # or other `absurd` value
}
==> powershell -noprofile -c "try{$x=@(gps programm -EA Stop|? {-not $_.Responding});exit $x.Count} catch {exit 5000}"
==> echo errorlevel=%errorlevel%
errorlevel=5000
==> powershell -noprofile -c "try{$x=@(gps cmd -EA Stop|? {-not $_.Responding});exit $x.Count} catch {exit 5000}"
==> echo errorlevel=%errorlevel%
errorlevel=0
==> powershell -noprofile -c "try{$x=@(gps HxOutlook -EA Stop|? {-not $_.Responding});exit $x.Count} catch {exit 5000}"
==> echo errorlevel=%errorlevel%
errorlevel=1
请注意上述枚举的不完整性:我们可以想象场景,其中运行更多具有指定名称的进程,其中一些响应,而其他不响应。
(换句话说,发现并没有响应 并不暗示,表示不存在另一个同名的发现并没有响应 …)