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

  • What happens if your starting curly bracket is on the same line as your if condition, rather than on the next line? I know PowerShell, in theory, doesn't care about placement around curly brackets, but for completeness to rule out anything like strange new line characters etc. Are you using Notepad, PowerShell IDE or another editor? – Kinnectus Jun 14 '17 at 14:50
  • this depends entirely on the parser in use. I would expect the powershell parser to be internal, but since it will work with a dozen differant script dialects, perhaps that is not entirely correct. Either way, I recommend using RKR formatting, and put the opening curly on the line with the control statement. I've never liked wasting an entire line with an opening curly. – Frank Thomas Jun 14 '17 at 15:38
  • @Kinnectus - When the starting curly bracket is on the same line, we receive no errors (but that only applies to Windows 2008 R2 and Windows 2012 R2). Windows 10 and Windows 2016 don’t seem to mind if the bracket in on a new line or not. – STGdb Jun 14 '17 at 16:57
  • @Frank Thomas – Running it directly from a PS window (the one built into the Windows OS) produces the error. I deployed a clean server OS in a workgroup (directly from MS ISO media), got the same results on the new 2012 R2 server (nothing “extra” was installed on it and the OS was not patched). It definitely seems to be OS related though. – STGdb Jun 14 '17 at 16:57
  • A lot of our (existing) scripts are formatted that way (with the opening IF statement curly bracket on a new line) for readability. The other day, I wanted to add in some error trapping to an existing script and when testing, I noticed that this error was being reported (and trapped), so the script was exiting out. I assume this has been occurring for a while now and we never knew that it was until I tried to add in some error trapping stuff. Thank you both for your replies. – STGdb Jun 14 '17 at 16:57
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.