Solved

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

Posted on 2013-01-29
4
656 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS 2010 Convert to SSIS 2013 On Different Box 3 58
Daily basis, refresh information from "B" to "I" column. 31 80
Compile Error 7 51
Clear Filter 8 32
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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