【发布时间】:2017-11-25 18:34:57
【问题描述】:
我正在编写一个可以从外部文本文件中删除文件的脚本。该脚本运行良好,但现在,我想通过将错误输出到外部文本文件来改进它。
我尝试过 try and 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 块。除非在 finally 块中,否则在 catch 之后不应有代码。
-
即使我在 catch 后删除了代码,脚本仍然有效,但只有我尝试的消息出现,即使有错误
标签: powershell error-handling try-catch