larksp

For loop in a for loop search with array for string

8 posts in this topic

hopefully i got that right in the title :)

so it loops through each of the updates in my case 4.. i dont want skype but have tried to add an option to stop more than just skype 
the loop is not right and unsure how to get right

This is just some of the testing code as does not actual need download the updates while testing
"Definition Update"                 -->    Only using as a test for array. to make sure it works and it does not lol :)

 

Local $Hideup[2]
$Hideup[0] = "Skype for Windows"
$Hideup[1] = "Definition Update"



$updateSession = ObjCreate("Microsoft.update.Session")
$updateSearcher = $updateSession.CreateupdateSearcher()

ConsoleWrite("Searching for updates..." & @CRLF)

$searchResult = $updateSearcher.Search("IsInstalled=0 and Type='Software' and IsAssigned=0")
; A LIST OF UPDATES
For $I = 0 To $searchResult.Updates.Count - 1
    $update = $searchResult.Updates.Item($I)
    ConsoleWrite($I + 1 & "> " & $update.Title & @CR)
;~  if StringInStr( $update.Title, $Hideup[0]) Then ConsoleWrite(@CRLF & 'YIPEE' & @CRLF) ; only check for 1 item
Next
If $searchResult.Updates.Count = 0 Then
    ConsoleWrite("There are no applicable updates.")
    Exit
EndIf


ConsoleWrite(@CRLF & "Creating collection of updates to download:" & @CR)
$updatesToDownload = ObjCreate("Microsoft.update.UpdateColl")
For $I = 0 To $searchResult.Updates.Count - 1
    $update = $searchResult.Updates.Item($I)
;~  ****************************************
;~  ****************************************
;~  ****************************************
    For $j = 0 To UBound($Hideup) -1
        If StringInStr($update.Title, $Hideup[$j]) Then
            ConsoleWrite('!' & $I + 1 & "> Removing: " & $update.Title & @CR)
            ExitLoop
        Else
            ConsoleWrite($I + 1 & "> adding: " & $update.Title & @CR)
            $updatesToDownload.Add($update)
        EndIf
    Next
    ;~  ****************************************
;~  ****************************************
;~  ****************************************
Next

 

so 1 worked and then is start getting doubles ups

the results i get from console

Searching for updates...
1> Skype for Windows desktop 7.3 (KB2876229)
2> Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.249.211.0)
3> August, 2017 Preview of Quality Rollup for .NET Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 on Windows 7 and Server 2008 R2 for x64 (KB4035036)
4> Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.251.546.0)

Creating collection of updates to download:
!1> Removing: Skype for Windows desktop 7.3 (KB2876229)
2> adding: Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.249.211.0)
!2> Removing: Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.249.211.0)
3> adding: August, 2017 Preview of Quality Rollup for .NET Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 on Windows 7 and Server 2008 R2 for x64 (KB4035036)
3> adding: August, 2017 Preview of Quality Rollup for .NET Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 on Windows 7 and Server 2008 R2 for x64 (KB4035036)
4> adding: Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.251.546.0)
!4> Removing: Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.251.546.0)

 

 

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

any suggestions ?

the only way i can work it out is doing it by 

if StringInStr( $update.Title, $Hideup[0]) Then
        ConsoleWrite('!' & $I + 1 & "> Removing: " & $update.Title & @CR) ; only check for 1 item
    ElseIf StringInStr( $update.Title, $Hideup[1]) Then
        ConsoleWrite('!' & $I + 1 & "> Removing: " & $update.Title & @CR) ; only check for 1 item
    Else
    endif

but then if i had an array of 20... e.g $hideup[20] that is alot of elseif 

Edited by larksp

Share this post


Link to post
Share on other sites

Caveat: I can't run your code due to priv restrictions where I am. It would be a lot easier if you built 1 array of the updates you have available and 1 array of the updates you would like to hide and made a simple standalone example.

The logic of the $j loop appears in error: For each item in $Hideup you are going to test your update and remove it if it matches and exit loop, but for each item in $Hideup you don't match to you are going to add it to your download list... surely you only want to add it once and only if no matches occur? Equally if you don't match the first one, you will add it, only to then remove it later...
    
Pseudo code
$I loop

  $match = false
  
  For $j = 0 To UBound($Hideup) -1
    if in list {$match = true, exitloop}
  Next <---- finish the loop first
  
  if $match = false {add to list to download} <---- conditional dependent upon the completed check of $Hideup

next $I

1 person likes this

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The main reason i went with If StringInStr   is so the name dont have to 100% match

$Hideup[0] = "Skype for Windows"
Removing:
Skype for Windows desktop 7.3 (KB2876229)
as next month it might be skype 7.4 for example

the idea i was going for was.. as it search for updates if that update was skype (or in $hideup (List)) it would skip it and move onto the next update there for not adding it to the download list  <--- but i am doing the loop wrong i guess not fully understanding what it is actually doing to what i want it to do

the "" if in list ""  i did not know that was a thing as cant F1 the word List so something i will have to look up on for details could be a usefull thing

 

Edited by larksp

Share this post


Link to post
Share on other sites

I appreciate why you are using StringInStr , I wrote "pseudo code" it defines the logic you require, not the actual code. "if in list" is whatever check you want to use e.g 

If StringInStr($update.Title, $Hideup[$j])

is fine. Your problem is your logic.


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites

ok thanks i will have a play now...

i think im starting to understand to why using = true or false then then checking for true of false before then adding

Share this post


Link to post
Share on other sites

I'm using $match as a switch. Each time you initially enter the $j loop $match is false. If nothing is found in the loop, then $match will still be false and you know that the update is not in your hide list. If you get a match somewhere in the $j loop then you set $match to true (and for the sake of efficiency exit the loop), when the loop has completed if $match is true you know your update is to be skipped. 


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites

thank you i have the console read out ok now

Searching for updates...
1>  Skype for Windows desktop 7.3 (KB2876229)
2> August, 2017 Preview of Quality Rollup for .NET Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 on Windows 7 and Server 2008 R2 for x64 (KB4035036)
3> Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.251.813.0)

Creating collection of updates to download:
!1> Removing: Skype for Windows desktop 7.3 (KB2876229)
2> adding: August, 2017 Preview of Quality Rollup for .NET Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 on Windows 7 and Server 2008 R2 for x64 (KB4035036)
!3> Removing: Definition Update for Microsoft Security Essentials - KB2310138 (Definition 1.251.813.0)

now to check it actually download and seeing it not install the hideup

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now