I am seemingly confused with regards to try/catch/finally. As I understand it, what I have here should attempt to get the OS data, and if it errors the catch would do.. nothing, and then finally would return a blank value. This seems to me to be fine, because if this bit of code can't get the data I need, the entire script is just going to exit. I imagine the issues I am trying to catch are a very old OS or permissions or what have you, which don't actually apply on my test machine, so I attempt to create an error, by, say misspelling 'win32_Processor!' with the extraneous exclamation point. Instead of returning blank, I get Get-WmiObject : Invalid query. Now, I could try to catch the specific error, but I had thought I could catch all errors this way. I also tried catch [system.exception], but this isn't actually a system.exception.
So, is the right answer a generic catch all? And if so, how do you do it? And if not, what is the best way to error trap here?
Gordon
function Get-OperatingSystemID {
try {
$OSArch = (Get-WMIObject -Class Win32_Processor).AddressWidth
if ((Get-WMIObject -Class Win32_OperatingSystem).Version -Match "^[0-9]\.[0-9]") {
$OSVersion = [decimal]$matches[0]
}
}
catch {
}
finally {
return $OSVersion, $OSArch
}
}