Just a quick question, probably a simple on at that. I am fairly new to VBScript, but I have managed to create a script which will automatically run through a list of domain computers and reboot them if they are online. I just want to find an easy way to add a log of the script results so I can run it automatically via scheduled task and see the results. For example: COMPUTER name was rebooted. Here is the script I have created which runs as a scheduled task...Thanks in advance!!
On Error Resume Next
Dim strFilename
strFilename = "c:\pstools\computertest.txt"
Dim objFSO, objTS, strComputer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(strFilename)
Do Until objTS.AtEndOfStream
strComputer = objTS.ReadLine
strComputer = UCase(strComputer)
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'")
For Each objItem in colItems
if objItem.StatusCode = 0 Then
PC= "ON"
Else
PC = "OFF"
End if
Next
If PC = "ON" then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "oot\cimv2")
'Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
'Ensure that no user is logged on
For Each objItem in colItems
strdomuser = ""&objItem.UserName
dpos = InStr(strdomuser,"\")
strlen = Len(strdomuser)
strdomuser = Right(strdomuser,strlen-dpos)
strdomuser = UCase(strdomuser)
if strdomuser ="" Then
Set wshshl = WScript.CreateObject("wscript.Shell")
strrunme = "cmd /c c:\pstools\psshutdown.exe -m ""Your computer is being rebooted for nightly maintenance"" -f -r -t 15 \\" &strComputer
wshshl.run strrunme,0
end if
Next
End If
Loop
objTS.Close
On Error Resume Next
Dim strFilename
strFilename = "c:\pstools\computertest.txt"
Dim objFSO, objTS, strComputer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(strFilename)
Do Until objTS.AtEndOfStream
strComputer = objTS.ReadLine
strComputer = UCase(strComputer)
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'")
For Each objItem in colItems
if objItem.StatusCode = 0 Then
PC= "ON"
Else
PC = "OFF"
End if
Next
If PC = "ON" then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "oot\cimv2")
'Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
'Ensure that no user is logged on
For Each objItem in colItems
strdomuser = ""&objItem.UserName
dpos = InStr(strdomuser,"\")
strlen = Len(strdomuser)
strdomuser = Right(strdomuser,strlen-dpos)
strdomuser = UCase(strdomuser)
if strdomuser ="" Then
Set wshshl = WScript.CreateObject("wscript.Shell")
strrunme = "cmd /c c:\pstools\psshutdown.exe -m ""Your computer is being rebooted for nightly maintenance"" -f -r -t 15 \\" &strComputer
wshshl.run strrunme,0
end if
Next
End If
Loop
objTS.Close