3

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

SOSidb
  • 533

3 Answers3

0

When PowerShell is fed a line (or block) of code - it evaluates that block (or line) as a whole

In this case, your first line is: If ($error -ne $null)

PowerShell knows how to interperate the line, but its invalid syntax because PowerShell doesn't have a "what to do" block in the event that the criteria is met.

enter image description here

If you feed PowerShell the block as a whole - it recognises the "what to do block" and knows where it needs to go next:

enter image description here

Are you able to elabourate more on which systems and platforms you are finding that accept the single lines? I have just tested this on Win10 (PowerShell 5), 2012R2 (Powershell 4 and 5), Windows 7 (PowerShell 4 and 5) and they all behave exactly the same.

Fazer87
  • 12,965
0

I had this same problem when my statement block included a here-string

If ($error -ne $null)
{
 $query = @" 
 do some sql;
 do more sql; 
 "@
}

When I changed it to one line without the here-string it worked

If ($error -ne $null)
{
 $query = "do some sql;do more sql;"
}

You may have something in your statement block that powershell doesn't allow as multiline too.

0

Powershell has two ways to parse commands, Expression mode and Argument mode. (see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.1)

Without having the different operating systems to test on, my best guess is that these commands changed from Expression mode to Argument mode (or vice versa depending on which way you go) between your OS'es.

The simplest work around (best practice) is to always leave the opening curly bracket on the top line. This allows the command/script to work regardless of which mode the parser is using.

You could also force the line to continue by adding a backtick ` to the end of the line. These are hard to see though and may introduce other issues, so your mileage may vary.

DBADon
  • 503