我正在撰寫一個 try/catch/finally 腳本來對不存在的檔案進行一些錯誤報告。是的,我正在研究 Log4j。這就是我到目前為止所擁有的。
$fileList = Get-Content "blah blah"
foreach ($file in $fileList)
{
try {
Get-Item $file
Set-ItemProperty $file -Name IsReadOnly -Value $false
zip -d $file org/apache/logging/log4j/core/lookup/JndiLookup.class
Set-ItemProperty $file -Name IsReadOnly -Value $true
}
catch
{
Write-Error "$file not found."
}
finally
{
$null = $errorreport
}
}
$errorreport | Out-File "blah blah.csv"
出于某種原因,它會將亂碼放入輸出檔案中,并且不會將不存在或機器未打開的檔案寫入 csv。我的猜測是它與捕獲部分有關,但我無法弄清楚。
對此的任何幫助將不勝感激,因為我是嘗試/捕捉/最終事物的新手。
非常感謝!
uj5u.com熱心網友回復:
塔格,
您需要在 Get-Item 中添加一個 -ErrorAction Stop 開關。這是一個要演示的測驗程式。
Try {
Get-Item -Path "G:\Test\Junkfile.txt"
"No Error Detected"
}
Catch { "Error Detected!"}
Finally {"I'm done here."}
樣本輸出:
Get-Item : Cannot find path 'G:\Test\Junkfile.txt' because it does not exist.
At line:3 char:4
Get-Item -Path "G:\Test\Junkfile.txt"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (G:\Test\Junkfile.txt:String) [G
et-Item], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetIt
emCommand
No Error Detected
I'm done here.
然后,您將-ErrorAction Stop添加到您獲得的 Get-Item 行:
Error Detected!
I'm done here.
uj5u.com熱心網友回復:
正如RetiredGeek解釋的那樣,您需要-ErrorAction Stop在 catch 塊中捕獲終止和非終止錯誤。
您不需要 finally 塊,我建議在 catch 內輸出一個物件,以便您可以在之后將其寫入適當的 CSV 檔案:
$fileList = Get-Content "X:\blah blah.txt"
# loop through the file paths and capture error messages in a variable
$errorreport = foreach ($file in $fileList) {
try {
$fileObject = Get-Item $file -ErrorAction Stop # enters the catch block on errors
$isReadOnly = $fileObject.IsReadOnly
$fileObject.IsReadOnly = $false
# I cannot check if this is correct...
zip -d $file org/apache/logging/log4j/core/lookup/JndiLookup.class
$fileObject.IsReadOnly = $isReadOnly
}
catch {
# in here, the $_ automatic variable represents the actual exception
Write-Host "Error processing '$file': $($_.Exception.Message)" -ForegroundColor Red
# output an object with the file and exception message for the CSV
[PsCustomObject]@{
File = $file
Error = $_.Exception.Message
}
}
}
# write the error report as proper CSV file
$errorreport | Export-Csv -Path "X:\blah blah.csv" -NoTypeInformation
uj5u.com熱心網友回復:
我不是專業人士,不確定這會有所幫助,但在答案部分更容易做到這一點。
$fileList = Get-Content "blah blah"
foreach ($file in $fileList) {
try {
Get-Item $file
Set-ItemProperty $file -Name IsReadOnly -Value $false
zip -d $file org/apache/logging/log4j/core/lookup/JndiLookup.class
Set-ItemProperty $file -Name IsReadOnly -Value $true
$errorreport = [PSCustomObject]@{
FileName = $file
}
}
catch {
Write-Error "$file not found."
}
finally {
$null = $errorreport
}
}
If ($errorreport) {
$errorreport | Out-File "blah blah.csv"
}
Else {
write-error "Problem"
}
您缺少的關鍵部分是您沒有將任何東西匯出到$ErrorReport
您的目標是什么?你想報告什么?我的回答可能不是很好,因為您沒有給我們足夠的指導。如果我要做像你一樣的事情,我會這樣做如下。
$fileList = Get-Content "blah blah"
$Result = $fileList | ForEach-Object {
$null = $errorreport
try {
Get-Item $_
Set-ItemProperty $_ -Name IsReadOnly -Value $false
zip -d $_ org/apache/logging/log4j/core/lookup/JndiLookup.class
Set-ItemProperty $_ -Name IsReadOnly -Value $true
}
catch {
Write-Error "$_ not found."
$errorreport = $true
}
[PSCustomObject]@{
FileName = $_
Error = $errorreport
}
}
$Result | Out-File "blah blah.csv" -NoTypeInformation
================只有錯誤匯出(如果有)。==========下面
$fileList = $null
$fileList = Get-Content "blah blah"
$Result = $null
$Result = $fileList | ForEach-Object {
$null = $errorreport
try {
Get-Item $_
Set-ItemProperty $_ -Name IsReadOnly -Value $false
zip -d $_ org/apache/logging/log4j/core/lookup/JndiLookup.class
Set-ItemProperty $_ -Name IsReadOnly -Value $true
}
catch {
Write-Error "$_ not found."
$errorreport = $true
[PSCustomObject]@{
FileName = $_
Error = $errorreport
}
}
}
If ($Result) {
$Result | Out-File "blah blah.csv" -NoTypeInformation
}
Else {
Write-host "No errors found"
}
我上面的其他人可能做得更好
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405494.html
標籤:
