Page 1 of 1

Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 2:00 pm
by owinsloe
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.160
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0

Hi guys, A slightly annoying issue with powershell is that it does not recognize the object script blocks with an open brace on a newline. I am in the habit (rightly or wrongly) of placing braces on a newline (except under these situations) and occasionally I forget.

PSS does not see this as an error and the script packages up ok but when you go to run it you get that awesome blocking user input request

cmdlet ForEach-Object at command pipeline position 1
Supply values for the following parameters:

eg.
This is good...
(1..3) | ?{ $_ -eq 1 }

This is NOT...
(1..3) | ?
{
$_ -eq 1
}

Maybe a future PS build will handle this but in the meantime would it be possible for PSS to detect this situation as an ERROR?

Thanks

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 2:16 pm
by davidc
Please try the latest service build of PowerShell Studio (v165). It should resolve this issue.

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 2:40 pm
by owinsloe
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.165
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0

Hi David,
That would have been amazing (and coincidental) had it resolved the issue, but it did not.

I pasted this into a new script and run through the PSS editor and it raised the smdlet where-object user input dialog and the editor did not indicate this was a problem by display a red bang.

(1 .. 3) | Where-Object
{
$_ -eq 1
}

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 2:51 pm
by davidc
My apologies, I misunderstood the request. I thought you were reporting a formatting issue where PowerShell Studio was placing the opening bracket on a new line.

As for errors, it is PowerShell engine that reports the syntax errors. Even though your intention was for the script block to be part of the Where-Object command, according to PowerShell, this is a valid statement and there is nothing syntactically wrong with it.

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 3:32 pm
by owinsloe
David,
No problem and understand what you are saying. Yes, syntax wise, it's fine, but if your intention is to have a non blocking script to run that does not require user input then this unexpected behavior can be very frustrating.

Maybe if I try a new approach, rather than PSS reporting this as an error, perhaps it could raise a warning. Something along the lines of...

"Placing a script block following an object statement on a newline will cause Powershell to request user input. Are you sure that you wish to do this?".

...or something along those lines. I know it's not really the responsibility of PSS but it would be a nice feature (perhaps an optional feature)

Thanks

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 3:49 pm
by davidc
I understand and thank you for the suggestion. I wonder if this would make more sense as a PSScriptAnalyzer rule.

Re: Error object blocks with open brace on newline

Posted: Tue Jun 18, 2019 4:12 pm
by owinsloe
Hmmmm, yes, that seems like a logical place. Thanks for the feedback David.
Cheers