有點像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. JavaScript中是否存在getColorBoundsRect()等價物?
- 3. Powershell中是否存在「拆分」等價物?
- 4. WeakHashMap是否存在java.util.concurrent等價物?
- 5. Axiis是否存在非Adobe等價物?
- 6. 是否存在0x80的ascii等價物?
- 7. IBM Worklight - 是否存在Window.open()等價物?
- 8. 是否存在與SELECT ... COUNT(*)... GROUP BY ...等價的等價物?
- 9. Java中是否存在std :: bitset等價物(或類似物)?
- 10. 在Python中是否有sessionInfo()等價物?
- 11. 在Java中是否有TweenMax等價物
- 12. 在java中是否有array_intersect()等價物?
- 13. 在Doxygen中是否有@inheritDoc等價物?
- 14. 在Python中是否有Rake等價物?
- 15. 在Ramda中是否有R.notEquals等價物?
- 16. 在Windows中是否有XEmbed等價物?
- 17. 在C#中是否存在boost :: shared_ptr <T>等價物?
- 18. 在Ruby中是否存在addslashes等價物?
- 19. 在Swift中是否存在decltype等價物?
- 20. 在C++中是否存在C#SecureString等價物?
- 21. 在C#Windows窗體中是否存在QTimer :: singleShot等價物?
- 22. 在jquery中是否存在一個Angular Multiple選擇等價物?
- 23. 在C++/CLI中是否存在C#的不安全等價物?
- 24. 在Python 3中是否存在與tokenize import pseudoprog.match的等價物?
- 25. 在SQL Server Management Studio中是否存在SELECT ... INTO OUTFILE等價物?
- 26. 在Ruby 1.8.5中是否存在Array#each_slice()的等價物?
- 27. 在AS3中是否存在MemberwiseClone()等價物?
- 28. GORM中是否存在「不在」等價物?
- 29. C++中是否存在Python中的「in」函數的等價物?
- 30. Unity具有CreateChildContainer,SimpleInjector或LightInject中是否存在等價物
usualy,在$ ErrorPreferenceVariable被設置爲 '停止' 和一個try/catch結構來處理錯誤 – 2015-04-02 11:19:17