01.$LASTEXITCODE
和$?
類(lèi)似于linux的shell腳本贮缅,powershell也可以用記錄程序退出碼的方式判斷命令是否執(zhí)行成功
其中
$?
表示最后一個(gè)操作的執(zhí)行狀態(tài)涛漂。如果最后一個(gè)操作成功聘芜,則包含 TRUE,失敗則包含 FALSE。$LASTEXITCODE
則是返回上一次執(zhí)行的退出碼,因?yàn)閘inux程序通常用退出碼0表示執(zhí)行成功复罐,所以我們判斷$LASTEXITCODE
是否為0就能判斷上次程序執(zhí)行是否成功。
總的來(lái)說(shuō)$LASTEXITCODE
雄家,邏輯上還要多繞一圈效诅,所以平時(shí)方便起見(jiàn)用$?就是了。
02.trap
trap關(guān)鍵詞可以指定讓程序終結(jié)的錯(cuò)誤發(fā)生時(shí)趟济,執(zhí)行一系列語(yǔ)句填帽。
- trap的默認(rèn)行為是在執(zhí)行完statement block里的語(yǔ)句 后顯示錯(cuò)誤,然后會(huì)繼續(xù)執(zhí)行腳本或者函數(shù)
- 在trap中使用break可以實(shí)現(xiàn)顯示錯(cuò)誤咙好,然后退出執(zhí)行
- 如果想要不顯示錯(cuò)誤繼續(xù)執(zhí)行,可以用continue
語(yǔ)法
trap [[<error type>]] {<statement list>}
statement list就是終結(jié)錯(cuò)誤發(fā)生時(shí)褐荷,會(huì)執(zhí)行的語(yǔ)句勾效。
錯(cuò)誤類(lèi)型定義了trap的處理范圍
trap處理所有終結(jié)錯(cuò)誤
沒(méi)有類(lèi)型定義的trap會(huì)在所有錯(cuò)誤發(fā)生的時(shí)候執(zhí)行。
當(dāng)一個(gè)讓程序終結(jié)的錯(cuò)誤沒(méi)有找到其他處理的腳本或命令叛甫,就會(huì)執(zhí)行trap里面的語(yǔ)句层宫,
下面是一個(gè)例子
trap {"Error found."}
function TrapTest {
trap {"Error found."}
nonsenseString
}
TrapTest
會(huì)出現(xiàn)下面的錯(cuò)誤
Error found.
nonsenseString:
Line |
3 | nonsenseString
| ~~~~~~~~~~~~~~
| The term 'nonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
trap 中使用$_
,也就是powershell自動(dòng)生成的當(dāng)前對(duì)象,就會(huì)被替換成輸出的錯(cuò)誤
function TrapTest {
trap {"Error found: $_"}
nonsenseString
}
TrapTest
Error found: The term 'nonsenseString' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
nonsenseString:
Line |
3 | nonsenseString
| ~~~~~~~~~~~~~~
| The term 'nonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
注意: trap可以在一個(gè)作用域代碼塊(scope)的任何地方定義其监,即使定義在一個(gè)代碼塊的底部萌腿,也會(huì)捕獲整個(gè)代碼塊的錯(cuò)誤
trap處理特定的錯(cuò)誤
下面是只處理沒(méi)有找到命令的錯(cuò)誤的trap,trap的使用的是.net的異常類(lèi)型
trap [System.Management.Automation.CommandNotFoundException]
{"Command error trapped"}
下面是系統(tǒng)異常類(lèi)型抖苦,System.Management.Automation.CommandNotFoundException是繼承自System.Exception的
trap [System.Exception] {"An error trapped"}
當(dāng)多個(gè)trap同時(shí)存在的時(shí)候powershell會(huì)匹配最精確特定的trap毁菱,如下
trap {"Other terminating error trapped" }
trap [System.Management.Automation.CommandNotFoundException] {
"Command error trapped"
}
nonsenseString
Command error trapped
nonsenseString:
Line |
5 | nonsenseString
| ~~~~~~~~~~~~~~
| The term 'nonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
trap捕獲錯(cuò)誤的作用域
trap捕獲錯(cuò)誤以后米死,會(huì)執(zhí)行trap內(nèi)部的語(yǔ)句,但是錯(cuò)誤處后續(xù)的代碼還會(huì)繼續(xù)執(zhí)行
下面是在函數(shù)內(nèi)部發(fā)生了錯(cuò)誤的情況贮庞,錯(cuò)誤被內(nèi)部的trap捕獲峦筒,并且函數(shù)的返回語(yǔ)句被正常執(zhí)行
function function1 {
trap { "An error: " }
NonsenseString
"function1 was completed"
}
function1
An error:
NonsenseString:
Line |
3 | NonsenseString
| ~~~~~~~~~~~~~~
| The term 'NonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
function1 was completed
下面的例子trap語(yǔ)句被放到函數(shù)外面,能夠捕獲到函數(shù)內(nèi)部的錯(cuò)誤窗慎,但是函數(shù)的返回語(yǔ)句沒(méi)有被執(zhí)行
function function2 {
NonsenseString
"function2 was completed"
}
trap { "An error: " }
function2
An error:
NonsenseString:
Line |
2 | NonsenseString
| ~~~~~~~~~~~~~~
| The term 'NonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
也就是說(shuō)物喷,trap如果在函數(shù)的外部,就可以實(shí)現(xiàn)捕獲到錯(cuò)誤就停止函數(shù)運(yùn)行的功能遮斥。
注意:多個(gè)trap定義相同類(lèi)型的錯(cuò)誤條件的時(shí)候峦失,只有最高處定義的trap會(huì)被使用
Remove-Item -ErrorAction Stop ThisFileDoesNotExist
trap { "whoops 1"; continue }
trap { "whoops 2"; continue }
注意:trap語(yǔ)句的定義域取決于它編譯的位置,如果它在一個(gè)函數(shù)或者腳本中术吗,那么當(dāng)函數(shù)或腳本退出時(shí)尉辑,他們內(nèi)部的trap語(yǔ)句就會(huì)被刪除
看下面的例子,函數(shù)外的錯(cuò)誤沒(méi)有被函數(shù)內(nèi)的trap捕獲
function function1 {
trap { "An error: " }
"function1 was completed"
}
NonsenseString
function1 was completed
NonsenseString: C:\Projects\base\test\testexit.ps1:6:1
Line |
6 | NonsenseString
| ~~~~~~~~~~~~~~
| The term 'NonsenseString' is not recognized as a name of a cmdlet, function,
| script file, or executable program. Check the spelling of the name, or if a
| path was included, verify that the path is correct and try again.
使用break和continue關(guān)鍵字
在trap中使用break和continue可以決定終結(jié)錯(cuò)誤發(fā)生時(shí)藐翎,繼續(xù)運(yùn)行還是停止
默認(rèn)沒(méi)有break關(guān)鍵字的情況下是會(huì)繼續(xù)運(yùn)行的材蹬,也會(huì)輸出錯(cuò)誤
function break_example {
trap {
"Error trapped"
break
}
1/$null
"Function completed."
}
break_example
Error trapped
ParentContainsErrorRecordException:
Line |
6 | 1/$null
| ~~~~~~~
| Attempted to divide by zero.
使用continue語(yǔ)句,powershell將從錯(cuò)誤中恢復(fù)吝镣,并且不會(huì)輸出錯(cuò)誤流
function continue_example {
trap {
"Error trapped"
continue
}
1/$null
"Function completed."
}
continue_example
Error trapped
Function completed.
綜上堤器,trap是一種比較簡(jiǎn)單的廣范圍的錯(cuò)誤處理方式,對(duì)于更細(xì)粒度的錯(cuò)誤處理末贾,建議使用try catch語(yǔ)句
03.try catch finally
try 捕獲的錯(cuò)誤闸溃,會(huì)被自動(dòng)保存到$Error變量里面,powershell會(huì)尋找catch語(yǔ)句來(lái)處理錯(cuò)誤拱撵。
這個(gè)語(yǔ)法就和c#的異常處理比較像
語(yǔ)法
try {<statement list>}`
catch [[<error type>][',' <error type>]*] {<statement list>}
finally {<statement list>}
捕獲錯(cuò)誤
try { NonsenseString }
catch { "An error occurred." }
An error occurred.
使用多個(gè)catch語(yǔ)句
try {
$wc = new-object System.Net.WebClient
$wc.DownloadFile("http://www.contoso.com/MyDoc.doc","c:\temp\MyDoc.doc")
}
catch [System.Net.WebException],[System.IO.IOException] {
"Unable to download MyDoc.doc from http://www.contoso.com."
}
catch {
"An error occurred that could not be resolved."
}
在try catch語(yǔ)句中使用trap
當(dāng)try語(yǔ)句中包含trap辉川,即使聲明了catch語(yǔ)句,也會(huì)執(zhí)行trap拴测,如果trap的位置比try更高乓旗,并且沒(méi)有catch語(yǔ)句,trap也會(huì)獲得控制集索,即使父級(jí)作用域中包含匹配的catch語(yǔ)句屿愚。
訪(fǎng)問(wèn)錯(cuò)誤信息 ACCESSING EXCEPTION INFORMATION
catch語(yǔ)句中的$_就包含了錯(cuò)誤信息
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_
}
An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
你還可以用$_打印堆棧信息
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
An Error occurred:
at <ScriptBlock>, <No file>: line 2
使用FINALLY來(lái)釋放資源
不管try塊是否遇到錯(cuò)誤,finally語(yǔ)句都會(huì)執(zhí)行务荆。即使catch中使用exit退出腳本妆距,或者使用ctrl+c中止腳本都會(huì)執(zhí)行。