手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >PowerShell: Try...Catch...Finally 实现方法
PowerShell: Try...Catch...Finally 实现方法
摘要:复制代码代码如下:functionTry{param([ScriptBlock]$Command=$(throw"Theparameter-...

复制代码 代码如下:

function Try

{

param

(

[ScriptBlock]$Command = $(throw "The parameter -Command is required."),

[ScriptBlock]$Catch = { throw $_ },

[ScriptBlock]$Finally = {}

)

& {

$local:ErrorActionPreference = "SilentlyContinue"

trap

{

trap

{

& {

trap { throw $_ }

&$Finally

}

throw $_

}

$_ | & { &$Catch }

}

&$Command

}

& {

trap { throw $_ }

&$Finally

}

}

使用示例:

复制代码 代码如下:

# Example usage

Try {

echo " ::Do some work..."

echo " ::Try divide by zero: $(0/0)"

} -Catch {

echo " ::Cannot handle the error (will rethrow): $_"

#throw $_

} -Finally {

echo " ::Cleanup resources..."

}

【PowerShell: Try...Catch...Finally 实现方法】相关文章:

PowerShell 获取系统信息的函数

PowerShell把文件删除到回收站的方法

PowerShell脚本实现添加、修改任务计划的例子

PowerShell中按文件后缀过滤的实现代码

PowerShell Out-File向只读文件写入内容的方法

PowerShell实现的文件同步脚本分享

Windows PowerShell是啥?看完本文你就懂它了

PowerShell读取文本文件指定行内容的方法

PowerShell小技巧之执行SOAP请求

PowerShell Contains函数查找字符串实例

精品推荐
分类导航