BCD LIB PowerShell cmdlets

Discussion in 'Scripting' started by Dark Dinosaur, Apr 1, 2023.

  1. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,155
    5,962
    150
    #1 Dark Dinosaur, Apr 1, 2023
    Last edited: Apr 3, 2023
    BCD LIB PowerShell cmdlets Module
    it decided to be a module ... ;)

    Code:
    # v1.61
    # 03/04/2023
    # Auther Mr X.Y.Z
    
    Changes
    ~ Update get-vhd-path API problem
    ~ Update add ram drive, new error messege
    
    extract to folder:
    C:\windows\system32\WindowsPowerShell\v1.0\Modules
    
    Export Functions
    "Get_Boot_info"
    "Get-VHD-Path"
    "Is-VHD-System"
    "Add_VHDX_BOOT"
    "Add_PARTITION_BOOT" #NEW
    "Add_RAM_DRIVE_BOOT"
    
    Optional Tags
    #Requires -Version 4
    #Requires -RunAsAdministrator
    #Requires -Modules Microsoft.Windows.BcdLIB.Cmdlets
    
    "Is-VHD-System" ->
    Identify VHD Running System

    "Get-VHD-Path" ->
    For Loaded VHDX / Live OS

    "Add_VHDX_BOOT",
    "Add_RAM_DRIVE_BOOT",
    "Add_PARTITION_BOOT"->
    Update BCD STORE with new Ram / VHDX / Partition Drive

    "Get_Boot_info" ->
    Export 'BCDEDIT /ENUM ALL' as A list of Boot_info Class List

    Boot type Option

    Code:
    enum BootType {
        unknown = 0
        Firmware_Application_101fffff = 1
        Resume_from_Hibernate = 2
        Firmware_Boot_Manager = 3
        Windows_Boot_Manager = 4
        Windows_Boot_Loader = 5
        Windows_Memory_Tester = 6
        EMS_Settings = 7
        Debugger_Settings = 8
        RAM_Defects = 9
        Global_Settings = 10
        Boot_Loader_Settings = 11
        Resume_Loader_Settings = 12
        Hypervisor_Settings = 13
        Device_options = 14
        Windows_Legacy_OS_Loader = 15
    }
    Class Boot_info
    Code:
        # Function
        [bool]Is_vhd()
        [bool]Is_ramdisk()
        [bool]Is_partition()
        [bool]Validate_vhd_Path()
        [int]Remove_ID()
    
        # Properties
        [BootType]$Boot_type
        [int]$hypervisordebugport
        [int]$debugport
        [int]$baudrate
        [int]$hypervisorbaudrate
        [int]$timeout
        [string]$debugtype
        [string]$filedevice
        [string]$filepath
        [string]$identifier
        [string]$device
        [string]$path
        [string]$description
        [string]$locale
        [string]$osdevice
        [string]$inherit
        [string]$recoverysequence
        [string]$displaymessageoverride
        [string]$recoveryenabled
        [string]$isolatedcontext
        [string]$allowedinmemorysettings
        [string]$nx
        [string]$bootmenupolicy
        [string]$systemroot
        [string]$resumeobject
        [string]$hypervisordebugtype
        [string]$hypervisorlaunchtype
        [string]$bootems
        [string]$badmemoryaccess
        [string]$winpe
        [string]$default
        [string]$debugoptionenabled
        [string]$displayorder
        [string]$toolsdisplayorder
        [string]$displaybootmenu
        [string]$detecthal
        [string]$ramdisksdidevice
        [string]$ramdisksdipath
    
     

    Attached Files:

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,155
    5,962
    150
    Some Demo codes for me / you / others / Aliens

    Optimize VHDX & Update Boot

    Code:
    cls
    Write-Host
    
    $ProgressPreference = 'SilentlyContinue'    # Subsequent calls do not display UI.
    
    $info = Get_Boot_info
    $hypr = "D:\Hyper-V"
    
    if (Is-VHD-System) {
      Write-Host
      "ERROR ## VHDX Live OS is A-live, script will stopped"
      Get-VHD-Path
      Write-Host
      pause
      return
    }
    
    "# Force dismount any VHD attched"
    Get-Disk | ? FriendlyName -match 'Msft Virtual Disk' | % {Dismount-VHD -DiskNumber ($_).DiskNumber -ErrorAction SilentlyContinue | Out-Null}
    $Msft = Get-Disk | ? FriendlyName -match 'Msft Virtual Disk';
    if ($Msft -and $Msft[0]) {
      Write-Host
      "ERROR ## Make sure to detach any VHD\X Drive"
      Write-Host
      Pause
      return
    }
    
    "# Clean Any s**ty VHD boot items"
    $info | ? {$_.Is_VHD() -and (!($_.Validate_vhd_Path()))} | % { $_.Remove_ID() | Out-null }
    $vhdx = $info | ? { ($_).Is_vhd() }
    
    cd $hypr
    (dir *.vhdx) | % {
      Write-Host
      "Reduce VHD Size  :: $($_.Name)"
      Resize-VHD -Path $_.Name -ToMinimumSize -ErrorAction SilentlyContinue
      "optimize VHD ..  :: $($_.Name)"
      optimize-vhd -Path $_.Name -Mode Quick -ErrorAction SilentlyContinue
    
      $item = Get-ChildItem $_.Name -ErrorAction SilentlyContinue
      if ($item -and $item.Exists) {
        $count = 0
        $matched = $vhdx | ? { ($_).Get_VHD_Path() -eq $item.FullName }
        $count = $matched.Count
    
        if ($count -eq 0 ) {
          "Install VHD ..   :: $($_.Name)"
          Add_VHDX_BOOT -VHD_File $item.FullName -Name $item.BaseName | out-null
        }
      }
    }
    
    $ProgressPreference = 'Continue'            # Subsequent calls do not display UI.
    Write-Host
    Pause
    return
    Re-Build boot info
    Code:
    $info = Get_Boot_info
    $part = Get-Partition | ? DriveLetter -eq 'c'
    $Msft = Get-Disk | ? FriendlyName -match 'Msft Virtual Disk'
    
    if (Is-VHD-System) {
      Write-Host
      "ERROR ## VHDX Live OS is A-live, script will stopped"
      Get-VHD-Path
      Write-Host
      pause
      return
    }
    
    if ($Msft -and $Msft[0]) {
      Write-Host
      "ERROR ## Make sure to detach any VHD\X Drive"
      Write-Host
      Pause
      return
    }
    
    if ($part -and ($part.PartitionNumber -eq 3)) {
      write-host
      write-host 'Remove additional partition'
      Remove-Partition -DiskNumber $part.DiskNumber -PartitionNumber 4 -Confirm:$false -ea 0
      write-host 'Re-Build Origional Store'
      write-host
      $info | ? Boot_type -eq ([BootType]::Device_options) | % { ($_.Remove_ID()| out-null) }
      $info | ? Boot_type -eq ([BootType]::Windows_Boot_Loader) | % { ($_.Remove_ID()| out-null) }
      $info | ? Boot_type -eq ([BootType]::Windows_Legacy_OS_Loader) | % { ($_.Remove_ID() | out-null) }
      bcdboot /bcdclean | out-null
      bcdboot $env:windir /l en-us | out-null
    }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,155
    5,962
    150
    #3 Dark Dinosaur, Apr 1, 2023
    Last edited: Apr 1, 2023
    (OP)
    Add ram drive
    Code:
    # Case OF Clean ISO Installation
    # Add_RAM_DRIVE_BOOT -Store ??? -Name Windows_10 -Wim_File \Sources\boot.wim -Sdi_File "\Boot\boot.sdi"
    
    # Case local drive ... Live OS ... add Winre for example
    # Add_RAM_DRIVE_BOOT -Name Windows_10 -Wim_File \Sources\boot.wim -Sdi_File "\Boot\boot.sdi" -Sdi_Partition_Letter E
    Clear Device List
    Code:
    Get_Boot_info | ? Boot_type -eq Device_options | % {($_).Remove_ID()}
    
    Search *.VHDX Files & Reduce & Modified Size to minimum
    Also, Add to the boot Store if not exist
    Also, Clean Any bad VHDX Entries

    Code:
    "# Clean Any s**ty VHD boot items"
    Get_Boot_info | ? {$_.Is_VHD() -and (!($_.Validate_vhd_Path()))} | % { $_.Remove_ID() | Out-null }
    $vhdx = Get_Boot_info | ? { ($_).Is_vhd() }
    
    (dir *.vhdx) | % {
      Write-Host 
      "Reduce VHD Size  :: $($_.Name)"
      Resize-VHD -Path $_.Name -ToMinimumSize -ErrorAction SilentlyContinue
      "optimize VHD ..  :: $($_.Name)"
      optimize-vhd -Path $_.Name -Mode Quick -ErrorAction SilentlyContinue
     
      $item = Get-ChildItem $_.Name -ErrorAction SilentlyContinue
      if ($item -and $item.Exists) {
        $count = 0
        $matched = $vhdx | ? { ($_).Get_VHD_Path() -eq $item.FullName }
        $count = $matched.Count
      
        if ($count -eq 0 ) {
          "Install VHD ..   :: $($_.Name)"
          Add_VHDX_BOOT -VHD_File $item.FullName -Name $item.BaseName | out-null
        }
      }
    }
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,155
    5,962
    150
    Now added option to add boot from a partition
    using bcdedit command [not bcdboot]
    So it now covers ... Ram / VHD[X] / Partition
    All option is available now!

    Code:
    # Add windows 10 from Drive C
    Add_PARTITION_BOOT 'Windows 10' C
    
    # Add windows 11 from Drive D
    Add_PARTITION_BOOT -Name 'Windows 11' -Letter D
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...