Solved

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

Posted on 2013-01-29
4
654 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
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 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 28

Expert Comment

by:sammySeltzer
ID: 38836467
Super!

Thanks alot
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now