これは、powershell -noprofile の Windows XP SP3 上の Powershell v2 で実行されています。
以下の Powershell スクリプトは、コピーしてコンソールに貼り付けると機能しますが、スクリプトとして実行すると機能しません。誰がそれを引き起こしているのかを見ることができますか?
スクリプトは次のとおりです (一部の名前は無実を保護するために変更されています)。
#Known Error values to count instances of
$KnownErrorText = @"
Invalid Descriptor Index
Syntax error or access violation
An error occurred while attemping to update a record
Message from LDAP server: Invalid credentials
Error sending data over TCP connection
"@
#Fix the variable so it's an array
$KnownErrorText = $knownErrorText.split("`n")
#output variables
$KnownErrors = @{}
$UnknownErrors = @()
#Generate the hash to contain counts of the the known errors
foreach ($line in $knownErrorText) {$KnownErrors.Add($line,0)}
#Get error lines from the log
$scerrors = Select-String -Path "\\myserver01\d$\logs\application.log" -Pattern "Error"
"$($scerrors.count) errors to process"
$ProcessedErrors = 1
#Look at each error. Pass the error through a switch to identify whether it's
#a known error or not. If it's Known, increment the appropriate count in the hash
#if it's not known, add it to the unknown errors output variable
foreach ($e in $scerrors) {
"$($ProcessedErrors)`tProcessing`t$($e.line)"
$addToUnknown = $true
"e.line type:`t$($e.line.gettype().name)`tLength$($e.line.length)"
switch -regex ($knownErrorText) {
#Look in the text of the current error for the any of the errors in
#the KnownErrorText array
#THIS IS THE PART THAT DOESN'T WORK WHEN RUNNING IN A SCRIPT
{"$($e.line)" -match "^.*$($_).*`$"} {
Write-host "Matched $($_)" -ForegroundColor Green -BackgroundColor Black
$KnownErrors.Set_Item($_,([int]$KnownErrors.Get_Item($_))+1)
$addToUnknown = $false
#We found our match so stop
#processing the KnownErrorText array through the switch
break
}
default {
Write-host "UnMatched`t$($_)" -ForegroundColor Red -BackgroundColor Black
}
}
#If we got through all the KnownErrorText values without finding a match,
#add the error to the UnknownErrors array to be displayed
if ($addToUnknown) {$UnknownErrors += $e}
$ProcessedErrors++
if ($ProcessedErrors -ge 5) {break}
}
#Console reporting
"Known Errors:"
$KnownErrors.GetEnumerator() | Sort-Object Value -Descending
""
"$($UnknownErrors.count) Unknown Errors:"
$UnknownErrors | Foreach {$_.line}
これをスクリプトとして実行すると (たとえば、c:\temp\ErrorReporting.ps1 に保存され、次のように呼び出された場合)
& c:\Temp\ErrorReporting.ps1
一致部分は失敗します:
{"$($e.line)" -match "^.*$($_).*
$"}`