| 1 |
# Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 |
# Use of this source code is governed by a BSD-style license that can be |
| 3 |
# found in the LICENSE file. |
| 4 |
|
| 5 |
# Usage: win_run_and_check_log.ps1 <cmd> <args> |
| 6 |
# Runs the given command, prints the exit code, and prints any application |
| 7 |
# crashes found in the Windows Application Event Log. If the command fails, |
| 8 |
# return exit code 1; if any crashes, return exit code 2; otherwise, return exit |
| 9 |
# code 0. |
| 10 |
|
| 11 |
$ErrorActionPreference = 'Stop' |
| 12 |
|
| 13 |
$begin = Get-Date |
| 14 |
|
| 15 |
# Run the command passed to the script. |
| 16 |
& $args[0] $args[1..($args.length - 1)] |
| 17 |
|
| 18 |
$res = $LastExitCode |
| 19 |
|
| 20 |
Write-Host "$($args[0]) exited with status $res" |
| 21 |
|
| 22 |
# dm sometimes exits with negative exit codes, e.g. -1073741569, which in some |
| 23 |
# cases cmd treats as 0. Ensure any non-zero are OK for cmd. |
| 24 |
if ($res -ne 0) { |
| 25 |
$res = 1 |
| 26 |
} |
| 27 |
|
| 28 |
$end = Get-Date |
| 29 |
|
| 30 |
# "-InstanceId 1000" means "Application Error". |
| 31 |
# We use "-ErrorAction SilentlyContinue" because Get-EventLog raises an error if |
| 32 |
# no logs are found. |
| 33 |
$faults = Get-EventLog -LogName Application -InstanceId 1000 -EntryType Error ` |
| 34 |
-After $begin -Before $end -ErrorAction SilentlyContinue |
| 35 |
|
| 36 |
if ($faults) { |
| 37 |
Write-Host 'First fault message:' |
| 38 |
Write-Host $faults[0].Message |
| 39 |
if ($res -eq 0) { |
| 40 |
$res = 2 |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
# https://stackoverflow.com/a/50202663 |
| 45 |
if ($res -ne 0) { |
| 46 |
$host.SetShouldExit($res) |
| 47 |
exit $res |
| 48 |
} |