Help me to create a batch script, and get the same result as this powershell script:
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
Example: "FWList | Format-List"
The output:
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}
In this script I can easily get value of any Firmwares saved in array.
I tried like this but confused about how to continue...
@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
Note:
bcdedit /enum firmwarecall at the heart of your solution to succeed, it must be run from an elevated session. Therefore, so must the code below.From your own batch-file attempt it looks like you're only looking for the values on the lines that start with
identifier, except for the line whose value is{fwbootmgr}.Note:
If you need to ensure that no preexisting
Item[%ndx%]variables exist, place the following before thefor /fcall:If you want to capture all values, in multiple emulated arrays each corresponding to one of the properties in the output from your PowerShell code:
Emulating what the PowerShell code does in a batch file would be exceedingly cumbersome.
Therefore, call your PowerShell code from your batch file, via
powershell.exe, the Windows PowerShell CLI.To make parsing of the output easier for your batch file, instruct the PowerShell code to produce CSV output, using
ConvertTo-Csv.