Hi Kaplan, the issue arises because in PowerShell, when you reference a variable inside a string that also contains a colon (:), the parser may misinterpret it. In your catch block, the line:
Write-Error "Error querying $ComputerName: $_"
is causing the parser error. To fix this, you can wrap the variable reference in curly braces to clearly delimit it, like this:
Write-Error "Error querying ${ComputerName}: $_"
This ensures PowerShell correctly interprets $ComputerName as a variable, followed by a colon and the error object. Alternatively, you can use string concatenation:
Write-Error ("Error querying " + $ComputerName + ": " + $_)
After making this change, your script should run without the parser error and properly display the error message when a query fails.
I hope this helps you get your script working smoothly! If you find this answer helpful, please don’t forget to hit “Accept Answer” 🙂.
Harry.