0

I have a log file what contains S.M.A.R.T. data of my hard drive. I would like to handle this file with PowerShell. Here is the part of my log file.

3 Spin_Up_Time            0x0020   100   100   000    Old_age   Offline      -       0
4 Start_Stop_Count        0x0030   100   100   000    Old_age   Offline      -       0
5 Reallocated_Sector_Ct   0x0032   100   100   000    Old_age   Always       -       0

And here is my code

$i = 1
$a = Get-Content log.txt

do {
$trimmed = $a[$i].trim()
$splitted = $trimmed.split(" ")
$i++
}while ($i -le 3)

If I use the .split(" ") it is working only with the thrid row. How can I split my all rows correctly?

Thank you

1
  • if I put $splitted[2] before the end of the loop I can not get the expected values.( 0x0020 0x0030 0x0032) Commented Apr 2, 2014 at 14:55

3 Answers 3

2

A bit more code, but it gives you something that's a little easier to work with in the end:

$SMART = gc c:\temp\test.txt | %{
    $temp = $_ -split " "|?{!([string]::IsNullOrWhiteSpace($_))}
    new-object psobject -Property @{
        "Entry"=$temp[0]
        "TestName"=$temp[1]
        "HexCode"=$temp[2]
        "Number1"=$temp[3]
        "Number2"=$temp[4]
        "Number3"=$temp[5]
        "Age"=$temp[6]
        "Status"=$temp[7]
        "Filler"=$temp[8]
        "Zero?"=$temp[9]
    }
}
$SMART|FT Entry,TestName,HexCode,Number1,Number2,Number3,Age,Status,Filler,Zero?
Sign up to request clarification or add additional context in comments.

Comments

1

what does this do for you?

$a = Get-Content log.txt    
-split $a

I get this

H:\> -split $a
3
Spin_Up_Time
0x0020
100
100
000
Old_age
Offline
-
0
4
Start_Stop_Count
0x0030
100
100
000
Old_age
Offline
-
0
5
Reallocated_Sector_Ct
0x0032
100
100
000
Old_age
Always
-
0

Comments

0

I like working with regex' here's a sample that allows you to name your columns.

$a = Get-Content log.txt
$pattern = [regex]'(?<rowid>\d+)\s(?<desc>[a-zA-Z_]+)\s+(?<hexdata>0x\d{4})\s+(?<col4>\d{3})\s+(?<col5>\d{3})\s+(?<col6>\d{3})\s+(?<text1>.+?)\s+(?<state>.+?)-\s+0'

foreach ($line in $a) {
    if ($line -match $pattern) {
        $dataobj = New-Object PSObject
        $dataobj | Add-Member -type NoteProperty -name "Description" -value $matches['desc']
        $dataobj | Add-Member -type NoteProperty -name "Hex Data" -value $matches['hexdata']
        $dataobj | Add-Member -type NoteProperty -name "State" -value $matches['state']

        $dataobj
    }

}

Results:

Description                                          Hex Data                                             State                                              
-----------                                          --------                                             -----                                              
Spin_Up_Time                                         0x0020                                               Offline                                            
Start_Stop_Count                                     0x0030                                               Offline                                            
Reallocated_Sector_Ct                                0x0032                                               Always                                             

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.