hello scripting guys,
could I please get some help with amending the script, I would like to add logging to the script , I did try to but I didn't get it quite right, so wondered if I could get some help with this, it doesn't seem to write any logs to the local machine.
I have only been scripting for a couple of days and would really like some feedback on where I am going wrong, thanks
'******************************************************************************
'scenario1.vbs
'Author: Peter Costantini, the Microsoft Scripting Guys
'Date: 9/2/04
'This script runs on the admin workstation. It gets a list of remote machines
'from a text file, copies the scripts to each machine, and
'then launches the first script on each machine.
'All scripts must be in the same folder.
'******************************************************************************
On Error Resume Next
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Creat log file
Set pinglog = obfso.opentextfile("c:\temp\pinglog.log", 2, "True")
set objlogfile = objFsoObject.OpenTextFile("c:\temp\testlog.log", 2, "True")
Set copyfilelog = obfsoObject.opentextfile("c:\temp\copyfile.log", 2, "True")
Set copyfilefailedlog = obfsoObject.opentextfile("c:\temp\copyfilefailed.log", 2, "True")
Set pinglogfailed = obfsoObject.opentextfile("c:\temp\pinglogfailed.log", 2, "True")
Set wuafixlog = obfsoObject.opentextfile("c:\temp\wuafix.log", 2, "True")
'Read computer names for install from text file.
If objFSO.FileExists(g_strHostFile) Then
Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
WScript.Echo "Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStream
g_strComputer = objTextStream.ReadLine
Wscript VbCrLf & g_strComputer
Wscript String(Len(g_strComputer), "-")
'Ping host to ensure that it is accessible.
blnPing = PingHost
If blnPing = True Then
pinglog.writeline strcomputer & "ping ok"
'Call functions and sub-routines and handle logic.
blnCopyFiles = CopyFiles
If blnCopyFiles = True Then
copyfilelog.writeline strcomputer & "copy file ok"
RunRemote
Else
copyfilefailedlog.writeline strcomputer & "copy file failed"
End If
Else
pinglogfailed.writeline strcomputer & "ping failed"
End If
Loop
objTextStream.Close
'******************************************************************************
Function PingHost
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & g_strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
PingHost = True
Else
PingHost = False
End If
End Function
'******************************************************************************
Function CopyFiles
' copied wuafix to remote machine
strRemoteFolder = "\\" & g_strComputer & "\c$\wuafix"
arrFiles = Array("wuafix.vbs")
'Check for folder on host and
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strRemoteFolder) Then
objFSO.CreateFolder(strRemoteFolder)
If Err = 0 Then
objcopyfile.writeline strcomputer
'WScript.Echo "Created folder " & strRemoteFolder & "."
Else
objcopyfilefailedlog.writeline strcomputer & "failed to copy folder"
Err.Clear
CopyFile = False
Exit Function
End If
End If
'Copy files.
intCount = 0
For Each strFile in arrFiles
If objFSO.FileExists(strFile) Then
objFSO.CopyFile strFile, strRemoteFolder & "\"
If Err = 0 Then
copyfilelog.writeline strcomputer & "success"
'WScript.Echo "Copied file " & strFile & " to folder " & _
'strRemoteFolder & "."
intCount = intCount + 1
Else
copyfilefailedlog.writeline strcomputer & "failed"
'WScript.Echo "Unable to copy file " & strFile & "."
End If
Err.Clear
Else
'WScript.Echo "File " & strFile & " not found."
copyfilefailedlog.writeline strcomputer & "file not found"
End If
Next
If (intCount = UBound(arrFiles) + 1) Then
CopyFiles = True
Else
CopyFiles = False
End If
End Function
'******************************************************************************
Sub RunRemote
'runs wua fix
strScript = "cscript c:\wuafix\wuafix.vbs"
'Connect to WMI service on remote machine.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _& g_strComputer & "\root\cimv2")
If Err <> 0 Then
objlogfile.writeline strcomputer & "connecting to wmi failed"
'WScript.Echo "Error connecting to WMI on \\" & g_strComputer & "."
'WScript.Echo "Error number: " & Err.Number
'WScript.Echo "Error source: " & Err.Source
'WScript.Echo "Error description: " & Err.Description
Error.Clear
Exit Sub
End If
'Get a Win32_Process object.
Set objProcess = objWMIService.Get("Win32_Process")
'Create a new process and run strScript in it.
intReturn = objProcess.Create(strScript, , , intProcessID)
If intReturn = 0 Then
wuafixlog.writeline strcomputer & "wua fix applied"
' WScript.Echo "Launched " & strScript & " WUA FIX " & _
' "Installer." & VbCrLf & _
'"Process ID: " & intProcessID & "."
Else
wuafixlog.writeline strcomputer & intReturn
' WScript.Echo "Error: " & strScript & " could not be started." & VbCrLf & _
' "Return code: " & intReturn & "."
End If
End Sub
many thanks