有點像perl中的<statement> || die
,這個東西很簡潔,我可以把它放在每個關鍵語句中,以避免在腳本的其他部分出錯時打擾powershell。在PowerShell中是否存在「... || die」等價物?
回答
大多數命令都支持-ErrorAction
通用參數。指定-ErrorAction Stop
通常會在發生錯誤時暫停腳本。見Get-Help about_CommonParameters
。
默認情況下,-ErrorAction
是Continue
。您可以通過更改$ErrorActionPreference
的值來更改默認選項。見Get-Help about_Preference_Variables
。
如果詳細程度確實是一個問題,-ErrorAction
別名爲-ea
。
當時這對我有用,非常感謝! – 2018-01-22 09:38:26
另一種在PowerShell中實現類似...|| die
的構造的方法,無需添加巨大的try-catch構造,可以使用自動變量$?
。
從Get-Help about_Automatic_variables
:
$?
Contains the execution status of the last operation. It contains
TRUE if the last operation succeeded and FALSE if it failed.
只需添加下面的右各關鍵語句後:
if(-not $?){
# Call Write-EventLog or write $Error[0] to an xml or txt file if you like
Exit(1)
}
謝謝Mathaias! – 2018-01-22 09:38:00
@Lyt_Seeker非常歡迎^ _ ^請點擊左邊的複選標記,考慮標記我的答案「accepted」 – 2018-01-22 11:38:00
那時候,培根比特這樣的解決方案爲我工作; 我已經使用$?後來換了其他的東西,但他的回答爲我解決了。感謝您在這裏再次輸入! – 2018-01-22 13:36:37
- 1. 在PowerShell中是否存在「this」的等價物?
- 2. WeakHashMap是否存在java.util.concurrent等價物?
- 3. C#'params'是否存在JavaScript等價物?
- 4. Axiis是否存在非Adobe等價物?
- 5. 在Java中是否有TweenMax等價物
- 6. 在Windows中是否有XEmbed等價物?
- 7. 在Python中是否有Rake等價物?
- 8. 在Doxygen中是否有@inheritDoc等價物?
- 9. 在SQL Server Management Studio中是否存在SELECT ... INTO OUTFILE等價物?
- 10. 在Python 3中是否存在與tokenize import pseudoprog.match的等價物?
- 11. GORM中是否存在「不在」等價物?
- 12. 在jquery中是否存在一個Angular Multiple選擇等價物?
- 13. 在C#中是否存在boost :: shared_ptr <T>等價物?
- 14. 在Ruby 1.8.5中是否存在Array#each_slice()的等價物?
- 15. 在Swift中是否存在decltype等價物?
- 16. Java Swing中是否存在「Group Box」等價物?
- 17. MySQL中是否存在T-SQL PARSEONLY標誌等價物?
- 18. ASP.NET MVC中是否存在JavaScript生成器的等價物?
- 19. System.Xml.Linq中是否存在System.Xml.XmlAttribute.Specified的等價物?
- 20. Flex中是否存在C#的Path.GetExtension等價物?
- 21. PowerShell中是否有Maven或Yum或Chocolatey等價物?
- 22. 什麼是bash $ *的Powershell等價物?
- 23. 廚師中是否有puppet_nist等價物?
- 24. 它是否存在Java的Wicket的Ruby等價物?
- 25. DataGridTemplateColumn是否存在TextBlock ScrollViewer.CanContentScroll =「True」的代碼隱藏等價物?
- 26. 是否存在。=用於自連接的JavaScript等價物?
- 27. 是否存在Apache CXF Framework的.Net等價物?
- 28. 是否存在Rails遷移的PHP等價物?
- 29. SQL腳本 - 是否存在#define的等價物?
- 30. 是否存在pythonic的「if __name__ ==」__main__「:main()」的R等價物?
usualy,在$ ErrorPreferenceVariable被設置爲 '停止' 和一個try/catch結構來處理錯誤 – 2015-04-02 11:19:17