Author Topic: ForEach Loop Issue - 2019A  (Read 49 times)

Thekub

  • Newbie
  • *
  • Posts: 13
ForEach Loop Issue - 2019A
« on: February 11, 2019, 07:31:29 am »
We run some code to manage Microsoft SCCM Collections, this code has been in use for several years.  Below is an excerpt, the behavior being seen is the ForEach loop appears to go into an infinite loop (or at least as long as we could stand to allow it to run). 

My co-worker downloaded and installed 2019A on 2/8/19 and got this behavior when running directly or running from a complied EXE.  The same .WBT I can run or compile and the code works as expected, I downloaded and installed 2019A on 1/22/19.  To rule out some issue with his upgrade he installed the same 2019A on a test system that has never had WB installed and the issue persisted.  He rolled back to one of the 2018 releases and the code returned to working order. 

Did the 2019A installer change between those ~2 weeks by chance?  Otherwise, I'm at a bit of a loss to explain this behavior.

Code: [Select]
objWMISCCMLocator = ObjectOpen("WbemScripting.SWbemLocator")
objWMISCCMServices = objWMISCCMLocator.ConnectServer("<servername>","root\sms\site_<site>",SrvAccount,SrvPassword)

InstCollection = objWMISCCMServices.Get("SMS_Collection.CollectionID='%SCCMCollectionID%'")

ForEach Rule in InstCollection.CollectionRules
If StrUpper(Rule.RuleName) == StrUpper(PCNameSelection)
ExistingRule = @TRUE
EndIf
Next

td

  • Tech Support
  • *****
  • Posts: 2725
    • WinBatch
Re: ForEach Loop Issue - 2019A
« Reply #1 on: February 11, 2019, 07:50:18 am »
The download for WinBatch+Compiler has not changed in any way since its release on January 9, 2019.  Any difference you are seeing is the result of some difference or change to the computer system and not the WinBatch+Compiler download.

The implementation of the ForEach loop was changed for 2019A to fix a rather obscure, never reported by a user bug and improve performance.  It is possible that we introduced a new bug in the processes.  Because the foreach loop was extensively tested before the release and your problem seems to be system dependent, it may be difficult to identify.  However, we will certainly investigate.   
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates

td

  • Tech Support
  • *****
  • Posts: 2725
    • WinBatch
Re: ForEach Loop Issue - 2019A
« Reply #2 on: February 11, 2019, 08:52:32 am »
The following change to your script may help narrow down the problems and may even be a workaround.  So if you are so inclined, please give it a try and report back.

Change this line:
Code: Winbatch
ForEach Rule in InstCollection.CollectionRules

to these lines:
Code: Winbatch
aRules =  InstCollection.CollectionRules
ForEach Rule in aRules
 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates

Thekub

  • Newbie
  • *
  • Posts: 13
Re: ForEach Loop Issue - 2019A
« Reply #3 on: February 11, 2019, 09:54:25 am »
My system didn't exhibit the behavior last Friday, so I installed 2019A on a spare laptop and couldn't reproduce the issue on that device.  Ill try and get my co-worker to re-upgrade his system back to 2019A and give this a shot if the behavior manifests again. 

Thekub

  • Newbie
  • *
  • Posts: 13
Re: ForEach Loop Issue - 2019A
« Reply #4 on: February 11, 2019, 10:21:07 am »
Jumped the gun on my original observation, I was able to reproduce on my test system as well confirmed the same behavior after my co-worker reinstalled 2019A.

In both cases the suggested modification caused the loop to end normally.  Likewise reverting back to the original layout caused the issue to return.

td

  • Tech Support
  • *****
  • Posts: 2725
    • WinBatch
Re: ForEach Loop Issue - 2019A
« Reply #5 on: February 11, 2019, 11:17:50 am »
The problem's source has been identified.  More testing needs to be done but the problem appears to be confined to collections of type COM Automation variant array.  The ObjectTypeGet function returns the type "ARRAY" for a variable of the type.

We should have a fix in the next release but for now, you will have to rely on the workaround solution.   We apologize for any inconvenience this may have caused and thank you for reporting the problem.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates