我不知道的方式與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
記錄是path
記錄之前2行,我們需要2線PreContext
(沒有PostContext
):
Select-String "path" -Context 2,0
現在我們從bcdedit /enum
的輸出中選出下面兩個塊:
identifier {current}
device partition=C:
path \Windows\system32\winload.exe
identifier {e0610d98-e116-11e1-8aa3-e57ee342122d}
device partition=C:
path \Windows\system32\winload.exe
既然我們只在PreContext
的第一行感興趣,我們選擇使用ForEach-Object
環這兩條線:
ForEach-Object { $_.Context.PreContext[0] }
從而降低兩個塊這樣:
identifier {current}
identifier {e0610d98-e116-11e1-8aa3-e57ee342122d}
從中我們刪除類別(identifier
)通過字符串替換:
ForEach-Object { $_.Context.PreContext[0] -replace '^identifier +' }
正則表達式'^identifier +'
匹配以單詞「identifier」開頭的(子)字符串,後跟一個或多個用空字符串替換的空格。該更換後兩個塊減少到這一點:
{current}
{e0610d98-e116-11e1-8aa3-e57ee342122d}
所以,現在我們只需要過濾掉含有{current}
,什麼剩下的塊是其他引導記錄的標識:
Where-Object { $_ -ne "{current}" }
之後,變量$otherboot
包含非當前引導記錄的標識符。
好了,這是接近,但它不會做我想做的,如果我在這一行中插入$測試,像這樣: | %{$ test = $ _。Context.PreContext [0] -replace'^ identifier +'}' $ test實際上是{current},我用上下文編號玩弄,我試着選擇字符串「identifier」這並沒有幫助。我不完全理解這裏發生了什麼。 它看起來像我正在循環每次啓動並試圖找到標識符?我對嗎?我不一定要替換它。如果我可以使$ test =另一個啓動標識符,那麼我可以寫: bcedit/default $ test – dwarf
你做錯了。請參閱最新答案中的解釋。 –