如何捕获终止错误并仍然继续执行我的PowerShell脚本 [英] How to catch terminating error and still continue my PowerShell script

查看:1249
本文介绍了如何捕获终止错误并仍然继续执行我的PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的一点帮助来改善我的脚本并正确处理异常. 我有一个脚本,可以使用日期比较来删除.log文件中的行,以定义它是否足够老,如果为true,则删除行. 这是我需要帮助的脚本部分:

I need a bit of your help to improve my script and handle exceptions correctly. I have a script to delete lines in a .log file using date comparison to define if it's older enough and if true, line is deleted. Here is the part of the script where i need help:

Foreach ($Fichier in $Fichiers)
{
    try
    {
        (Get-Content $Fichier) |
            Where-Object {$_} |
            Where-Object { ([datetime]::ParseExact(([string]$_).Substring(0,19), $Format, $Culture) -ge (Get-Date).AddDays(-$Jours)) } |
            Set-Content $Fichier
        LogMessage -Message "Fichier $Fichier purgé des toutes les lignes datant de plus de $Jours jours"
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        LogMessage -Message "$FailedItem - $ErrorMessage"
    }
}

问题是我的.log中有这样的行

The problem is my .log have lines like these

[ 30-10-2017 16:38:11.07 | INFO    | XXX.ActiveMQ.ConsumerService.Business.TransactionConsumerTopic | WriteMessageToDlq    ] - Le traitement du message ID:XXXXX-56720-1509361559247-13:1:1:1:1 a rencontré une erreur côté Web Service : TKO / JMS_3.9.47612_2017-10-30 16:38:00.937_TKO / 3.9.47612 / 2017-10-30 16:38:00.937 / 
[1];Erreur non-gérée : Une erreur applicative est survenue.

如您所见,第一行对我的脚本来说是确定的,它可以对子字符串和日期进行比较,但是有时在文件中,您还有第二行是不正确的,因为它会引发错误,这是终止错误因为我的脚本无法继续

As you can see, the first line is OK for my script, it makes the Substring and the date comparison, but sometimes in the file, you have that second line which is not OK because it throws an error, a terminating error because my script doesn't continue

Exception lors de l'appel de « ParseExact » avec « 3 » argument(s) : « La chaîne n'a pas été reconnue en tant que DateTime valide. »

如果日期比较引发错误,我会尝试找到下一行的方法. 在@Luka-Klarić的评论

I try to find a way to go next line if the date comparison raises an error. i have also tried that way after the comment of @Luka-Klarić

try
    {
        (Get-Content $Fichier) |
            Where-Object {$_} |
            Where-Object { ([datetime]::ParseExact(([string]$_).Substring(0,19), $Format, $Culture) -ge (Get-Date).AddDays(-$Jours)) } -ErrorAction SilentlyContinue |
            Set-Content $Fichier
        LogMessage -Message "Fichier $Fichier purgé des toutes les lignes datant de plus de $Jours jours"
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        LogMessage -Message "$FailedItem - $ErrorMessage"
        Continue
    }

但是它也不起作用.对于像我这样的PowerShell新手,请提供任何帮助,并询问是否需要更多信息.

But it's not working too. Any help is appreciated for a PowerShell noob like me and ask if you need further information.

我已经更改了解决该问题的方法,并以此方式找到了解决方案->

EDIT : I have change the approach for that problem and i found a solution this way -> Purge with PowerShell a big log file by deleting line by line and stopping it when my date comparison is true

推荐答案

通常try{}catch{}避免程序停止:

$dates = "05/12/2012", "5/12/2012", "05/12/2012"
foreach($date in $dates){try{[datetime]::ParseExact($date, "dd/mm/yyyy", $null )}catch{}}

第二个日期会产生异常,但脚本仍会继续.

The second date generate an exception but the script still go on.

这篇关于如何捕获终止错误并仍然继续执行我的PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆