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
499 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
9 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 250 total points
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
You need to add the line:
    SvcChange ("Start")
somewhere.  
 
0
 
LVL 2

Assisted Solution

by:KConner32
KConner32 earned 125 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now