将 Bcdedit 固件保存到批处理文件中的数组

问题描述 投票:0回答:1

帮我创建一个批处理脚本,并得到与此powershell脚本相同的结果:

function FWList {
    $FWStore = bcdedit /enum firmware
    $FWOS = ($FWStore | select-string identifier,device,path,description) -notmatch '{fwbootmgr}'
    for ( $n=0; $n -lt $FWOS.count; $n++ ) {
        if ( $FWOS[$n] -match 'identifier' ) {
            if ( $FWOS[$($n + 1 )] -match 'device' ) {
                [PsCustomObject]@{
                    des  = $FWOS[$($n + 3)].line.TrimStart('description').Trim()
                    id   = $FWOS[$n].line.Split()[-1]
                    path = $FWOS[$($n + 2)].line.Split()[-1]
                    dev  = $FWOS[$($n + 1)].line.Split()[-1]
                }
            } else {
            [PsCustomObject]@{
                    des  = $FWOS[$($n + 1)].line.TrimStart('description').Trim()
                    id   = $FWOS[$n].line.Split()[-1]
                }
            }
        }
    }
}

# Example Usage
FWList | Format-List #To show in List view
FWList | Format-Table #To show in Table view
(FWList)[1..3] | Foreach {$_.des}
Pause

示例:“FWList | 格式列表”

输出:

des  : Windows Boot Manager
id   : {bootmgr}
path : \EFI\Microsoft\Boot\bootmgfw.efi
dev  : partition=\Device\HarddiskVolume3

des  : opensuse-secureboot
id   : {1fa30703-d549-11ee-8b03-806e6f6e6963}
path : \EFI\opensuse\shim.efi
dev  : partition=\Device\HarddiskVolume3

在这个脚本中,我可以轻松获取数组中保存的任何固件的值。

我尝试了这样的操作,但对如何继续感到困惑......

@echo off
setlocal ENABLEDELAYEDEXPANSION
set "FWOSList=bcdedit /enum firmware ^| findstr "identifier description device path" ^| findstr /v "{fwbootmgr}""
set arr=0
for /F "usebackq tokens=1-4" %%a in ( `%FWOSList%` ) DO (
    if /I "%%a[%arr%]"=="identifier" set ID[%arr%]=%%b
    set /a arr+=1
)
arrays powershell batch-file firmware bcdedit
1个回答
0
投票

从您自己的批处理文件尝试来看,您似乎只是在查找以

identifier
开头的行上的值,但值为
{fwbootmgr}
的行除外。

@echo off & setlocal enableDelayedExpansion

:: Loop over all "identifier" lines of interest and store the 2nd
:: whitespace-separated token in individual ID[%ndx%] variables that emulate
:: an array.
set i=0
for /f "usebackq tokens=2" %%a in (`
  bcdedit /enum firmware ^| findstr "^identifier " ^| findstr /v "{fwbootmgr}"
`) do set "ID[!i!]=%%a" & set /a i=i+1

:: Example processing:

:: Print all ID[%ndx%] variables that were created.
echo -- Resulting ID[%%ndx%%] variables:
set ID[

:: Target a specific variable, the 2nd one in this example.
set i=1
echo -- Value of ID[%i%]:
echo !ID[%i%]!
© www.soinside.com 2019 - 2024. All rights reserved.