PowerShell中捕获异常时的行号和列号的方法
2019/7/10 21:27:51
本文主要是介绍PowerShell中捕获异常时的行号和列号的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如果自己写函数动态地去执行一些脚本块或者脚本文件,那么捕获到异常时行号和列号信息非常有用。例如脚本文件:
复制代码 代码如下:
trap{
$info=$_.InvocationInfo
"在第{0}行,第{1}列捕获到异常:{2}" -f $info.ScriptLineNumber,$info.OffsetInLine ,$_.Exception.Message
continue
}
get-date
1gb/2kb
Get-Service | foreach {1/0}
执行结果为:
复制代码 代码如下:
Thursday, June 26, 2014 4:41:41 PM
524288
在第9行,第15列捕获到异常:Attempted to divide by zero.
这篇关于PowerShell中捕获异常时的行号和列号的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-09-14SharePoint 2019 用 PowerShell将启用发布功能站点网站集另存为模板
- 2022-08-30PowerShell教程 - 程序性能和BUG分析工具
- 2022-08-30PowerShell教程 - 模块管理(Modules Management)
- 2022-08-29PowerShell教程 - Web requests(Web请求)
- 2022-08-26PowerShell教程 - 日期时间管理(Date & Time Management)
- 2022-08-25PowerShell教程 - 磁盘与硬件管理(Disk & Hardware Management)
- 2022-08-25PowerShell教程 - 系统事件管理(System Event Management)
- 2022-08-25PowerShell教程 - 文件系统管理(File System Management)
- 2022-08-24PowerShell教程 - 网络管理(Network Management)
- 2022-08-24PowerShell