我不知道使用 WMI 的方法,但您可以bcdedit
结合使用Select-String
:
$otherboot = bcdedit /enum |
Select-String "path" -Context 2,0 |
ForEach-Object { $_.Context.PreContext[0] -replace '^identifier +' } |
Where-Object { $_ -ne "{current}" }
解释:
的输出bcdedit /enum
大致如下:
Windows Boot Manager
--------------------
identifier {bootmgr}
device partition=\Device\HarddiskVolume1
description Windows Boot Manager
locale en-US
...
Windows Boot Loader
-------------------
identifier {current}
device partition=C:
path \Windows\system32\winload.exe
description Windows 7
locale en-US
...
Windows Boot Loader
-------------------
identifier {e0610d98-e116-11e1-8aa3-e57ee342122d}
device partition=C:
path \Windows\system32\winload.exe
description DebugEntry
locale en-US
...
此输出的相关部分是具有记录的Windows Boot Loader
部分,与该部分不同。因此,我们可以使用此记录仅选择部分:Windows Boot Manager
path
Windows Boot Loader
Select-String "path"
由于identifier
记录在记录之前的 2 行path
,我们需要 2 行PreContext
(并且没有PostContext
):
Select-String "path" -Context 2,0
现在我们从输出中选择了以下两个块bcdedit /enum
:
标识符 {当前}
设备分区=C:
路径 \Windows\system32\winload.exe
标识符 {e0610d98-e116-11e1-8aa3-e57ee342122d}
设备分区=C:
路径 \Windows\system32\winload.exe
因为我们只对第一行感兴趣,所以我们使用循环PreContext
选择这两行:ForEach-Object
ForEach-Object { $_.Context.PreContext[0] }
这将两个块减少为:
标识符 {当前}
标识符 {e0610d98-e116-11e1-8aa3-e57ee342122d}
identifier
我们通过字符串替换从中删除类别 ( ):
ForEach-Object { $_.Context.PreContext[0] -replace '^identifier +' }
正则表达式'^identifier +'
匹配以单词“identifier”开头的(子)字符串,后跟一个或多个空格,替换为空字符串。在这个替换之后,两个块减少到这个:
{当前的}
{e0610d98-e116-11e1-8aa3-e57ee342122d}
所以现在我们只需要过滤掉包含的块{current}
,剩下的是另一个引导记录的标识符:
Where-Object { $_ -ne "{current}" }
在此之后,变量$otherboot
包含非当前引导记录的标识符。