0
我試圖根據它的ErrorRecord.CategoryInfo.Category
枚舉值來處理錯誤。在PowerShell中測試ErrorRecord.CategoryInfo.Category
代碼:
try {
# assembly not installed on workstation
[Reflection.Assembly]::LoadWithPartialName('Oracle.DataAccess')
# throws error with a category of 'InvalidType'
$connection = New-Object Oracle.DataAccess.Client.OracleConnection($ConnectionString)
$Connection.Open()
}
catch {
# generates 'DEBUG: CategoryInfo.Category: InvalidType'
write-debug "CategoryInfo.Category: $($_.CategoryInfo.Category)"
# generates 'DEBUG: Category: InvalidType' (the `default` switch)
switch ($_.CategoryInfo.Category) {
[ErrorCategory.InvalidType] {Write-Debug "InvalidType"}
[ErrorCategory.InvalidOperation] {Write-Debug "InvalidOperation"}
default { write-Debug "Category: $($_.CategoryInfo.Category)" }
}
}
爲什麼不代碼執行,而不是default
開關ErrorCategory.InvalidType
開關?
對Referencing system.management.automation.dll in Visual Studio的接受答案表明我需要安裝system.management.automation
組件。
有沒有辦法測試$_.CategoryInfo.Category
而無需安裝此程序集?
我看不出鏈接的問題是如何相關的。 'System.Management.Automation.dll'是PowerShell的核心。你只需要引用它就可以在C#/ VbScript中使用PowerShell –