CCNP-PRO

Based on the IT journey of Michael Rickert

Checking windows registry keys in batch scripts

Today I had to create a quick and dirty batch file script to fix some issues with WSUS on user machines. The machines were pulling down a bad update file from the WSUS server and needed to have their local ‘SoftwareDistribution’ folder cleared so they could download a proper version from Microsoft’s site. This error was affecting quite a few machines on the network and the easiest way to clear the folder on all the machines was to create a batch script that could be set as a login script for the AD. The script worked like this:

  1. Check if the machine is currently using the local WSUS server
  2. If it was, go to the repair section of the script. If it was tied to Microsoft updates, then end the script and leave the machine as-is.
  3. *now we are running the fix* Stop the Windows Update service
  4. change the registry key for UseWUServer to a value of “0”
  5. delete the folder “C:\Windows\SoftwareDistribution”
  6. restart the Windows Update service and end the script

Now that you know the issue, lets create a script that will automate the repair process and free up some time for the SysAdmins! First, here’s my solution batch script to this problem:

@echo off
:START
:: Check if windows update is using the WSUS server or Windows
reg query HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v UseWUServer | find “0x1”

if %errorlevel% EQU 1 goto END
:: fix Windows update problem
echo Please wait…
net stop wuauserv
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v UseWUServer /t REG_DWORD /d 0x0 /f
rmdir “C:\Windows\SoftwareDistribution” /S /Q
net start wuauserv
goto END

:END
echo Done…

Now that you’ve seen the solution, here’s a line-by-line breakdown of the batch script I created to solve this problem:

  1. *this section query’s the registry using reg.exe and searches for a value of “1” for the UseWUServer registry key*

    reg query HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v UseWUServer | find "0x1"

  2. *this part of the script says: if you don’t find the value of “1” for the key specified, end the script now*

    if %errorlevel% EQU 1 goto END

  3. *now lets get started with the fix, because someone that doesn’t need the fix has already stopped the script. Let’s use the ‘net’ command to stop the Windows Update service*

    net stop wuauserv

  4. *next lets change that registry key value to tell the Windows machine that it should use Windows Update server instead of the WSUS server by making the key value “0”. Oh, and don’t prompt us about anything either “/f”*

    reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v UseWUServer /t REG_DWORD /d 0x0 /f

  5. *remove the entire “SoftwareDistribution” directory and don’t ask us if we want to perform this action or not “/Q”*

    rmdir "C:\Windows\SoftwareDistribution" /S /Q

  6. *start the Windows Update service back up and let it re-create the “SoftwareDistribution” folder*

    net start wuauserv

  7. * go to the end of the script and stop running the script*

    goto END :END

In the end this fixed the WSUS bug issue quickly and efficiantly and allowed the system administrators to only perform this action once, rather than going around to each workstation and performing all of those repetitive steps

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: