如何捕获终止错误并仍然继续执行我的PowerShell脚本

编程入门 行业动态 更新时间:2024-10-28 14:28:11
本文介绍了如何捕获终止错误并仍然继续执行我的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.

我已经更改了解决该问题的方法,并以此方式找到了解决方案-> 使用PowerShell清除大日志文件,方法是逐行删除并在我的日期比较为true时将其停止

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脚本

本文发布于:2023-10-24 07:50:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   错误   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!