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.
User avatar
Eurisko
Posts: 17
Joined: Thu Feb 14, 2013 9:06 am

Format Script hoses foreach loops in some cases.

Post by Eurisko » Mon Jul 14, 2014 7:11 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Format Script hoses foreach loops in some cases.

Post by davidc » Mon Jul 14, 2014 8:12 am

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
Joined: Thu Feb 14, 2013 9:06 am

Re: Format Script hoses foreach loops in some cases.

Post by Eurisko » Mon Jul 14, 2014 11:58 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Format Script hoses foreach loops in some cases.

Post by davidc » Mon Jul 14, 2014 1:07 pm

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.