scriptz
asked on
vbs script to read computers and services from a list and disable them
Need vbs script that will read a list of servers from a txt file then read a list of services that need to be disabled on those servers and set those services to disabled. This script is not working.
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
Dim objFSO : Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strcomputer = "C:\Users\servers.txt"
ServicesFile = "C:\Users\ServiceList.txt"
'arrServices = Array("SCardSvr")
'To read services from a text file, comment the above and uncomment the next line
arrServices = Split(objFSO.OpenTextFile( ServicesFi le).ReadAl l, vbNewLine)
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strcom puter, intForReading, False)
For Each strService In arrServices
objServiceDisplayName = "SCardSvr"
Set objWMIService = GetObject("winmgmts:\\" & strcomputer & "\root\CimV2")
Set colListOfServices = objWMIService.ExecQuery ("Select * From Win32_Service Where DisplayName ='" & objServiceDisplayName & "'")
On Error Resume next
For Each objService in colListOfServices
objService.ChangeStartMode ("disabled ")
Wscript.Sleep 5000
errReturnCode = objService.StartService()
Next
Next
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
Dim objFSO : Set objFSO = CreateObject("Scripting.Fi
strcomputer = "C:\Users\servers.txt"
ServicesFile = "C:\Users\ServiceList.txt"
'arrServices = Array("SCardSvr")
'To read services from a text file, comment the above and uncomment the next line
arrServices = Split(objFSO.OpenTextFile(
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strcom
For Each strService In arrServices
objServiceDisplayName = "SCardSvr"
Set objWMIService = GetObject("winmgmts:\\" & strcomputer & "\root\CimV2")
Set colListOfServices = objWMIService.ExecQuery ("Select * From Win32_Service Where DisplayName ='" & objServiceDisplayName & "'")
On Error Resume next
For Each objService in colListOfServices
objService.ChangeStartMode
Wscript.Sleep 5000
errReturnCode = objService.StartService()
Next
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
this script will allow you enter list of all your servers or computers and then stop and disable a service. In this example the service was sCardSvr.
Super!
Thanks alot
Thanks alot
ASKER
'define constants for text file input
Const ForReading = 1
'Define Paths to server list file
strListFile = "c:\servers.txt"
'Create file system object
Set objFSO = CreateObject("Scripting.Fi
'Open computer list file and load into an array
Set objListFile = objFSO.OpenTextFile(strLis
strData = objListFile.ReadAll
arrComputer = Split(strData, vbCrlf)
objListFile.CLose
set objListFile = Nothing
'Process each computer name and change the service from manual to disabled
For Each strComputer in arrComputer
If strComputer <> "" Then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'SCardSvr'")
For Each objService in colServiceList
If objService.State = "Running" Then
objService.StopService()
Wscript.Sleep 5000
End If
errReturnCode = objService.ChangeStartMode
Next
End if
Next