使用 .NET(PowerShell 或 .NET)获取 BCD 条目

Getting BCD entries with .NET (PowerShell or .NET)

我正在创建一个应用程序来分析引导配置数据 (BCD) 中的条目。

我尝试过使用 PowerShell,但它似乎没有提供任何 cmdlet 来处理它。所以,我又回到了 .NET,尤其是 C#。

我想要像这样获取 BCD 条目的东西

var entries = bcd.GetEntries();

条目是 IList<BcdEntry>

class BcdEntry
{
    public string Name {get; set; }
    IDictionary<string, IList<string>> Properties { get; set; }
}

问题是我不知道如何获取条目。调用 BCDEdit 是可能的,但它需要解析命令的输出,这是一项繁琐的任务。

希望你能想出解决我问题的办法。

bcdedit.exe /enum 输出解析为自定义 objects:

列表的 PSv4+ 解决方案
# IMPORTANT: bcdedit /enum requires an ELEVATED session.
$bcdOutput = (bcdedit /enum) -join "`n" # collect bcdedit's output as a *single* string

# Initialize the output list.
$entries = New-Object System.Collections.Generic.List[pscustomobject]]

# Parse bcdedit's output.
($bcdOutput -split '(?m)^(.+\n-)-+\n' -ne '').ForEach({
  if ($_.EndsWith("`n-")) { # entry header 
    $entries.Add([pscustomobject] @{ Name = ($_ -split '\n')[0]; Properties = [ordered] @{} })
  } else {  # block of property-value lines
    ($_ -split '\n' -ne '').ForEach({
      $propAndVal = $_ -split '\s+', 2 # split line into property name and value
      if ($propAndVal[0] -ne '') { # [start of] new property; initialize list of values
        $currProp = $propAndVal[0]
        $entries[-1].Properties[$currProp] = New-Object Collections.Generic.List[string]
      }
      $entries[-1].Properties[$currProp].Add($propAndVal[1]) # add the value
    })
  }
})

# Output a quick visualization of the resulting list via Format-Custom
$entries | Format-Custom

注:

  • LotPing观察,

    • bcdedit.exe 输出 部分本地化;具体而言,包括以下项目:
      • 条目 headers(例如,英语 Windows Boot Manager 在西班牙语中是 Administrador de arranque de Windows
      • 奇怪的是,属性 的英文名字也是 identifier(例如,西班牙语中的 Identificador)。
    • 为了简洁起见,代码没有尝试将本地化名称映射到它们的 US-English 对应名称,但可以做到。

    • 此外,与 this ServerFault question 一起发布的示例 bcdedit 输出(重复)表明可能 属性 名称太长以至于它们 运行 转换成它们的值,中间没有空格,也没有 t运行cation.
      如果这不仅仅是发布的产物,则需要做更多的工作来处理这种情况; this article 包含 属性 个名称的列表。

  • [pscustomobject] 实例被使用而不是自定义实例 BcdEntry class;在 PSv5+ 中,您可以直接在 PowerShell 中创建这样的自定义 class。

  • 属性 值都被捕获为 string 值,收集在 [List[string]] 列表中(即使只有 1 个值);需要额外的工作才能将它们解释为特定类型;
    例如,[int] $entries[1].Properties['allowedinmemorysettings'][0] 将字符串 '0x15000075' 转换为整数。


示例输入/输出:

给定 bcdedit.exe /enum 这样的输出...

Windows Boot Manager
--------------------
identifier              {bootmgr}
device                  partition=C:
displayorder            {current}
                        {e37fc869-68b0-11e8-b4cf-806e6f6e6963}
description             Windows Boot Manager
locale                  en-US
inherit                 {globalsettings}
default                 {current}
resumeobject            {9f3d8468-592f-11e8-a07d-e91e7e2fad8b}
toolsdisplayorder       {memdiag}
timeout                 0

Windows Boot Loader
-------------------
identifier              {current}
device                  partition=C:
path                    \WINDOWS\system32\winload.exe
description             Windows 10
locale                  en-US
inherit                 {bootloadersettings}
recoverysequence        {53f531de-590e-11e8-b758-8854872f7fe5}
displaymessageoverride  Recovery
recoveryenabled         Yes
allowedinmemorysettings 0x15000075
osdevice                partition=C:
systemroot              \WINDOWS
resumeobject            {9f3d8468-592f-11e8-a07d-e91e7e2fad8b}
nx                      OptIn
bootmenupolicy          Standard

...上面的命令产生这个:

class PSCustomObject
{
  Name = Windows Boot Manager
  Properties = 
    [
      class DictionaryEntry
      {
        Key = identifier
        Value = 
          [
            {bootmgr}
          ]

        Name = identifier
      }
      class DictionaryEntry
      {
        Key = device
        Value = 
          [
            partition=C:
          ]

        Name = device
      }
      class DictionaryEntry
      {
        Key = displayorder
        Value = 
          [
            {current}
            {e37fc869-68b0-11e8-b4cf-806e6f6e6963}
          ]

        Name = displayorder
      }
      class DictionaryEntry
      {
        Key = description
        Value = 
          [
            Windows Boot Manager
          ]

        Name = description
      }
      ...
    ]

}

class PSCustomObject
{
  Name = Windows Boot Loader
  Properties = 
    [
      class DictionaryEntry
      {
        Key = identifier
        Value = 
          [
            {current}
          ]

        Name = identifier
      }
      class DictionaryEntry
      {
        Key = device
        Value = 
          [
            partition=C:
          ]

        Name = device
      }
      class DictionaryEntry
      {
        Key = path
        Value = 
          [
            \WINDOWS\system32\winload.exe
          ]

        Name = path
      }
      class DictionaryEntry
      {
        Key = description
        Value = 
          [
            Windows 10
          ]

        Name = description
      }
      ...
    ]

}

以编程方式处理条目:

foreach($entry in $entries) { 
  # Get the name.
  $name = $entry.Name
  # Get a specific property's value.
  $prop = 'device'
  $val = $entry.Properties[$prop] # $val is a *list*; e.g., use $val[0] to get the 1st item
}

注意:$entries | ForEach-Object { <# work with entry $_ #> },即使用管道也是一种选择,但如果条目列表已经在内存中,foreach 循环会更快。

我对@mklement0 脚本做了一些修改,太多了无法发表评论。

  • 为了解决多行属性问题,这些属性(所有 似乎包含在花括号中)与 RegEx 替换连接。
  • 为了独立于语言环境,脚本仅使用虚线标记 header 部分用于拆分内容(需要注意的是它插入了一个空白 第一次进入)
  • 我想知道为什么词典里只有4个词条 输出直到我找到 $FormatEnumerationLimit 的默认值 是 4

  • 为了避免在输出中换行,脚本使用 Out-String -Width 4096


## Q:\Test18\SO_50946956.ps1
# IMPORTANT: bcdedit /enu, requires an ELEVATED session.
#requires -RunAsAdministrator

## the following line imports the file posted by SupenJMN for testing
$bcdOutput = (gc ".\BCDEdit_ES.txt") -join "`n" -replace '\}\n\s+\{','},{'
## for a live "bcdedit /enum all" uncomment the following line
# $bcdOutput = (bcdedit /enum all) -join "`n" -replace '\}\n\s+\{','},{'

# Create the output list.
$entries = New-Object System.Collections.Generic.List[pscustomobject]]

# Parse bcdedit's output into entry blocks and construct a hashtable of
# property-value pairs for each.
($bcdOutput -split '(?m)^([a-z].+)\n-{10,100}\n').ForEach({
  if ($_ -notmatch '  +') {
    $entries.Add([pscustomobject] @{ Name = $_; Properties = [ordered] @{} })
  } else {
    ($_ -split '\n' -ne '').ForEach({
      $keyValue = $_ -split '\s+', 2
      $entries[-1].Properties[$keyValue[0]] = $keyValue[1]
    })
  }
})

# Output a quick visualization of the resulting list via Format-Custom
$FormatEnumerationLimit = 20
$entries | Format-Custom | Out-String -Width 4096 | Set-Content BCDEdit_ES_Prop.txt

脚本的简短示例输出(~700 行)

class PSCustomObject
{
  Name = 
  Properties = 
    [
    ]

}

class PSCustomObject
{
  Name = Administrador de arranque de firmware
  Properties = 
    [
      class DictionaryEntry
      {
        Key = Identificador
        Value = {fwbootmgr}
        Name = Identificador
      }
      class DictionaryEntry
      {
        Key = displayorder
        Value = {bootmgr},{e37fc869-68b0-11e8-b4cf-806e6f6e6963},{05d4f193-712c-11e8-b4ea-806e6f6e6963},{05d4f194-712c-11e8-b4ea-806e6f6e6963},{cb6d5609-712f-11e8-b4eb-806e6f6e6963},{cb6d560a-712f-11e8-b4eb-806e6f6e6963},{cb6d560b-712f-11e8-b4eb-806e6f6e6963}
        Name = displayorder
      }
      class DictionaryEntry
      {
        Key = timeout
        Value = 1
        Name = timeout
      }
    ]

}

我的方法看起来有点像这样:

(bcdedit /enum | Out-String) -split '(?<=\r\n)\r\n' | ForEach-Object {
    $name, $data = $_ -split '\r\n---+\r\n'

    $props = [ordered]@{
        'name' = $name.Trim()
    }

    $data | Select-String '(?m)^(\S+)\s\s+(.*)' -AllMatches |
        Select-Object -Expand Matches |
        ForEach-Object { $props[$_.Groups[1].Value] = $_.Groups[2].Value.Trim() }

    [PSCustomObject]$props
}

上面的代码基本上是从将 bcdedit 输出合并为一个字符串开始的,就像其他答案一样,然后将该字符串拆分为引导配置数据块。然后再次拆分这些块中的每一个,以将标题与实际数据分开。标题作为引导配置部分的名称添加到哈希表中,然后使用 key/value 对的正则表达式解析数据块。这些被附加到哈希表,最终转换为自定义 object.

由于 orderedPSCustomObject 类型加速器,代码至少需要 PowerShell v3。

当然,您可以对上面的基本示例代码应用各种优化。例如,不同的引导配置部分可能具有不同的属性。引导管理器部分具有 toolsdisplayordertimeout 等属性,它们在引导加载程序部分中不存在,而引导加载程序部分具有 osdevicesystemroot 等不存在的属性存在于启动管理器部分。如果您想要所有生成的 object 的一组一致的属性,您可以通过 Select-Object 将它们与您希望 object 具有的属性列表进行管道传输,例如:

... | Select-Object 'name', 'identifier', 'default', 'osdevice' 'systemroot'

列表中不存在的属性将从 object 中删除,而 object 中不存在的属性将添加一个空值。

此外,您可以将它们转换为更合适的类型或仅修改值,而不是将所有值都创建为字符串,例如从字符串中删除大括号。

... | ForEach-Object {
    $key = $_.Groups[1].Value
    $val = $_.Groups[2].Value.Trim()

    $val = $val -replace '^\{(.*)\}$', ''
    if ($val -match '^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$') {
        $val = [guid]$val
    } elseif ($val -eq 'yes' -or $val -eq 'true') {
        $val = $true
    } elseif ($val -eq 'no' -or $val -eq 'false') {
        $val = $false
    } elseif ($key -eq 'locale') {
        $val = [Globalization.CultureInfo]$val
    }

    $props[$key] = $val
}