问题标签 [powershell-trap]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
811 浏览

powershell - Powershell陷阱没有被持续触发

我不明白为什么我在 Powershell 中看到这种行为:

为什么一个表达式会触发陷阱而另一个不会?

0 投票
2 回答
15161 浏览

powershell - Powershell trap [Exception] is not catching my error

For some reason my script is not catching the exception when I run the following script against a file that does not exist. I based this code from examples I found on the web but it doesn't seem to work for me.

I would appreciate any tips or pointers on how to fix this.

Note: in the example below I've also tried

but that didn't work either.

Here's the script:

output:

0 投票
1 回答
1869 浏览

powershell - Get-ChildItem -Filter 和 Robocopy (ErrorAction) 的 Powershell 错误陷阱

我正在起草一个 powershell 脚本来手动备份一些 DC。备份需要从一个文件夹移动到另一个文件夹,我使用 Get-ChildItem -Filter "Backup*" 选择备份文件,然后使用 robocopy 移动它们。

它可以工作,但如果源或目标路径不正确,我想添加一个陷阱和自定义错误消息“将本地备份从 XX 复制到 SystemStateBackup 时出错”。我还想将任何错误通过管道传输到日志文件。

没有陷阱的原版是

如果我只使用部分代码,陷阱就会起作用

但它不会以这种方式工作

它会导致以下 powershell 错误

Get-ChildItem:找不到路径“F:\WindowsImageBackup\ITUPW-PRODDC”,因为它不存在。在 line:1 char:88 + Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem <<<< f:\WindowsImageBackup\I TUPW-PRODDC -Filter "Backup*" -Name | ForEach-Object { robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$ " "f:\WindowsIma geBackup\ITUPW-PRODDC7\SystemStateBackup\$ " /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl} -ea "Stop" + CategoryInfo : ObjectNotFound: (F:\WindowsImageBackup\ITUPW-PRODDC:String) [Get-ChildItem], ItemNotFoun dException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

任何建议将不胜感激。

谢谢你的时间。

Amelia - 一个抽到最后一根稻草的系统管理员

0 投票
1 回答
1440 浏览

powershell - 陷阱删除项目异常

在 powershell 中存在一些我无法捕获的异常,或者我没有采取任何措施来纠正问题。

所以这里是做这些事情的代码:

我已经注释掉了陷阱表达式中的类型,以确保捕捉到任何东西。但不幸的是,我从来没有进入陷阱条款。

我可以看到异常日志,但看不到“移至垃圾箱”日志。

0 投票
1 回答
94 浏览

winforms - 陷阱{} 在 winform 中不起作用

trap{}我在所有脚本中都有一个通用规则来处理和记录我没有通过 Try/catch 处理的所有不可预见的异常。

这很好用。

但是,当我有一个带有 windows 窗体的脚本时,所有异常都会自动显示为 .Net-popup,并且trap{}不会执行里面的代码,因此永远不会记录错误。

简单示例:

为什么陷阱被忽略?
以及如何让我的表单在出现异常时执行陷阱?
尤其是在具有 100 多个函数的脚本中,我不想trap{}为每个函数添加一个(有效的)。

0 投票
1 回答
148 浏览

powershell - 我不明白陷阱是如何工作的

我正在尝试制作一个陷阱命令来捕获找不到文件的异常。这是代码:

我是否误解了如何做到这一点?

0 投票
1 回答
38 浏览

powershell - 从 Powershell 中的 TRAP 中排除错误代码

我正在运行一个脚本来获取我正在监视的某些站点的 Invoke-WebRequest 状态代码(例如 504、400、200 等)。

我正在使用 TRAP 方法将错误记录到一个文本文件中,以便以后进行分析。

代码如下:

问题:我可以排除捕获某些错误代码,如“503”吗?

0 投票
1 回答
197 浏览

powershell - Powershell:陷阱继续打破整个循环

尝试创建一种更好的方法来处理某些自动化内容的错误。

我的理解是,当在 Trap 中使用“继续”时,它应该保持循环,但跳过当前迭代的其余部分。但是,在下面的代码中,一切都按我的预期处理,除了在循环中,代码完全停止,无论我尝试什么,我都无法让它继续循环。

任何人都可以解释为什么或有一个解决方案可以让循环继续,同时仍然捕获(而不是尝试/捕获)错误?