Wednesday, January 14, 2009

Script Code: Batch Running WUAUCLT "DetectNow"

Suppose you want to reach out and force a bunch of computers to run an Automatic Updates self-check right away. You could:
  • Put on some comfortable shoes, grab a drink and start running around
  • Delegate this to someone you don't like
  • Write a script to do it for you
BAT file using PSExec:

@echo off
c:\pstools\psexec.exe \\pc1,pc2,pc3 "%windir%\system32\wuauclt.exe" "/detectnow"

Save the .BAT file and run it using an account which has local admin rights on each of the computers. The problem with this approach is that the exit code is only returned for the last computer in the list. One way around this is to parse the list of computers and run psexec against each one separately. Then you can capture the exit code to see if it was successful or not.

I could use FOR %% in a BAT script and use the ERRORLEVEL variable to check on the result of each request. But I'm lazy and resort to what I know best: VBScript or KiXtart.

Const computers = "pc1,pc2,pc3"
Const pstools = "c:\sysinternals\psexec.exe"
Const rmtCmd = "%windir%\system32\wuauclt.exe"
Const rmtArg = "/detectnow"

Set objShell = CreateObject("Wscript.Shell")

count_good = 0
count_all = 0

For each computer in Split(computers, ",")
    count_all = count_all + 1
    cmd = pstools & " \\" & computer & " " & rmtCmd & " " & rmtArg
    wscript.echo "connecting to " & computer
    Set oExec = objShell.Exec(cmd)
    Do While oExec.Status = 0
        WScript.Sleep 100
    If oExec.ExitCode = 0 Then
        Wscript.Echo vbTab & "process was initiated"
        count_good = count_good + 1
        Wscript.Echo vbTab & "process failed"
    End If

Set objShell = Nothing
Wscript.Echo count_all & " attempted"
Wscript.Echo count_good & " completed"

You could do this in KiXtart, or Powershell or pretty much any scripting language.

Caveat: Because everything in life comes with a caveat (or three), you need to be aware that any remote scripting requires that you make sure things are in order before it'll work. Firewalls, port exceptions, user privileges, etc. Enjoy!
Post a Comment