将 `Bcdedit /Firmware` 输出保存到数组

问题描述 投票: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 : ATA HDD: ADATA SU650
id  : {ed2a2e5c-bb68-11ee-89e6-806e6f6e6963}

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

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

@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

)
rem get description, identifier, device, path
arrays powershell batch-file firmware bcdedit
1个回答
1
投票

注意

  • 为了使作为解决方案核心的
    bcdedit /enum firmware
    调用成功,它必须从 提升的会话 运行。因此,下面的代码也必须如此。

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

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

@echo off & setlocal enableDelayedExpansion :: Loop over all "identifier" lines of interest and store the 2nd :: whitespace-separated token of each :: 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%]!
注:

  • 如果您需要确保不存在任何预先存在的

    Item[%ndx%]

     变量,请将以下内容放在 
    for /f
     调用之前:

    for /f "delims==" %%i in ('Set Item[ 2^>NUL') do set "%%i="
    
    

如果您想

捕获所有值,在多个模拟数组中,每个值对应于PowerShell代码输出中的属性之一:

@echo off & setlocal enableDelayedExpansion set i=0 for /f "usebackq tokens=1-4 delims=," %%a in (` powershell -NoProfile -Command "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] } } } } }; FWList | ConvertTo-Csv -NoTypeInformation | select -Skip 1" `) do set "DES[!i!]=%%~a" & set "ID[!i!]=%%~b" & set "PATH[!i!]=%%~c" & set "DEV[!i!]=%%~d" & set /a i=i+1 :: Example processing: :: Print all "array variables" that were created. echo -- Resulting DEV[%%ndx%%] variables: set DES[ echo -- Resulting ID[%%ndx%%] variables: set ID[ echo -- Resulting PATH[%%ndx%%] variables: set PATH[ echo -- Resulting DEV[%%ndx%%] variables: set DEV[
    
© www.soinside.com 2019 - 2024. All rights reserved.