PowerShell: Try...Catch...Finally 實(shí)現(xiàn)方法
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..."
}
相關(guān)文章
Powershell中的文件夾共享及磁盤(pán)映射的操作步驟
本文介紹了如何在Windows下使用Powershell和cmd實(shí)現(xiàn)文件夾共享,內(nèi)容包括查看共享列表、創(chuàng)建共享文件夾、驅(qū)動(dòng)器映射和共享訪問(wèn)、刪除共享等操作步驟,感興趣的朋友跟隨小編一起看看吧2025-02-02
PowerShell中使用正則表達(dá)式跨行匹配字符串的方法
這篇文章主要介紹了PowerShell中使用正則表達(dá)式跨行匹配字符串的方法,重點(diǎn)在于正則表達(dá)式的寫(xiě)法,需要的朋友可以參考下2014-08-08
PowerShell DSC組件 xExchange 發(fā)布
這篇文章主要介紹了PowerShell DSC組件 xExchange 發(fā)布,xExchange實(shí)現(xiàn)可以在PowerShell中使用DSC來(lái)部署和配置Exchange,需要的朋友可以參考下2015-04-04
PowerShell實(shí)現(xiàn)時(shí)間管理小秘書(shū)
這篇文章主要介紹了PowerShell實(shí)現(xiàn)時(shí)間管理小秘書(shū),本文是一個(gè)PowerShell的綜合編程實(shí)例,實(shí)現(xiàn)了一個(gè)用來(lái)管理時(shí)間的功能,需要的朋友可以參考下2015-04-04

