Reputation: 33
I have script that outputs a log file, now I need to read this log file and create a new log file for when it finds the words WARNING & ERROR and i need it to write the line number and output an error code from 0-20 for ERROR and 21-40 for WARNING, but everytime I rn the script my ExitCodeLog.lg is empty.
$file = 'H:\REPO\ADNEW\Testlog.log'#@(Get-ChildItem -Filter Remove-StallUserObjects_*.log)[-1]
$lineNumber = 0
$errorCode = 0
$warningCode = 21
$output = ForEach ($line in $file) {
$lineNumber++
if ($errorCode -eq 20) {
$errorCode = 0
}
if ($warningCode -eq 40) {
$warningCode = 21
}
if ($line -cmatch " ERROR: ") {
"$lineNumber $errorCode"
$errorCode++
}
if ($line -cmatch " WARNING: ") {
"$lineNumber $warningCode"
$warningCode++
}
}
$output | Out-File -FilePath 'H:\REPO\ADNEW\ExitCodeLog.log'
Upvotes: 0
Views: 150
Reputation: 2929
The first problem is, that the contents of $file
is a plain string.
You should use
$file = get-content 'H:\REPO\ADNEW\Testlog.log'
Furthermore you can optimize the first two if statements by using a modulo operator:
$errorcode = $errorcode % 20
$warningcode = 21 + ($warningcode % 20)
Upvotes: 1