Skip to content

Instantly share code, notes, and snippets.

@devhawk
Created February 11, 2016 18:52
Show Gist options
  • Select an option

  • Save devhawk/0fac19af5c038c6ed2c6 to your computer and use it in GitHub Desktop.

Select an option

Save devhawk/0fac19af5c038c6ed2c6 to your computer and use it in GitHub Desktop.
param([Parameter(Mandatory=$true)][string]$filename, [string]$displayname)
$local:windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$local:windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $local:windowsIdentity
if ($local:windowsPrincipal.IsInRole("Administrators") -ne 1)
{
throw "add-bcd-vhd must be run from an administrator account"
}
$f = resolve-path $filename
if ([string]::IsNullOrEmpty($displayname))
{
$displayname = [IO.Path]::GetFileNameWithoutExtension($f)
}
$vhdvalue = "[$($f.Drive.Name):]$($f.Path.Substring(2))"
$rx = [regex] "\{\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}"
$r = bcdedit /copy '{current}' /d $displayname
$m = $rx.match($r)
if ($m.success)
{
$guid = $m.value
$ret = bcdedit /set $guid device vhd=$vhdvalue
if ($ret -ne "the operation completed successfully.")
{
throw $ret
}
$ret = bcdedit /set $guid osdevice vhd=$vhdvalue
if ($ret -ne "the operation completed successfully.")
{
throw $ret
}
$ret = bcdedit /set $guid detecthal on
if ($ret -ne "the operation completed successfully.")
{
throw $ret
}
echo "$displayname ($(split-path -leaf $f)) VHD Boot Configured"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment