Solved

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

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

Expert Comment

by:sammySeltzer
ID: 38836467
Super!

Thanks alot
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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