手机
当前位置:查字典教程网 >脚本专栏 >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 Contains函数查找字符串实例

Powershell中阻止”确认提示”的方法

Powershell 查找用户的主SMTP地址

PowerShell统计文件夹下文件个数的方法

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

PowerShell实现测试端口可用性脚本分享

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

PowerShell函数参数设置为即可选又必选的方法

PowerShell中使用ArrayList实现数组插入、删除、添加例子

PowerShell小技巧之执行SOAP请求

精品推荐
分类导航