What is wrong with my script?

Discussion in 'Scripting' started by Super Spartan, Sep 4, 2015.

  1. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    They may be a Powershell script out in web land some where which can do what you want ?
     
  2. Super Spartan

    Super Spartan MDL Expert

    May 30, 2014
    1,709
    990
    60
    Yes I'll need to research more on my own :(
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. RickC

    RickC MDL Novice

    Sep 1, 2015
    4
    3
    0
    #23 RickC, Sep 6, 2015
    Last edited by a moderator: Apr 20, 2017
    Remove the preceding apostrophe from the IF condition (i.e. Wscript.echo "No match found for " & hideupdates(j)) then run the script from an elevated command prompt and it will work.

    Continually clicking on the OK buttons on the dialogs quickly got boring so I changed the script's ECHOs to write to an output file instead, except for the last "Finished" dialog.

    The following code works without error. Note that it takes a very long time to work its way through the list of updates. (AFAIK 0 is considered a number so the hideupdates array 0-22 needs to be Dim(ensioned) to 23.)

    Code:
    Dim fso
    Dim hideupdates(23)
    
    hideupdates(0) = "KB2505438"
    hideupdates(1) = "KB2670838"
    hideupdates(2) = "KB2952664"
    hideupdates(3) = "KB2976978"
    hideupdates(4) = "KB2977759"
    hideupdates(5) = "KB2990214"
    hideupdates(6) = "KB3021917"
    hideupdates(7) = "KB3022345"
    hideupdates(8) = "KB3035583"
    hideupdates(9) = "KB3044374"
    hideupdates(10) = "KB3064683"
    hideupdates(11) = "KB3065987"
    hideupdates(12) = "KB3065987-v2"
    hideupdates(13) = "KB3065988"
    hideupdates(14) = "KB3065988-v2"
    hideupdates(15) = "KB3068708"
    hideupdates(16) = "KB3072318"
    hideupdates(17) = "KB3075249"
    hideupdates(18) = "KB3075851"
    hideupdates(19) = "KB3075853"
    hideupdates(20) = "KB3080149"
    hideupdates(21) = "KB3083324"
    hideupdates(22) = "KB3083325"
    
    Set fso = WScript.CreateObject("Scripting.Filesystemobject")
    Set f = fso.CreateTextFile("C:\Temp\output.txt", 2)
    
    Set updateSession = createObject("Microsoft.Update.Session")
    Set updateSearcher = updateSession.CreateupdateSearcher()
    
    Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
    
    For i = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(i)
    For j = LBound(hideupdates) To UBound(hideupdates)
    If instr(1, update.Title, hideupdates(j), vbTextCompare) = 0 Then
    f.WriteLine "No match found for " & hideupdates(j)
    Else
    f.WriteLine "Hiding " & hideupdates(j)
    update.IsHidden = True
    End If
    Next
    Next
    f.Close
    WScript.echo "Finished"
    Unfortunately, although there's no longer any visible error, the output.txt file shows it's constantly looping through the list of updates until the script finishes. Not only that, a check in Control Panel > Windows Update > Restore hidden updates shows it's hiding far more updates than reported in the output.txt file. Sorry but my VBS knowledge isn't good enough to fix this.
     
  4. mysteriously

    mysteriously Guest

    #24 mysteriously, Sep 9, 2015
    Last edited by a moderator: Apr 20, 2017
    You may want to try this

    1hideKBs.cmd


    Code:
    @echo off&cls&title Hide updates. Remember to be connected to internet!
    
    echo Checking for internet connectivity needed to perform updates...
    echo This will try to ping google.com 4 times.
    echo If ping successful will update, otherwise continue without updates.
    
    
    ping -n 4 google.com|find "TTL" >nul
    IF NOT ERRORLEVEL 1 goto :internet
    IF     ERRORLEVEL 1 goto :end
    :internet
    echo.
    echo Internet connection detected.
    echo.
    
    
    echo HideKbs.vbs
    cscript //nologo "%~dp0hideKBs.vbs"
    
    
    echo.
    echo Finished.
    ping localhost -n 5 >nul
    
    
    :end

    hideKBs.vbs

    Code:
    ' Windows 7 hide KBs - ndog' last updated - 06.04.2016 by mysteriously MDL
    
    
    Dim hideupdates(8)
    
    
    hideupdates(0) = "KB3035583"
    hideupdates(1) = "KB2952664"
    hideupdates(2) = "KB2977759"
    hideupdates(3) = "KB3021917"
    hideupdates(4) = "KB3068708"
    hideupdates(5) = "KB3075249"
    hideupdates(6) = "KB3080149"
    hideupdates(7) = "KB3081954"
    hideupdates(8) = "KB3123862"
    
    
    set updateSession = createObject("Microsoft.Update.Session")
    set updateSearcher = updateSession.CreateupdateSearcher()
    
    
    Set searchResult = updateSearcher.Search("IsHidden=0 and IsInstalled=0 and Type='Software'")
    
    
    For i = 0 To searchResult.Updates.Count-1
        set update = searchResult.Updates.Item(i)
        For j = LBound(hideupdates) To UBound(hideupdates)
            if instr(1, update.Title, hideupdates(j), vbTextCompare) = 0 then
                  Wscript.echo "No match found for " & hideupdates(j)
            else
                Wscript.echo "Hiding " & hideupdates(j)
                update.IsHidden = True
            end if
        Next
    Next
    
    
    msgbox "Press any key to continue", VBOKOnly, "Attention" 
    source and credits to NDog http://ss64.org/viewtopic.php?id=1345

    This is the script I use, customized on my own. Customize it for yourself :)

    Start using 1hideKBs.cmd both files needs to be in the same folder. This worked for me. I've hidden 3 more updates (than manually hiding) and got "No match found" when updates were not available or already hidden.

    [​IMG]
     
  5. Super Spartan

    Super Spartan MDL Expert

    May 30, 2014
    1,709
    990
    60
    can anyone confirm if this works? I don't have a Windows 7 system at the moment to test
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. RickC

    RickC MDL Novice

    Sep 1, 2015
    4
    3
    0
    #26 RickC, Sep 14, 2015
    Last edited by a moderator: Apr 20, 2017
    I tried 1hideKBs.cmd and hideKBs.vbs on a Win 7 VM. hideKBs.vbs failed with an error message: Microsoft VBScript compilation error: Unterminated string constant.. It turned out that there were a couple of concatenation errors in the script. Had to change the script to:

    Code:
    ' Windows 7 hide KBs - ndog
    ' last updated - 09.09.2015 by mysteriously MDL
    
    Dim hideupdates(36)
    
    hideupdates(0) = "KB2592687" 'Remote Desktop Protocol 8.0
    hideupdates(1) = "KB2861855" 'Remote Desktop Protocol 8.0
    hideupdates(2) = "KB2574819" 'Remote Desktop.
    hideupdates(3) = "KB2830477" 'Remote Desktop
    hideupdates(4) = "KB2709981" 'Windows Media Player 12
    hideupdates(5) = "KB2803821" 'Windows Media Player 12
    hideupdates(6) = "Bing Desktop" 'With this we get all versions
    hideupdates(7) = "Internet Explorer 8"
    hideupdates(8) = "Internet Explorer 9"
    hideupdates(9) = "Internet Explorer 10"
    hideupdates(10) = "KB2673774" 'Bing Bar 7.3 KB
    hideupdates(11) = "Bing Bar" 'Bing Bar ALL
    hideupdates(12) = "KB2952664" 'Win10 upgrade update
    hideupdates(13) = "KB2977759" 'Win10 upgrade update
    hideupdates(14) = "KB3021917" 'Win10 upgrade update
    hideupdates(15) = "KB3035583" 'Get Windows 10 app
    hideupdates(16) = "KB3068708" 'Diag&Telemetry track svc
    hideupdates(17) = "KB3075249" 'Telemetry to consent.exe
    hideupdates(18) = "KB3080149" 'Diag&Telemetry track svc
    hideupdates(19) = "KB2990214" 'Win10 upgrade update
    hideupdates(20) = "KB3022345" 'Diag&Telemetry track svc
    hideupdates(21) = "KB3050265" 'Win10 upgrade update
    hideupdates(22) = "KB3065987" 'WU Win10 upgrade update
    hideupdates(23) = "KB3075851" 'WU Win10 upgrade update
    hideupdates(24) = "KB3083324" 'WU Win10 upgrade update
    hideupdates(25) = "KB3080333" 'MS Silverlight
    hideupdates(26) = "KB2902907" 'MSE
    hideupdates(27) = "KB3063822" 'MSE
    hideupdates(28) = "KB2876229" 'Skype 7.3
    hideupdates(29) = "KB3048761" 'MS Silverlight
    hideupdates(30) = "KB2977218" 'MS Silverlight
    hideupdates(31) = "KB2636927" 'MS Silverlight
    hideupdates(32) = "KB2668562" 'MS Silverlight
    hideupdates(33) = "KB3056819" 'MS Silverlight
    hideupdates(34) = "KB2617986" 'MS Silverlight
    hideupdates(35) = "KB2512827" 'MS Silverlight
    hideupdates(36) = "KB2526954" 'MS Silverlight
    
    
    set updateSession = createObject("Microsoft.Update.Session")
    set updateSearcher = updateSession.CreateupdateSearcher()
    
    Set searchResult = updateSearcher.Search("IsHidden=0 and IsInstalled=0 and Type='Software'")
    
    For i = 0 To searchResult.Updates.Count-1
        set update = searchResult.Updates.Item(i)
        For j = LBound(hideupdates) To UBound(hideupdates) 
            'MsgBox hideupdates(j)
            if instr(1, update.Title, hideupdates(j), vbTextCompare) = 0 then
                  Wscript.echo "No match found for " & hideupdates(j)
            else
                Wscript.echo "Hiding " & hideupdates(j)
                update.IsHidden = True
            end if
        Next
    Next
    
    msgbox "Press any key to continue", VBOKOnly, "Attention"
    After this the script worked to completion without error and hid the updates it found. Note that it's very slow and created a lot of CPU activity (although this could be because it was running on a VM).
     
  7. mysteriously

    mysteriously Guest

    You should start 1hideKBs.cmd instead of hideKBs.vbs. Dunno why, but in post there were 2 empty lines in places where they should not be. Fixed.

    It is 'slow' because it checks for updates. Also it should be launched few times on clean windows installation. It all depends on the updates you want to hide. If you hide Update_KB_x_v5, then next update check will offer you Update_KB_x_v4. In other words if you hide the newest version of an update, then WU will offer you older version of the same update on next updates check
     
  8. RickC

    RickC MDL Novice

    Sep 1, 2015
    4
    3
    0
    Hi mysteriously,

    Yes, I ran 1hideKBs.cmd (as Administrator) instead of hideKBs.vbs. After I got rid of the 2 empty lines in hideKBs.vbs it worked like a charm. Perhaps I should have made this clearer in my previous post.

    Many thanks for posting the scripts and for this additional information. It hadn't occurred to me about WU subsequently offering older versions of updates so I'm really glad you mentioned it.
     
  9. mysteriously

    mysteriously Guest

    #29 mysteriously, Oct 14, 2015
    Last edited by a moderator: Oct 15, 2015
    1,5 GB svchost.exe RAM usage when checking updates was unacceptable, so I removed Windows Update Client updates from hide updates script and let it update to fix RAM usage.
     
  10. mysteriously

    mysteriously Guest

    #30 mysteriously, May 3, 2016
    Last edited by a moderator: Apr 20, 2017
    The following script is better:
    https://github.com/WindowsLies/BlockWindows/blob/master/HideWindowsUpdates.vbs

    Just put the .vbs in same directory as

    RunHideWindowsUpdates.cmd
    Code:
    @echo off&cls&title RunHideWindowsUpdates.vbs
    
    :getAdmin
    @echo off
    ::Check for permissions
        IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
            >nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
    )    ELSE (
            >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    )
    
    ::if error flag set, you do NOT have admin rights.
        if '%errorlevel%' NEQ '0' (
            echo Requesting administrative privileges...
            goto UACPrompt
    )     else ( goto gotAdmin )
    
    :UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B
    
    :gotAdmin
    ::checkOS
    setlocal
    for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
    if NOT "%version%" == "6.1" goto :noWin7
    
    ::win7
    endlocal
    
    goto :hideUpdates
    
    :hideUpdates
    
    ::Never check for updates
    reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" /v AUOptions /t REG_DWORD /d 1 /f
    
    cd /d %windir%\system32\
    
    ::hide updates
    echo Checking for internet connectivity...
    echo Trying to ping google.com 4 times.
    echo if successful, the batch will hide GWX and telemetry-related updates.
    echo Otherwise it will try again.
    
    ping -n 4 google.com|find "TTL" > NUL
    if     ERRORLEVEL 1 goto :noInternet
    
    ::internet
    echo Internet connection detected.
    
    ::restart WU service
    sc stop wuauserv > NUL
    ::just in case
    timeout/t 15
    sc start wuauserv > NUL
    
    cd /d "%~dp0"
    start "title" /b /wait cscript.exe "HideWindowsUpdates.vbs" 2952664 2977759 3021917 3035583 3068708 3075249 3080149 3081954 3123862
    echo Finished. Updates hidden.
    
    ::restore previous setting
    reg delete "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" /f
    echo Previous Windows Update setting has been restored.
    
    pause
    exit
    
    :noWin7
    echo You're NOT running Windows 7. The batch will terminate.
    pause
    exit /b
    
    :noInternet
    echo Internet connection failure.
    echo Retrying soon...
    timeout/t 15
    goto :hideUpdates

    You may want to replace
    Code:
    2952664 2977759 3021917 3035583 3068708 3075249 3080149 3081954 3123862
    with your own set of updates
     
  11. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #31 Compo, May 4, 2016
    Last edited by a moderator: Apr 20, 2017
    This batch sequence appears to me to be in the wrong order!

    You run a check for architecture in order to run the appropriate command to check for admin.
    Then you check to see if you're on the correct OS.
    You continue by force changing the end users Windows Update Policy.
    Next you determine if there is an internet connection.
    You proceed to blindly stop a service wait for a set time then start it again.
    Run the VBScript.
    Delete a registry key on the end users system.

    If they're running the wrong OS, then it makes little sense to have gone through the OS architecture check, run cacls, write a vbscript to their PC, run that vbscript, delete that vbscript, check the OS architecture again, run cacls, tell them they're running the wrong OS and exit.

    If they have the correct OS but dont have a working internet connection then its even worse because you do the OS architecture check, run cacls, write a vbscript to their PC, run that vbscript, delete that vbscript, check the OS architecture again, run cacls, write/overwrite a registry key, check for the internet connection, tell them the connection isnt working, wait fifteen seconds, check for the internet connection, tell them the connection isnt working, wait fifteen seconds, check for the internet connection, tell them the connection isnt working, wait fifteen seconds etc. for ever…

    You also appear to be writing to a registry key used in a 32-bit OS and not to that in a 64-bit one. Regardless of that you're just deleting it afterwards, despite saying you were 'restoring the previous setting'.
    You're also stopping a service, which you have no idea is running, then starting it also with no idea if it's stopped.