Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-10-13
9
Medium Priority
?
512 Views
Last Modified: 2013-11-10
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
Comment
Question by:slavetonone
[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
9 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 22716298
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
 

Author Comment

by:slavetonone
ID: 22722729
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
 

Author Comment

by:slavetonone
ID: 22722875
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 1000 total points
ID: 22754761
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
 

Author Comment

by:slavetonone
ID: 22754872
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 1000 total points
ID: 22756053
You need to add the line:
    SvcChange ("Start")
somewhere.  
 
0
 
LVL 2

Assisted Solution

by:KConner32
KConner32 earned 500 total points
ID: 24868152
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

610 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