Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script can stop service, but cannot start it 1

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I am trying to create a script that will stop a service, troubleshoot, and then start the service.

So far, I am able to stop the service, but I am unable to start the service. I actually have to go into the computer and start it manually.

Below is the code I have so far:

Code:
' Must be run from command prompt with System Domain rights, on Windows XP or 2000
' It asks for text file with list of computers to delete the hardware ID
' Then it asks for SEP install password

Option Explicit

Dim objInputFSO
Dim strInputFile, strData, arrLines, strLine
Dim objPassword, strPassword, objShell, objWMIService
Dim oReg
Dim strComputer, strKeyPath, strValueName, strValue, dwValue 
Dim strPath, strVersion, intComm, strGroup, strPolicy, strHardware, strVirus, strIPS, intNTP


const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002

'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")

'Name of the input text file
strInputFile = InputBox("What is the name of file with list of computers?")

'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Create Password Object
Set objPassword = CreateObject("ScriptPW.Password") 

'Prompt User for Password
WScript.StdOut.Write "Please enter your password:" 

'Input is stored in strPassword
strPassword = objPassword.GetPassword() 

'Create Shell Object
Set objShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2:Win32_Process")


'For loop to go through list of computers

On Error Resume Next

'Step through the lines
For Each strLine in arrLines

  strComputer = strLine

  Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")

  'Find the SEP Installation path
  strKeyPath = "SOFTWARE\Symantec\Symantec Endpoint Protection\AV"
  strValueName = "Home Directory"
  oReg.GetStringValue _
   HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
  strPath = strValue

  'Stop smc.exe service
  objWMIService.Create(strPath & "\smc.exe -stop " & strPassword)


  'troubleshooting tasks
  'troubleshooting tasks
  'troubleshooting tasks

  'Start smc.exe service
  objWMIService.Create(strPath & "\smc.exe -start ")


Next

'Cleanup
Set objInputFSO=Nothing
 
do you need to provide the password for starting it? just a thought.

Code:
  objWMIService.Create(strPath & "\smc.exe -start " [red]& strPassword[/red])

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
It doesn't need password to start.

However I tried it with password, and it still doesn't work.
 
Yeah, it was a long shot. Try and start the service in the cmd so you can see any errors that the vbs may not be showing you.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
No errors when I ran it from command prompt.

However, you gave me some ideas.

Turns out, we need sleep funtion

New code

Code:
' Must be run from command prompt with System Domain rights, on Windows XP or 2000
' It asks for text file with list of computers to delete the hardware ID
' Then it asks for SEP install password

Option Explicit

Dim objInputFSO
Dim strInputFile, strData, arrLines, strLine
Dim objPassword, strPassword, objShell, objWMIService
Dim oReg
Dim strComputer, strKeyPath, strValueName, strValue, dwValue 
Dim strPath, strCom



const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002

'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")

'Name of the input text file
strInputFile = InputBox("What is the name of file with list of computers?")

'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Create Password Object
Set objPassword = CreateObject("ScriptPW.Password") 

'Prompt User for Password
WScript.StdOut.Write "Please enter your password:" 

'Input is stored in strPassword
strPassword = objPassword.GetPassword() 

'Create Shell Object
Set objShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2:Win32_Process")


'For loop to go through list of computers

On Error Resume Next

'Step through the lines
For Each strLine in arrLines

  strComputer = strLine

  Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")

  'Find the SEP Installation path
  strKeyPath = "SOFTWARE\Symantec\Symantec Endpoint Protection\AV"
  strValueName = "Home Directory"
  oReg.GetStringValue _
   HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
  strPath = strValue

  'Stop smc.exe service
  objWMIService.Create(strPath & "\smc.exe -stop " & strPassword)

  Wscript.sleep(10000)

  'Start smc.exe service
  objWMIService.Create(strPath & "\smc.exe -start ")


Next

'Cleanup
Set objInputFSO=Nothing
 
Sweet, so I assume things are working? On what type of machine will this script be running? Perhaps not necessary for this particular implemenation, but what would happen if the it ran on a old machine. Would 10 seconds be enough time for the service to shutdown? What if 15 seconds is needed? What about (God forbid) 30 seconds? Consider replacing a static delay with a "dynamic" one.

Code:
'Wait for the service to stop.
do
   set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where Name='smc.exe'")
   wscript.sleep 100
loop until (colProcesses.count <> 0)

'Then wait a little more
wscript.sleep 1000

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
This script will be running on Win XP, but you're right, some of the computers may be old.

Will incorporate the "dynamic" delay.
 
oops

loop until (colProcesses.count <> 0)

should be

loop until (colProcesses.count [red]=[/red] 0)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top