I’ve come across something with PS and I don’t know if I’m doing something wrong or maybe it is just a bug in PS (and I can’t find the answer anywhere)? I’ve tested this with PS v4, v5.0 and v5.1 using different Windows OS’s. First, it seems that no matter what the OS is and no matter what the PS version is, when I run the following code snippet below either directly via a PS window or via a PS script, I receive no errors and everything appears to be OK:
$error.clear()
If ($error -ne $null) { $error }
However, on specific types of systems (seeming to be an OS-based issue), I run the following code and sometimes I receive an error:
$error.clear()
If ($error -ne $null)
{
$error
}
I can run the above error-generating code on Windows 10 and Windows 2016 servers and everything is OK (no errors are reported). If I run the exact same code on Windows 2008 R2 or Windows 2012 R2, I receive the following error message:
Missing statement block after If ( condition ).
It doesn’t seem to matter whether I have PS 4, 5 or 5.1.1 installed on the Windows 2008 R2 and Windows 2012 R2 systems, I still get the error. And it doesn’t seem to matter whether I am running as Admin or not, same results. And it doesn't seem to make a difference whether they are domain-joined or workgroup systems. All of our systems are patched as of the May 2017 patch Tuesday updates.
Is it a PS requirement (on Windows 2008 R2 and Windows 2012 R2 systems) that the opening curly bracket of a PS “IF” statement must be on the same line as the IF statement itself? It doesn’t seem to be that way for Windows 10 and Windows 2016 server.
Thanks in advance,
UCG

