?
Solved

vbs script to read computers and services from a list and disable them

Posted on 2013-01-29
4
Medium Priority
?
665 Views
Last Modified: 2013-01-30
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
0
Comment
Question by:scriptz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 total points
ID: 38835130
You didn't say what wasn't working.

Try changing these

For Each objService in colListOfServices

objService.ChangeStartMode("disabled")

Wscript.Sleep 5000

errReturnCode = objService.StartService()

Open in new window


To These


For Each objService In colListOfServices

StartMode = UCase(objService.StartMode)


If (StartMode = "Manual") Or (StartMode = "Disabled") Then
errReturnCode = objService.ChangeStartMode("Disabled")
End If

Open in new window

0
 

Author Comment

by:scriptz
ID: 38836218
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
0
 

Author Closing Comment

by:scriptz
ID: 38836229
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.
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 38836467
Super!

Thanks alot
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question