File: reboot_detect.ps1

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (47 lines) | stat: -rw-r--r-- 1,087 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Function to check whether machine is currently shutting down
function ShuttingDown {
    [string]$sourceCode = @"
using System;
using System.Runtime.InteropServices;

namespace Vagrant {
    public static class RemoteManager {
        private const int SM_SHUTTINGDOWN = 0x2000;

        [DllImport("User32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetSystemMetrics(int Index);

        public static bool Shutdown() {
            return (0 != GetSystemMetrics(SM_SHUTTINGDOWN));
        }
    }
}
"@
    $type = Add-Type -TypeDefinition $sourceCode -PassThru
    return $type::Shutdown()
}

if (ShuttingDown) {
  exit 1
} else {
  # See if a reboot is scheduled in the future by trying to schedule a reboot
  . shutdown.exe -f -r -t 60

  if ($LASTEXITCODE -eq 1190) {
    # reboot is already pending
    exit 2
  }

  if ($LASTEXITCODE -eq 1115) {
    # A system shutdown is in progress
    exit 2
  }

  # Remove the pending reboot we just created above
  if ($LASTEXITCODE -eq 0) {
    . shutdown.exe -a
  }
}

# no reboot in progress or scheduled
exit 0