Skip to content

Commit dafe6b0

Browse files
committed
Improved Datestamp parsing from logs
1 parent 5d5cb8a commit dafe6b0

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

LabTech.psm1

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ Function Get-LTError{
17581758
Open the log file in a sortable searchable window.
17591759
17601760
.NOTES
1761-
Version: 1.2
1761+
Version: 1.3
17621762
Author: Chris Taylor
17631763
Website: labtechconsulting.com
17641764
Creation Date: 3/14/2016
@@ -1770,20 +1770,25 @@ Function Get-LTError{
17701770
Update Date: 3/18/2018
17711771
Purpose/Change: Changed Erroraction from Stop to unspecified to allow caller to set the ErrorAction.
17721772
1773+
Update Date: 1/26/2019
1774+
Purpose/Change: Update for better international date parsing support
1775+
17731776
.LINK
17741777
http://labtechconsulting.com
17751778
#>
17761779
[CmdletBinding()]
17771780
Param()
17781781

17791782
Begin{
1783+
Set-Alias -name LINENUM -value Get-CurrentLineNumber -WhatIf:$False -Confirm:$False
1784+
Write-Debug "Starting $($myInvocation.InvocationName) at line $(LINENUM)"
17801785
$BasePath = $(Get-LTServiceInfo -EA 0 -Verbose:$False -WhatIf:$False -Confirm:$False -Debug:$False|Select-Object -Expand BasePath -EA 0)
17811786
if (!$BasePath){$BasePath = "$env:windir\LTSVC"}
17821787
}#End Begin
17831788

17841789
Process{
17851790
if ($(Test-Path -Path "$BasePath\LTErrors.txt") -eq $False) {
1786-
Write-Error "ERROR: Unable to find log. $($Error[0])"
1791+
Write-Error "ERROR: Line $(LINENUM): Unable to find log. $($Error[0])"
17871792
return
17881793
}
17891794
Try{
@@ -1794,7 +1799,7 @@ Function Get-LTError{
17941799
if ($items[1]){
17951800
$object = New-Object -TypeName PSObject
17961801
$object | Add-Member -MemberType NoteProperty -Name ServiceVersion -Value $items[0]
1797-
$object | Add-Member -MemberType NoteProperty -Name Timestamp -Value $([datetime]$items[1])
1802+
$object | Add-Member -MemberType NoteProperty -Name Timestamp -Value $(Try {[datetime]::Parse($items[1])} Catch {})
17981803
$object | Add-Member -MemberType NoteProperty -Name Message -Value $items[2]
17991804
Write-Output $object
18001805
}
@@ -1803,14 +1808,15 @@ Function Get-LTError{
18031808
}#End Try
18041809

18051810
Catch{
1806-
Write-Error "ERROR: There was an error reading the log. $($Error[0])"
1811+
Write-Error "ERROR: Line $(LINENUM): There was an error reading the log. $($Error[0])"
18071812
}#End Catch
18081813
}#End Process
18091814

18101815
End{
18111816
if ($?){
18121817
}
18131818
Else {$Error[0]}
1819+
Write-Debug "Exiting $($myInvocation.InvocationName) at line $(LINENUM)"
18141820
}#End End
18151821
}#End Function Get-LTError
18161822

@@ -2478,7 +2484,7 @@ Function Get-LTProbeErrors{
24782484
Open the log file in a sortable searchable window.
24792485
24802486
.NOTES
2481-
Version: 1.1
2487+
Version: 1.3
24822488
Author: Chris Taylor
24832489
Website: labtechconsulting.com
24842490
Creation Date: 3/14/2016
@@ -2490,39 +2496,50 @@ Function Get-LTProbeErrors{
24902496
Update Date: 3/18/2018
24912497
Purpose/Change: Changed Erroraction from Stop to unspecified to allow caller to set the ErrorAction.
24922498
2499+
Update Date: 1/26/2019
2500+
Purpose/Change: Update for better international date parsing support
2501+
24932502
.LINK
24942503
http://labtechconsulting.com
24952504
#>
24962505
[CmdletBinding()]
24972506
Param()
24982507

24992508
Begin{
2509+
Set-Alias -name LINENUM -value Get-CurrentLineNumber -WhatIf:$False -Confirm:$False
2510+
Write-Debug "Starting $($myInvocation.InvocationName) at line $(LINENUM)"
25002511
$BasePath = $(Get-LTServiceInfo -EA 0 -Verbose:$False -WhatIf:$False -Confirm:$False -Debug:$False|Select-Object -Expand BasePath -EA 0)
25012512
if (!($BasePath)){$BasePath = "$env:windir\LTSVC"}
25022513
}#End Begin
25032514

25042515
Process{
25052516
if ($(Test-Path -Path "$BasePath\LTProbeErrors.txt") -eq $False) {
2506-
Write-Error "ERROR: Unable to find log. $($Error[0])"
2517+
Write-Error "ERROR: Line $(LINENUM): Unable to find log. $($Error[0])"
25072518
return
25082519
}
25092520
$errors = Get-Content "$BasePath\LTProbeErrors.txt"
25102521
$errors = $errors -join ' ' -split ':::'
2511-
Foreach($Line in $Errors){
2512-
$items = $Line -split "`t" -replace ' - ',''
2513-
$object = New-Object -TypeName PSObject
2514-
$object | Add-Member -MemberType NoteProperty -Name ServiceVersion -Value $items[0]
2515-
$object | Add-Member -MemberType NoteProperty -Name Timestamp -Value $([datetime]$items[1])
2516-
$object | Add-Member -MemberType NoteProperty -Name Message -Value $items[2]
2517-
Write-Output $object
2518-
}#End Foreach
2522+
Try {
2523+
Foreach($Line in $Errors){
2524+
$items = $Line -split "`t" -replace ' - ',''
2525+
$object = New-Object -TypeName PSObject
2526+
$object | Add-Member -MemberType NoteProperty -Name ServiceVersion -Value $items[0]
2527+
$object | Add-Member -MemberType NoteProperty -Name Timestamp -Value $(Try {[datetime]::Parse($items[1])} Catch {})
2528+
$object | Add-Member -MemberType NoteProperty -Name Message -Value $items[2]
2529+
Write-Output $object
2530+
}#End Foreach
2531+
}#End Try
2532+
2533+
Catch{
2534+
Write-Error "ERROR: Line $(LINENUM): There was an error reading the log. $($Error[0])"
2535+
}#End Catch
25192536
}
25202537

25212538
End{
25222539
if ($?){
25232540
}
25242541
Else {$Error[0]}
2525-
2542+
Write-Debug "Exiting $($myInvocation.InvocationName) at line $(LINENUM)"
25262543
}#End End
25272544
}#End Function Get-LTProbeErrors
25282545

0 commit comments

Comments
 (0)