amending script to read list of computers, run script and output to log file RRS feed

  • Question

  • Hello all,

    I have cobbled together a script that runs and does what I want, now I would like to amend the script to read a list of computers rather than use the msg box that it is currently using for the strcomputer, if the computers doesn't respond to a ping, then log that, if it does continue with the script and when it is complete, log a success or failure. I have just started scripting and would really appreciate some help on this one,thanks. I created the script to fix an SCCM updates issue and failing task sequences, so it may prove useful to others.

    There are msg box entries that can be removed that were originally in there for the user running the script.

    'setting objects
    Dim net, objFSO, shell
    Dim objFile, strLine, intResult
    Set objnet = CreateObject("")
    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objshell = CreateObject("")
    strfile = "c:\wuafix\wuafix.vbs"
    strUser = "domain\user"
    strPassword = "password"
    'getting server name or IP address
    strComputer=InputBox("Enter the IP or computer name of the remote machine on which to repair the WUA agent:", "Starting WUA Fix")
    'check to see if the server can be reached
    Dim strPingResults
    Set pingExec = objshell.Exec("ping -n 3 -w 2000 " & strComputer) 'send 3 echo requests, waiting 2secs each
    strPingResults = LCase(pingExec.StdOut.ReadAll)
    If Not InStr(strPingResults, "reply from")>0 Then
      WScript.Echo strComputer & " did not respond to ping."
    End If
    'Check if source file exists
    If Not objFSO.FileExists(strFile) Then
       WScript.Echo "The source file does not exist"
    End If
    MsgBox "The WUA Fix is in process.  Please wait.", 64, "Script Message"
    'mapping drive to remote machine
    If objFSO.DriveExists("Z:") Then
    objnet.RemoveNetworkDrive "Z:","True","True"
    End If
    objnet.MapNetworkDrive "Z:", "\\" & strComputer & "\c$", True
    'creating folder for install exe on remote machine
    If (objFSO.FolderExists("Z:\wuafix\") = False) Then
       objFSO.CreateFolder "Z:\wuafix"
    End If
    'copying vbs to remote machine
    objFSO.CopyFile strFile, "Z:\wuafix\wuafix.vbs"
    'set command line executable to run a silent install remotely
    strInstaller1 = "cscript.exe c:\wuafix\wuafix.vbs"
    'strInstaller2 = "c:\wuafix\wuafix.vbs"
    strExec = "c:\pstools\PsExec.exe "
    'objshell.Run strExec & " \\" & strComputer & strInstaller1
    On Error Resume Next
    result = objshell.Run(strExec & " \\" & strComputer & " " & strInstaller1)
    If Err.Number = 0 Then 
    WScript.Echo "PSXEC Runing WUA fix remotely"
    Else MsgBox Err.Number
    MsgBox result
    End If
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colLoggedEvents = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application' AND " _
     & "EventCode = '4'")
    Wscript.Echo "Event Viewer checked and Fix Applied:" & colLoggedEvents.Count
    MsgBox "Removing mapped drive  Please wait.", 64, "Script Message"
    If objFSO.DriveExists("Z:") Then
    objnet.RemoveNetworkDrive "Z:","True","True"
    End If
    MsgBox "The WUA Fix has been applied.", 64, "Script Message"

    Any help appreciated and explanations on the process would be great as I would like to learn the process involved, which is difficult when working during the day.

    many thanks

    • Edited by MisterD101 Monday, July 7, 2014 12:31 PM
    Tuesday, July 1, 2014 8:16 AM


All replies

  • Hi,

    A cursory read of your script looks like the purpose of it is to run a script called wuafix.vbs on a remote computer.

    What is the purpose of that script and why do you need to run it on remote computers?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 1, 2014 2:20 PM
  • Hi Bill,

    long story short, I have approx. 2800 clients with an old entry in WMI for updates that the sccm client cannot clear or run because they do not exist anymore, so the client will not run updates or use a task sequence because of this, so my script fixes this and does a couple of other things, I have found another way to do this by running  a different script that uses WMI to call a cscript function that uses the wuafix.vbs that is coped to the machine, I am also changing the echo entries to output to a log file instead so that I can track what client has run the fix andn which ones haven't.

    If you have any suggestions then please let me know, nothing nefarious :)

    many thanks

    Tuesday, July 1, 2014 3:45 PM
  • It is a very, very bad idea to hard-code a password in a script like that.

    If you really want to use psexec, it can already read from a file and run a command on a list of computers.

    psexec "@c:\temp\computers.txt" -u myuser ...

    It still isn't secure because psexec sends the password over the network as clear-text, but at least you can run the command without hard-coding the password into the script.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 1, 2014 3:52 PM
  • hi Bill,

    I didn't in the script I have used, sorry, forgot to remove it from this post....

    many thanks

    Tuesday, July 1, 2014 4:11 PM
  • Right, so to my other point, psexec can already run a command on a list of computers.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 1, 2014 4:12 PM