使用Try / Catch输出错误

时间:2017-06-22 12:57:40

标签: powershell error-handling 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" 
    }
}

1 个答案:

答案 0 :(得分:0)

您需要-ErrorAction Stop

添加get-childitem