"Format Script" moving "{" causing block to not execute

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
HRPlano
Posts: 3
Last visit: Fri Jul 28, 2023 12:08 pm

"Format Script" moving "{" causing block to not execute

Post by HRPlano »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2017, 5.4.145
32 or 64 bit version of product: 64
Operating system: Windows 10 Pro
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

Hello. I have the following section of code:

gci -Path $FolderPath -File | Sort-Object -Property LastWriteTime -Descending | Select FullName -First 1 | %($_){
Copy-Item $_.FullName -Destination $DestinationPath (continues down until "}"

When I run the "Format Script" command, it is moving the "{" on the first line down, so that I have:

gci -Path $FolderPath -File | Sort-Object -Property LastWriteTime -Descending | Select FullName -First 1 | %($_)
{
Copy-Item $_.FullName -Destination $DestinationPath

Then, when running the script, it doesn't execute the script block, instead just outputs the text of the script to the screen. After a couple of hours of trying to figure out what I'd done, I tried moving it back to its original position, and all is good again.

I guess 2 questions, is this normal PowerShell/PowerShell Studio behavior, and/or, is there anything I could do to prevent a similar error (other than not run the Format Script)?

Thanks very much!
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: "Format Script" moving "{" causing block to not execute

Post by davidc »

Thank you for reporting this issue. It is the ($_) part of the Foreach call that is triggering the parenthesis formatting rule.

Is there a reason that you have the ($_) in the ForEach-Object cmdlet statement instead of the following?

Code: Select all

 | % { Copy-Item $_.FullName ... }
I ask because I never have seen it called this way.
David
SAPIEN Technologies, Inc.
User avatar
HRPlano
Posts: 3
Last visit: Fri Jul 28, 2023 12:08 pm

Re: "Format Script" moving "{" causing block to not execute

Post by HRPlano »

Thanks, David. To be honest, I "borrowed" the code from some website, so, for better or worse, I can't claim credit for authoring.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.