I am trying to look for the presence of a specific registry property and if present set a variable to the value. I will be doing an If/Then/ElseIf sequence, so I don't want to wrap the hole thing in a Try/Catch to address the fact that in PS5.1 Get-ItemPropertyValue
doesn't support -errorAction:silentlyContinue
, so I really want to set the value to the execution of a code block that includes the try/catch with an empty catch, something like this...
if ($value = @{try {Get-ItemPropertyValue "Registry::$nameKey" -name:AdskProductCode}catch{}}) {
I feel like it's possible and I am close and just missing some nuance. Or perhaps this is a stupid idea and there is a better approach? They key reason for keeping the try/catch inside the if/then/else is because I will look for AdskProductCode and not finding that property I will look for AdskPackageCode, and in each case what I do with the value is different. If the try/catch wraps the iff then not finding AdskProductCode dumps me out completely and I never get to the else if looking for AdskPackageCode. Failing that there will be at least a final else, and maybe another else if, so simply looking for AdskPackageCode in the catch doesn't work. I could cascade the try/catch stuff, but good lord that's ugly.