Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Can someone help me write a vbs script to create dir, copy file, and stop and start services PLEASE

HI,

Thank you in advance for any and all help you can give with this!

Here is what I am trying to do:

1) I need a vbs script that will check for a few services when I run it and if they are present stop them no matter what state they are in.  I also need to create a directory under "c:\documents and settings\all users\application data\new folder" not sure how to do this with all the spaces and the new folder name will also require spaces...  Then copy a txt file to that same folder.
Then I need to call a program to run and when that program exits to do the following:
Start all services it stopped before at the beggining.

Not sure if it is possible, but it would be great to have the script check what services are present and their state (Stopped, started etc) and if they are stopped just leave them stopped and stop the ones that are not and then when the application is closed it will start the ones that were started before. (Putting the services back to the same state they were before the application with the script was run.)

The services that I need to check for are:

SQL 2000 MSDE service with instance name MSSQLSERVER
SQL 2005 Express with instance name MSSQLSERVER
SQL 2005 with instance name MSSQLSERVER
IIS including World Wide Web service
Home grown application service (auto.exe)

I believe that the service names for all these and or commands will be: (but please correct me if I am wrong or if there is a better command etc)

Stop services:
SQL2000 MSDE = net stop mssqlserver
SQL2005 Express = net stop mssql$sqlexpress
SQL2005 = not sure of this one
IIS = net stop iisadmin or iisreset /stop
Custom Service = net stop auto

Start Services:
SQL2000 MSDE = net start mssqlserver
SQL2005 Express = net start mssql$sqlexpress
SQL2005 = not sure of this one
IIS = net start iisadmin and net start W3SVC or iisreset
Custom Service = net start auto

Let me know if you need any other info and thanks again for all your help!!!!
0
slavetonone
Asked:
slavetonone
4 Solutions
 
sirbountyCommented:
Open up Services (Start->Run->Services.msc) and double-click those services to get the actual service filename....update the script with those accurate names.

This should accomplish what you're after...
You'll need to update/expand the services in the first few lines (Service1 thru Service 4)
and also change the Program information that you want to run
Service1 = "mssqlserver.exe"
Service2 = "mssql$sqlexpress.exe"
Service3 = "iisadmin.exe"
Service4 = "auto.exe"
AppName = "C:\Program Files\MyApp\App.exe"
 
arrServices = Array(Service1, Service2, Service3, Service4)
 
SvcChange ("Stop")
 
'Create Folder
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\documents and settings\all users\application data\new folder"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
Set objFSO = Nothing
 
'Run Program
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
objShell.Run AppName
Set objShell = Nothing
 
Sub SvcChange (strAction)
  If strAction = "Stop" Then strState = "Stopped"
  If strAction = "Start" Then strState = "Running"
 
For Each Service in arrServices
  Set objWMI = GetObject  ("winmgmts:\\.\root\cimv2")
  Set colServices = objWMI.ExecQuery ("Select * From Win32_Service Where  State <> '" & strState & "' And Name = '" & Service & "'")
  For Each objService in colServices
    If strAction = "Stop" Then
      objService.StopService()
    Else
      objService.StartService()
    End If
  Next
Next
End Sub

Open in new window

0
 
slavetononeAuthor Commented:
When I run what you provided above after adding the program information and edititng for the correct service names etc, it stop the services, but it does not start them back up.  Also the path of the new directory actualy is a few more directories deep, I have edited it and it works, but is the best way to do this?  Please see my code below and let me know if you see anything wrong with the directory creation process and or the reason the services are not starting.  (In this example I am only dealing with the IISADMIN service for troubleshooting.)

Thanks!
Service1 = "mssqlserver"
Service2 = "mssql$sqlexpress"
Service3 = "iisadmin"
Service4 = "W3SVC"
Service5 = "auto"
Auto = "C:\auto.bat"
 
arrServices = Array(Service1, Service2, Service3, Service4, Service5)
 
SvcChange ("Stop")
 
'Create Folder Path
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\documents and settings\all users\application data\Johnson Controls"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
strFolder = "c:\documents and settings\all users\application data\Johnson Controls\MetasysIII"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
strFolder = "c:\documents and settings\all users\application data\Johnson Controls\MetasysIII\DatabaseFiles"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
Set objFSO = Nothing
 
'Run Program
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
objShell.Run vSCT
Set objShell = Nothing
 
Sub SvcChange (strAction)
  If strAction = "Stop" Then strState = "Stopped"
  If strAction = "Start" Then strState = "Running"
 
For Each Service in arrServices
  Set objWMI = GetObject  ("winmgmts:\\.\root\cimv2")
  Set colServices = objWMI.ExecQuery ("Select * From Win32_Service Where  State <> '" & strState & "' And Name = '" & Service & "'")
  For Each objService in colServices
    If strAction = "Stop" Then
      objService.StopService()
    Else
      objService.StartService()
    End If
  Next
Next
End Sub

Open in new window

0
 
slavetononeAuthor Commented:
I have also found that the IISADMIN service is not stopping all the time and it is because the W3SVC service is dependent on the IISADMIN service, so if I change the order and have the W3SVC service first in the list it stops both.  Does this mean (once it is working) the last part of the script that starts the services will need to be changed to make sure to bring them up in the correct order?  (I dont think it matters when they come up actually)  I will just try it and see what happens after I get your reply as to why the services are not starting back up.  Thanks again for all your help!!!!

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
DanRollinsCommented:
Did you try changing the order, as you indicated that you would?  The startup problem is almost certainly that one Service depends upon another.
In the Sevices countrol panel, you cahn ciew the properties of the Service and then click the Dependencies tav to find which services depend on which.
Note, also, that some Services take a few seconds to start.  If you think you have the order right but it still fails, then try adding a delay before starting the Services that depend on other Services.
0
 
slavetononeAuthor Commented:
I have checked the order and seem to have that working for the most part, but putting a delay may help  some too.  How would be the best way to put a delay in the above script?  Also, I still have not been able to get the last part of the script to work at all.  (start the services back up) Any ideas on why that part is not working?

Thanks!
0
 
DanRollinsCommented:
You need to add the line:
    SvcChange ("Start")
somewhere.  
 
0
 
KConner32Commented:
Try this.  This will remember the state of the running services, and only turn those back on.
arrServices = Array("mssqlserver", "mssql$sqlexpress", "iisadmin", "W3SVC", "auto")
arrServicesStat = Array(0, 0, 0, 0, 0)
SvcChange ("Stop")
 
Create Folder Path
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\documents and settings\all users\application data\Johnson Controls"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
strFolder = "c:\documents and settings\all users\application data\Johnson Controls\MetasysIII"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
strFolder = "c:\documents and settings\all users\application data\Johnson Controls\MetasysIII\DatabaseFiles"
If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
Set objFSO = Nothing
 
Run Program
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
objShell.Run vSCT
Set objShell = Nothing
SvcChange ("Start")
Sub SvcChange (strAction)
  For l=0 to 4
	Set objWMI = GetObject  ("winmgmts:\\.\root\cimv2")
  	Set colServices = objWMI.ExecQuery ("Select * From Win32_Service Where  Name = '" & arrServices(l) & "'")
  	For Each objService in colServices
		
    		If objservice.state = "Running" and strAction="Stop" Then
			arrServicesStat(l)=1
    			objService.StopService()
			'msgbox "stopping " & objService.caption
		End If
    		If strAction="Start" and arrServicesStat(l)=1 Then
    			objService.StartService()
			'msgbox "starting " & objService.caption
		End If
	Next
Next
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now