-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-BCDEntry.ps1
181 lines (156 loc) · 5.06 KB
/
New-BCDEntry.ps1
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function New-BCDEntry
{
<#
.SYNOPSIS
Add a New OS-Entry to the BCD-Store.
.DESCRIPTION
For new OS-Entries, this function can be used. It adds new OS-Entrys to the
bcd-Store using the bcdedit /create command and adding the missing values.
.EXAMPLE
Add-BCDEntry -VHDPath c:\vhd\winpe.vhdx
Returns the GUID of the Currently Running OS.
.NOTES
Author: Holger Voges
Version: 1.0.1
Date: 2019-05-06
.LINK
URLs to related sites
The first link is opened by Get-Help -Online Get-BCDCurrentOSGuid
.INPUTS
List of input types that are accepted by this function.
.OUTPUTS
List of output types produced by this function.
#>
[cmdletBinding()]
param(
# Determines wether to boot winload.efi or winload.exe
# If booted from Windows PE, the Firmware-Type can be determined from Registry
[String]
[ValidateSet('UEFI','BIOS')]
$FirmwareType,
# Path to the bootable vhd
[Parameter(mandatory = $true,
ParameterSetName = 'VHD',
ValueFromPipelineByPropertyName = $true
)]
[validatescript({
Test-Path $_ -Type leaf
})]
$vhdPath,
# Driveletter of Operating-System Partition
[Parameter(mandatory = $true,
ParameterSetName = 'OSDrive',
ValueFromPipelineByPropertyName = $true
)]
[ValidatePattern('^([C-Zc-z]:?)$')]
[String]$OSDriveLetter,
# Description for the bootmenu
[Parameter()]
$description,
# Sets the new Boot-Entry as the Default OS to Boot
[switch]$MakeDefault,
# Has to be set if the new OS is Windows PE
[Switch]$WinPE,
# If you want to disable Hyper-V for any reason
[switch]$disableHyperV,
# Forces Detection of Hardware Abstraction Layer on Bootup
[switch]
$detecthal,
# Switches back to legacy Boot Menu which is displayed before OS Bootup
[switch]
$LegacyBootMenu,
# Set the No Execution Features
[string]
[ValidateSet('OptIn','OptOut','AlwaysOn','AlwaysOff')]
$NoExecuteSetting = 'OptOut',
# Add the new Boot-Entry at the Beginning of the OS-List. Default is to put it at the end
[switch]
$FirstBootEntry
)
process
{
# Deklarationsblock
[regex]$guidpattern = '(\{[\dabcdef]{8}-[\dabcdef]{4}-[\dabcdef]{4}-[\dabcdef]{4}-[\dabcdef]{12}\})'
if ( -not $description )
{
$description = ( Get-ChildItem -Path $vhdPath ).BaseName
}
If ( -not $FirmwareType )
{
$FirmwareType = Get-BCDFirmWareType
}
If ( $PsCmdlet.ParameterSetName -eq 'OsDrive' )
{
If ( $OSdriveLetter.Length -eq 1 )
{
$OSdriveLetter += ':'
}
if (-not ( Test-Path $OSdriveLetter ))
{
Throw 'Das Bootlaufwerk konnte nicht gefunden werden'
}
Else
{
$OSDrive = 'partition={0}' -f $OSDriveLetter
}
}
# Anlegen eines neuen Betriebssystem-Eintrags und speichern der neuen GUID
$NewBcd = bcdedit.exe /create /d $description /application osloader
$null = $NewBcd -match $guidpattern
$NewBcdGuid = $Matches[0]
if ( $PsCmdlet.ParameterSetName -eq 'vhd' )
{
$driveletter = $vhdPath.substring(0,2)
$vhd = 'vhd=' + '[' + $driveletter + ']' + $vhdPath.Substring(2)
$null = bcdedit.exe /set $NewBcdGuid OSDevice $vhd
$null = bcdedit.exe /set $NewBcdGuid Device $vhd
}
Elseif ( $PsCmdlet.ParameterSetName -eq 'OSDrive' )
{
$null = bcdedit.exe /set $NewBcdGuid OSDevice $OSDrive
$null = bcdedit.exe /set $NewBcdGuid Device $OSDrive
}
if ( $FirmwareType -eq 'UEFI' )
{
$null = bcdedit.exe /set $NewBcdGuid Path '\Windows\system32\winload.efi'
}
Else
{
$null = bcdedit.exe /set $NewBcdGuid Path '\Windows\system32\winload.exe'
}
$null = bcdedit.exe /set $NewBcdGuid Description $description
$null = bcdedit.exe /set $NewBcdGuid systemroot '\windows'
$null = bcdedit.exe /set $NewBcdGuid inherit '{6efb52bf-1766-41db-a6b3-0ee5eff72bd7}'
$null = bcdedit.exe /set $NewBcdGuid nx $NoExecuteSetting
foreach ( $param in $PSBoundParameters )
{
switch ( $param.keys )
{
'WinPE' {
$null = bcdedit.exe /set $NewBcdGuid winpe yes
$null = bcdedit.exe /set $NewBcdGuid detecthal yes
}
'detectHal' { $null = bcdedit.exe /set $NewBcdGuid detecthal yes }
'LegacyBootMenu' { $null = bcdedit.exe /set $NewBcdGuid bootmenupolicy Legacy }
'MakeDefault' { $null = bcdedit.exe /default $NewBcdGuid }
}
}
If ( $DisableHyperVisor)
{
$null = bcdedit.exe /set $NewBcdGuid hypervisorlaunchtype off
}
Else
{
$null = bcdedit.exe /set $NewBcdGuid hypervisorlaunchtype Auto
}
If ( $FirstBootEntry )
{ $null = bcdedit.exe /displayorder $NewBcdGuid /addfirst }
Else
{
$null = bcdedit.exe /displayorder $NewBcdGuid /addlast
}
[PSCustomObject][ordered]@{
Identifier = $NewBcdGuid
}
}
}