Remove empty elements from an array

Welcome Forums General PowerShell Q&A Remove empty elements from an array

  • This topic has 3 replies, 2 voices, and was last updated 11 months ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #170548
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Hey, I´m kinda new into PowerShell and this is my first more complex script. What I meant is extracting informations from log  – Free memory and Adapter time. Log is in .txt format and this script is actually working. But, I dont think that this is the correct way how to extract informations from some text file. Any tips for a begginer? Thanks for help

      $file= 'C:\users\jhavlicek\Documents\PowerShell\hsmvypis_a.txt'
      
      $cont= Get-Content $file
      
      [email protected]()
      
      $cont| foreach {
      
      $at = $null
      $bt = $null
      
      $a= $_| Select-String "Adapter Clock"
      $b= $_| Select-String "Free Memory"
      
      if (([string]$a).Length -ge 10)
      {
      $ai=([string]$a).indexof(":")
      $at=([string]$a).substring($ai+2).Trim()
      }
      
      if (([string]$b).Length -ge 10)
      {
      $bi=([string]$b).IndexOf(":")
      $bt=([string]$b).substring($bi+2).Trim()
      }
      
      $array += @($at;$bt) | Where-Object {$_}
      }
      
      $pocet=$array.Count
      
      $csv= @("cas","mem")
      $i=0
      
      while ($i -le $pocet) {
      
      $array[$i]
      $array[$i+1]
      $newRow = New-Object PsObject -Property @{ cas = $($array[$i]) ; mem = $($array[$i+1]) }
      
      $csv += $newRow
      $i=$i+2
      }
      
      $csv |ft "cas","mem"
      
    • #170560
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      It would help us (and probably even yourself) when you fomrat your code nicely – including indentation and with less needless blank lines.
      Regardless of that you could post some example lines of your log file and the expected result. This way we will not have to guess that much. 😉

    • #170692
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Example lines from my log are here:

      ProtectToolkit C Configuration Utility 5.6.0
      Copyright (c) Safenet, Inc. 2009-2017

      Current Adapter Configuration for Device 0:

      Model : PSI-E2:PL1500
      Batch : 0000
      Manufacturing date: 15/08/2018 15:52:06
      Serial Number : 579000
      Adapter Clock : 25/06/2019 10:57:29 (+1:00+DST)
      Board Revision : 6.00
      Firmware Version : 5.01.02
      Cprov Version : 5.6
      Hardware Status : BATTERY OK PCB v0 FPGA v0 EXT PINS 0
      Free Memory : 62230528
      SM Size Free/Total: 2737552/4091776
      Temperature : 46 Deg C

      This is example of one output from log. There are many more of those but with exact same infos. What im doing here is extracting just “Free Memory” and “Adapter Clock” into a 2 columns (One with memory, second with Clock”). Also, I get rid of names, so only value remains, for example:

      25/06/2019 10:57:29 (+1:00+DST) 62230528

    • #170704
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      Hmmm … I’m still pretty unsure if I have all necessary information but this could be a starter for you … try it:

      $Content = Get-Content -Path 'C:\users\jhavlicek\Documents\PowerShell\hsmvypis_a.txt'
      $Content | 
          Where-Object { $_ -match '^.+\s+:\s+.+$' } | 
              ForEach-Object { $_ -replace '\s+:\s+', ' = ' } |
                  ConvertFrom-StringData | 
                      Where-Object { $_.Keys -match 'Adapter' -or $_.Keys -match 'Memory' }

      At least with the example data you provided it should work.

Viewing 3 reply threads
  • The topic ‘Remove empty elements from an array’ is closed to new replies.