-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrestore-boot-order.ps1
More file actions
72 lines (60 loc) · 2.38 KB
/
Copy pathrestore-boot-order.ps1
File metadata and controls
72 lines (60 loc) · 2.38 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
$erroractionpreference = 'stop'
# Windows Boot Manager puts itself back at the front of the firmware boot order
# on every boot, so setting the order alone never survives to the next one and
# the machine boots straight past GRUB into Windows. Firmware updates and in
# place upgrades do the same thing, they are just not the common case.
#
# BootNext is a one shot override that the firmware consumes and clears, and it
# takes precedence over the boot order, so setting it on every boot is what
# actually guarantees the next one reaches GRUB. The order is put back as well,
# so that a boot which does not run this first, e.g. straight after a firmware
# update, still has a chance of landing in the right place.
#
# The entry is looked up by description rather than by its identifier, because
# an entry that has been recreated rather than reordered has a new one.
$wanted = 'Fedora'
$lines = bcdedit /enum firmware
# Map each entry identifier to its description.
$descriptions = @{}
$identifier = $null
foreach ($line in $lines) {
if ($line -match '^identifier\s+(\{[^}]+\})') {
$identifier = $matches[1]
}
elseif ($identifier -and $line -match '^description\s+(.+?)\s*$') {
$descriptions[$identifier] = $matches[1]
$identifier = $null
}
}
# The first displayorder in the output is the one belonging to {fwbootmgr},
# which is the firmware boot order. {bootmgr} has one of its own further down.
$order = @()
$inorder = $false
foreach ($line in $lines) {
if (-not $inorder -and $line -match '^displayorder\s+(\{[^}]+\})') {
$inorder = $true
$order += $matches[1]
}
elseif ($inorder) {
if ($line -match '^\s+(\{[^}]+\})\s*$') { $order += $matches[1] }
else { break }
}
}
$target = $descriptions.keys |
where-object { $descriptions[$_] -eq $wanted } |
select-object -first 1
if (-not $target) {
"No firmware boot entry called $wanted, leaving the boot order alone."
return
}
# bcdedit calls BootNext the bootsequence of {fwbootmgr}. This is the part that
# matters, so it is done unconditionally.
"Setting the next boot to $wanted ..."
bcdedit /set '{fwbootmgr}' bootsequence $target
if ($order[0] -eq $target) {
"$wanted is already first in the firmware boot order."
}
else {
"Moving $wanted to the front of the firmware boot order ..."
bcdedit /set '{fwbootmgr}' displayorder $target /addfirst
}