Hi All,
Hoping you can help a noob out. Recent job change has given me the oppurtunity to learn scripting and programming. However I'm a bit stumped with a bit of code and was wondering if someone could point me in the right direction.
I have a server that is brought down for backups and restarted nightly. On ocassion a particular service doesn't always restart so I have borrowed and modified a script to resolve this. I have the output being sent to a logfile however I would prefer the logfile to append rather than overwrite and a time stamp to be placed between the entries.
Here's the code:
TIA
Hoping you can help a noob out. Recent job change has given me the oppurtunity to learn scripting and programming. However I'm a bit stumped with a bit of code and was wondering if someone could point me in the right direction.
I have a server that is brought down for backups and restarted nightly. On ocassion a particular service doesn't always restart so I have borrowed and modified a script to resolve this. I have the output being sent to a logfile however I would prefer the logfile to append rather than overwrite and a time stamp to be placed between the entries.
Here's the code:
Code:
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
Logfile = "C:\fso\fso.txt"
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Logfile, ForAppending)
systime=now()
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colRunningServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'spooler'")
For Each objItem In colRunningServices
If objItem.State="Stopped" Then
objFile.WriteLine "the service is STOPPED" & objItem.DisplayName & " -- " & objItem.State
Call SetManStart
Else
objFile.WriteLine " the service is RUNNING" & space(1) & objItem.DisplayName & " -- " & objItem.State
End If
Next
Sub SetManStart
'Set service to Manual
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServicesState = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='spooler'")
For Each objService in colServicesState
objService.ChangeStartMode("AUTO")
wscript.sleep 5000
'Start service
If objService.State="Stopped" then
objService.StartService()
wscript.sleep 2000
objFile.WriteLine "service was Started"
End If
Next
End Sub
TIA