Home > Net >  reading text file and capturing content in an array using powershell
reading text file and capturing content in an array using powershell

Time:11-18

I have a text file having content in it as below. i want to get data within "HEALTH_AT_A_GLANCE" in one array and data within "FIRMWARE_INFORMATION" in another array excluding INDEX* My question is how to achieve it. I tried a lot but not working for me, please suggest

HEALTH_AT_A_GLANCE
BIOS_HARDWARE STATUS= OK
FANS STATUS= OK
FANS REDUNDANCY= Redundant
TEMPERATURE STATUS= OK
POWER_SUPPLIES STATUS= OK
POWER_SUPPLIES REDUNDANCY= Redundant
BATTERY STATUS= OK
PROCESSOR STATUS= OK
MEMORY STATUS= OK
NETWORK STATUS= OK
STORAGE STATUS= OK
HEALTH_AT_A_GLANCE
FIRMWARE_INFORMATION
INDEX_1
FIRMWARE_NAME VALUE = ILO
FIRMWARE_VERSION VALUE = 2.44 Apr 30 2021
INDEX_1
INDEX_2
FIRMWARE_NAME VALUE = System ROM
FIRMWARE_VERSION VALUE = U30 v2.50 (05242021)
INDEX_2
INDEX_3
FIRMWARE_NAME VALUE = Redundant System ROM
FIRMWARE_VERSION VALUE = U30 v2.50 (05242021)
INDEX_3
INDEX_4
FIRMWARE_NAME VALUE = Intelligent Platform Abstraction Data
FIRMWARE_VERSION VALUE = 15.2.0 Build 51
INDEX_4
INDEX_5
FIRMWARE_NAME VALUE = Power Management Controller Firmware
FIRMWARE_VERSION VALUE = 1.0.7
FIRMWARE_FAMILY VALUE = 19h
INDEX_5
INDEX_6
FIRMWARE_NAME VALUE = Power Management Controller FW Bootloader
FIRMWARE_VERSION VALUE = 1.1
INDEX_6
INDEX_7
FIRMWARE_NAME VALUE = System Programmable Logic Device
FIRMWARE_VERSION VALUE = 0x31
INDEX_7
INDEX_8
FIRMWARE_NAME VALUE = Server Platform Services (SPS) Firmware
FIRMWARE_VERSION VALUE = 4.1.4.505
INDEX_8
INDEX_9
FIRMWARE_NAME VALUE = HPE Smart Storage Energy Pack 1 Firmware
FIRMWARE_VERSION VALUE = 0.70
INDEX_9
INDEX_10
FIRMWARE_NAME VALUE = Intelligent Provisioning
FIRMWARE_VERSION VALUE = 3.64.2
INDEX_10
INDEX_11
FIRMWARE_NAME VALUE = Innovation Engine (IE) Firmware
FIRMWARE_VERSION VALUE = 0.2.2.2
INDEX_11
INDEX_12
FIRMWARE_NAME VALUE = Embedded Video Controller
FIRMWARE_VERSION VALUE = 2.5
INDEX_12
INDEX_13
FIRMWARE_NAME VALUE = HPE Smart Array P816i-a SR Gen10
FIRMWARE_VERSION VALUE = 3.53
INDEX_13
INDEX_14
FIRMWARE_NAME VALUE = HP Ethernet 1Gb 4-port 366FLR Adapter
FIRMWARE_VERSION VALUE = 1.2836.0
INDEX_14
INDEX_15
FIRMWARE_NAME VALUE = HPE Smart Array P816i-a SR Gen10
FIRMWARE_VERSION VALUE = 
INDEX_15
FIRMWARE_INFORMATION

Required as below:
array1 will hold below data

ILO ->2.44 Apr 30 2021    
System ROM -> U30 v2.50 (05242021)
Redundant System ROM ->U30 v2.50 (05242021)
Intelligent Platform Abstraction Data ->15.2.0 Build 51
Power Management Controller Firmware ->1.0.7
Power Management Controller FW Bootloader -> 1.1
System Programmable Logic Device -> 0x31       
Server Platform Services (SPS) Firmware -> 4.1.4.505
HPE Smart Storage Energy Pack 1 Firmware -> 0.70         
Intelligent Provisioning -> 3.64.2
Innovation Engine (IE) Firmware -> 0.2.2.2    
Embedded Video Controller -> 2.5
HPE Smart Array P816i-a SR Gen10 -> 3.53
HP Ethernet 1Gb 4-port 366FLR Adapter ->1.2836.0

Array2 will hold below data:

BIOS_HARDWARE STATUS= OK
FANS STATUS= OK
FANS REDUNDANCY= Redundant
TEMPERATURE STATUS= OK
POWER_SUPPLIES STATUS= OK
POWER_SUPPLIES REDUNDANCY= Redundant
BATTERY STATUS= OK
PROCESSOR STATUS= OK
MEMORY STATUS= OK
NETWORK STATUS= OK
STORAGE STATUS= OK

I wrote the below code, its picking INDEX only till 8 after that not picking. how to get all INDEX data?

$patternarr = @()
$array = @()
$result = ""

$string = Get-Content $importpath
foreach ($line in $string)
{
    if ($line -like "*INDEX_*")
    {
        $patternarr = $line.Substring($line.length -1, 1)
    }
}
$patternmax = $patternarr | measure -Maximum
$patternmax
$patternnr = $patternmax.Maximum
For ($i=0; $i -lt $patternnr; $i  )
{
    $pattern ="INDEX_$i(.*?)INDEX_$i"
    $result = [regex]::match($string, $pattern).Groups[1].value
    $array  = $result.replace("                FIRMWARE_NAME VALUE = ","").replace("               FIRMWARE_VERSION VALUE = ","-> ").replace("                FIRMWARE_FAMILY VALUE = 19h","")
}
$array```

CodePudding user response:

You can try this as base script.

$x = @"
HEALTH_AT_A_GLANCE
BIOS_HARDWARE STATUS= OK
FANS STATUS= OK
FANS REDUNDANCY= Redundant
TEMPERATURE STATUS= OK
POWER_SUPPLIES STATUS= OK
POWER_SUPPLIES REDUNDANCY= Redundant
BATTERY STATUS= OK
PROCESSOR STATUS= OK
MEMORY STATUS= OK
NETWORK STATUS= OK
STORAGE STATUS= OK
HEALTH_AT_A_GLANCE
FIRMWARE_INFORMATION
INDEX_1
FIRMWARE_NAME VALUE = ILO
FIRMWARE_VERSION VALUE = 2.44 Apr 30 2021
INDEX_1
INDEX_2
FIRMWARE_NAME VALUE = System ROM
FIRMWARE_VERSION VALUE = U30 v2.50 (05242021)
INDEX_2
INDEX_3
FIRMWARE_NAME VALUE = Redundant System ROM
FIRMWARE_VERSION VALUE = U30 v2.50 (05242021)
INDEX_3
INDEX_4
FIRMWARE_NAME VALUE = Intelligent Platform Abstraction Data
FIRMWARE_VERSION VALUE = 15.2.0 Build 51
INDEX_4
INDEX_5
FIRMWARE_NAME VALUE = Power Management Controller Firmware
FIRMWARE_VERSION VALUE = 1.0.7
FIRMWARE_FAMILY VALUE = 19h
INDEX_5
INDEX_6
FIRMWARE_NAME VALUE = Power Management Controller FW Bootloader
FIRMWARE_VERSION VALUE = 1.1
INDEX_6
INDEX_7
FIRMWARE_NAME VALUE = System Programmable Logic Device
FIRMWARE_VERSION VALUE = 0x31
INDEX_7
INDEX_8
FIRMWARE_NAME VALUE = Server Platform Services (SPS) Firmware
FIRMWARE_VERSION VALUE = 4.1.4.505
INDEX_8
INDEX_9
FIRMWARE_NAME VALUE = HPE Smart Storage Energy Pack 1 Firmware
FIRMWARE_VERSION VALUE = 0.70
INDEX_9
INDEX_10
FIRMWARE_NAME VALUE = Intelligent Provisioning
FIRMWARE_VERSION VALUE = 3.64.2
INDEX_10
INDEX_11
FIRMWARE_NAME VALUE = Innovation Engine (IE) Firmware
FIRMWARE_VERSION VALUE = 0.2.2.2
INDEX_11
INDEX_12
FIRMWARE_NAME VALUE = Embedded Video Controller
FIRMWARE_VERSION VALUE = 2.5
INDEX_12
INDEX_13
FIRMWARE_NAME VALUE = HPE Smart Array P816i-a SR Gen10
FIRMWARE_VERSION VALUE = 3.53
INDEX_13
INDEX_14
FIRMWARE_NAME VALUE = HP Ethernet 1Gb 4-port 366FLR Adapter
FIRMWARE_VERSION VALUE = 1.2836.0
INDEX_14
INDEX_15
FIRMWARE_NAME VALUE = HPE Smart Array P816i-a SR Gen10
FIRMWARE_VERSION VALUE = 
INDEX_15
FIRMWARE_INFORMATION
"@

$l = $x.Split("`n")

$l1 = [System.Collections.Generic.List[String]]::new()
$l2 = [System.Collections.Generic.List[String]]::new()

$startH = $false
$startF = $false
$nextasvalue = $false
$header = ""

foreach($line in $l) {
    if ($line -match "HEALTH_AT_A_GLANCE") {
        if ($startH) {
            $startH = $false
        } else {
            $startH = $true
        }
        continue
    }

    if ($line -match "FIRMWARE_INFORMATION") {
        if ($startF) {
            $startF = $false
        } else {
            $startF = $true
        }
        continue
    }

    if ($line -match "^INDEX") {
        continue
    }

    if ($startH) {
        $l1.Add($line)
    }

    if ($startF) {
        if ($nextasvalue) {
            $value = $line.Split("=")[1].Trim()
            $l2.Add("$header ->$value")
            $nextasvalue = $false
        }
        else {
            $header = $line.Split("=")[1].Trim()
            $nextasvalue = $true
        }
    }
}

$l1
$l2

CodePudding user response:

Issue got resolved by using code line $pattern ="INDEX_$i(.*?)INDEX_$i"

  • Related