Format Script hoses foreach loops in some cases.

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 9 years and 8 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
Eurisko
Posts: 17
Last visit: Fri Jun 16, 2023 11:06 am

Format Script hoses foreach loops in some cases.

Post by Eurisko »

In PowerShell Studio 4.1.62, when I hit Format Script, it hoses several foreach statements in my code. Is there something I'm doing wrong?

What should look like this:
$erc_xml.configuration.InstanceConfigurationData.InstanceConfiguration.add | foreach { If ($_.AdfsWhr -eq $Identifier)
...
Ends up like this:
$erc_xml.configuration.InstanceConfigurationData.InstanceConfiguration.add | foreach
{
If ($_.AdfsWhr -eq $Identifier)
{...
By putting the command for the foreach on the next line, foreach won't work correctly. I had to go through and fix a bunch of these after being bit by it today. Is there something I can do to avoid this?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Format Script hoses foreach loops in some cases.

Post by davidc »

Leave it to PowerShell to use same word as keyword and cmdlet. Our parser is treating foreach as a keyword in this case instead of a cmdlet.

Until we resolve this, I recommend using the Foreach-Object (or any other alias) instead of foreach.


David
David
SAPIEN Technologies, Inc.
User avatar
Eurisko
Posts: 17
Last visit: Fri Jun 16, 2023 11:06 am

Re: Format Script hoses foreach loops in some cases.

Post by Eurisko »

Isn't there a difference in performance between Foreach vs Foreach-Object?

Several articles imply that they work a little differently and performance can be drastically different in some situations.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Format Script hoses foreach loops in some cases.

Post by davidc »

When you are using the pipeline, you are using the Foreach-Object cmdlet which is slower then the foreach construct:
PowerShell Code
Double-click the code block to select all.
foreach ($item in $items)
{
...
}
But it really depends on what you are doing. Sometimes the Foreach-Object cmdlet may be the easier way.

David
David
SAPIEN Technologies, Inc.
This topic is 9 years and 8 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.