Link to home
Start Free TrialLog in
Avatar of scriptz
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.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 
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(ServicesFile).ReadAll, vbNewLine)
 
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strcomputer, 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
ASKER CERTIFIED SOLUTION
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scriptz
scriptz

ASKER

I ended up building off of your idea and came up with this thanks for your assistance.


'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.FileSystemObject")

'Open computer list file and load into an array
Set objListFile = objFSO.OpenTextFile(strListFile, ForReading, False)
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("Disabled")  
Next

End if
Next
Avatar of scriptz

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