If you just started PowerShell today then I can only imagine what you'll be doing with it in a year's time... You have started very well in my opinion.
Now, you seem to want to make everything into a function, which I suppose there is little harm in, but personally it seems overkill. When I pasted your test code into my ISE the first thing I did was comment out the first line and 28th line through the end of it. There just doesn't seem to be a need for all that.
Next I added a parameter to the Get-Log function so that a path can be provided if desired, and if excluded it will default to your res.txt file.
Function Get-Record {
Param ($Log)
for ($i=1; $i -lt $Log.Count; $i++) { # ignore the header
$Testcase = $Log[$i].split("`n")
$Output = New-Object PSobject -Property @{
Name = $Testcase[0]
Data = $Testcase[3..($Testcase.count)]|?{!($_ -match "\[#TC#]")}
}
$Output|Add-Member -MemberType NoteProperty -Name "Error" -value $(if($Output.data -match "^.+?(\[#ERROR#]|\[#WARNING#])"){$true}else{$false})
$Output
}
} # End: Function Get-Record
After that I looked at the value of $Log once it was gotten. You end up with an array with 3 strings in it. That's all fine and good, but what you really want is an array with 3 arrays in it if you ask me. Right now $Log[0] is a string with 4 lines of text, and you'd be better off with an array of 4 strings... so let's go that route first. I modified your Get-Record to accomplish that.
Function Get-Record {
Param ($Log)
for ($i=1; $i -lt $Log.Count; $i++) { # ignore the header
$Testcase = $Log[$i].split("`n")
You'll notice the split is done on n which is the powershell NewLine character. Then I updated the object you created to exclude the [#TC#] which was used as a delimiter, and assigned it a variable instead of just outputting it. Once I had that $Output variable I tested it for
[#ERROR#]and
[#WARNING#]` using a regex match and added a new Error property to the object depending on if an error was found or not.
$Output = New-Object PSobject -Property @{
Name = $Testcase[0]
Data = $Testcase[3..($Testcase.count)]|?{!($_ -match "\[#TC#]")}
}
$Output|Add-Member -MemberType NoteProperty -Name "Error" -value $(if($Output.data -match "^.+?(\[#ERROR#]|\[#WARNING#])"){$true}else{$false})
$Output
}
} # End: Function Get-Record
Then I pretty much passed the rest off as is except added my path to the log that I made from your example text.
# Load the log into memory
$Log = Get-Log c:\temp\test.log
$Records = Get-Record -Log $Log
$Records # Added only to see the current progress.
#} #End: Function Import-MyLog1
#
#clear
#Import-MyLog1
Now, you could clean it up a bit I suppose by trimming blank space from the beginning of lines if you wanted, but that's just a matter of taste. But it gives you 2 entries in $Records each with the name you wanted, and the data lines, and a boolean Error property.