-1
我正在研究可以從外部文本文件中刪除文件的腳本。腳本工作很好,但現在,我想通過將錯誤輸出到外部文本文件來改善它。使用Try/Catch輸出錯誤
我嘗試過嘗試和趕上,但我不知道它爲什麼不起作用。下面是相關PS1代碼:
$LogFile = "log.txt"
$ErrorFile = "error.txt"
Get-ChildItem -Path (Get-Content liste.txt) |
ft FullName -HideTableHeaders |
Out-File $logfile -Append
foreach ($i in Get-Content liste.txt) {
try {
Get-ChildItem -Path $i -Include *.* -Recurse | Remove-Item
Write-Output "tout est supprimé"
} catch {
Write-Output "Something's wrong"
}
}
您正在使用try/catch塊錯誤地添加
-ErrorAction Stop
。除非它處於finally塊中,否則不應該在捕獲後添加代碼。 – Christopher即使我刪除了代碼後,我的腳本工作,但只有我的嘗試的消息出現,即使有錯誤 –