Solved

Script to start or stop services

Posted on 2007-11-29
28
4,330 Views
Last Modified: 2010-08-05
Experts!  Here is the situation.  I have a server running Websense and I would like to backup the database.  In order to do that, I need to stop the MSSQL$ and SQLAgent$ service before running the backup.  I posted a similiar question before and was given this script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colService = objWMIService.ExecQuery("Select * from Win32_Service where Name='mssql$websense'")
For Each objService in colService
    if objService.State = "Stopped" Then
      errReturn = objService.StartService()
      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
      For each objService2 in colService2
            'msgbox objService2.DisplayName
                errReturn = objService2.StartService()
      Next
    Elseif objService.State = "Running" Then
      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
      For each objService3 in colService3
            'msgbox objService3.DisplayName
                errReturn=objService3.StopService()
      Next
      errReturn = objService.StopService()
    End If
Next

What I was hoping to have the script do was check to see if those two services were running and if they were, to shut them off.  Then I would schedule the script to run again and turn them back on.  What seems to happen is only one service shuts down and the other stays on when the script runs the first time.  When it runs the second time, it turns the first service back on and shuts down the other service.

So what I need help on is how to modify the above script to give the desired results (stated above) or use a new script that will solve this issue.  Either way, the points are available for whoever provides a fix.  Thanks for your help!!
0
Comment
Question by:samiam41
  • 17
  • 8
  • 3
28 Comments
 
LVL 5

Expert Comment

by:usarian
Comment Utility
SQL Server doe not need to be shut down to run a backup.
you can use NTBACKUP, any commercial backup software (which mostly use NTBACKUP behind the scenes), or use the SQL Server command BACKUP.

0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
Wow did I just let a brain fart rip thru this place.. sorry..
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
: )

Not a problem.  I tried using HP Data Protector's (we used this backup software because of the mix of UNIX and Windows servers) SQL Agent to backup the database but the MSDE database still became "suspect".  So now I am just going to the services down while I back it up.
0
 
LVL 8

Expert Comment

by:deadite
Comment Utility
Give this a shot, just change strService1 and strService 2 to match the names of the services.   I'm sure you can take the time to make the code a bit more efficient by making it a sub to call, but it was quicker this way...
strComputer = "."

strService1 = "mssql$websense"

strService2 = "SQLAgent$websense"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colService = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colService

	'CASE 1

    If objService.Name = strService1

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

	    Elseif objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If

	'CASE 2

    Elseif objService.Name = strService2

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

	    Elseif objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If			

	End If	

Next

Open in new window

0
 
LVL 8

Expert Comment

by:deadite
Comment Utility
I should mention, the only draw back with that script is, it will either start or stop the service depending on the current state.  If you know they will always be started at the same time, it will stop them both.  However, if one is stopped and one is started, it will start the first one and stop the second.
0
 
LVL 8

Expert Comment

by:deadite
Comment Utility
You can even do this with a BAT file, minus the dependencies using

@Echo Off
Rem Stop Serives
Net Stop MSSQL$websense
Net Stop SQLAgent$websense


and have a second BAT File to start them
@Echo Off
Net Start MSSQL$websense
Net Stop SQLAgent$websense


If you prefer VBS, here are 2 files you can have to stop and start it:
' Stop Running Services

strComputer = "."

strService1 = "mssql$websense"

strService2 = "SQLAgent$websense"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colService = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colService

	'CASE 1

    If objService.Name = strService1

	    If objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If

	'CASE 2

    Elseif objService.Name = strService2

	    If objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If			

	End If	

Next
 
 
 
 
 
 
 

' Start Running Services

strComputer = "."

strService1 = "mssql$websense"

strService2 = "SQLAgent$websense"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colService = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colService

	'CASE 1

    If objService.Name = strService1

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

	    End If

	'CASE 2

    Elseif objService.Name = strService2

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

		End If

	End If	

Next

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
deadite-  thanks for the script.  I have to admit that scripting is something I am still learning.  I am more network and server geared and I am trying to learn more scripting.  Given that, I am not sure what you mean by "sub to call".  Will the first script you posted also start/stop the dependencies?
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
The first script you posted I get an error when I run it.

Line 8
Character 37
Error:  Expected 'Then'
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Any help would be appreciated.
0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
take deadite's code and put the word "THEN" at the end of line 8, which is:
If objService.Name = strService1

change to:
If objService.Name = strService1 Then



strComputer = "."

strService1 = "mssql$websense"

strService2 = "SQLAgent$websense"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colService = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colService

	'CASE 1

    If objService.Name = strService1 Then

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

	    Elseif objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If

	'CASE 2

    Elseif objService.Name = strService2

	    If objService.State = "Stopped" Then

	      errReturn = objService.StartService()

	      Set colService2 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService2 in colService2

	            'msgbox objService2.DisplayName

	                errReturn = objService2.StartService()

	      Next

	    Elseif objService.State = "Running" Then

	      Set colService3 = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='" & objService.Name & "mssql$websense'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

	      For each objService3 in colService3

	            'msgbox objService3.DisplayName

	                errReturn=objService3.StopService()

	      Next

	      errReturn = objService.StopService()

	    End If			

	End If	

Next

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Thanks for the post.  I made the change and received a similiar error for line 25/char 41.  I added it and am testing it now.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
No dice.  The services stay "started".

Any other ideas?  deadite posted this and I wasn't sure what he meant:  

"just change strService1 and strService 2 to match the names of the services.   I'm sure you can take the time to make the code a bit more efficient by making it a sub to call, but it was quicker this way"

0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
I can start and stop them manually.  I just did it to make sure that it wasn't something else.
0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
Try deadite's cmd:
Net Stop SQLAgent$websense

That is, Start-->Run-->CMD
type:
Net Stop SQLAgent$websense

hit enter, what response do you get?
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 9

Author Comment

by:samiam41
Comment Utility
The SQLAgent$WEBSENSE service is stopping.
The SQLAgent$WEBSENSE service was stopped successfully.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Are services CasE sensitive?
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Nevermind.  Dumb question.  I should have known better.
0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
Refresh my memory here.. why are we not using the SQL Server 'BACKUP DATABASE' TSQL command...?
0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
After running the CMD command, does the agent service say "Started" or "Stopped"
(bear with me here)
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
I'm not sure we went over that command.  I am trying to stop the MS/SQL services so I can backup the databases using HP Data Protector.  If the services are running and I try to back them up, they become "suspect" and I have to recreate/restore them.  

If you know of a way that I can copy the databases to a directory on another server and we can do away with this script, I am all ears.  As long as the solution allows me to backup the databases and not corrupt them in the process, I can be flexible.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
After running the net stop sqlagent$websense command, the status for sql is blank under "computer management > services".  There is no status.  If I use pstools and check the status of that service, it shows me stopped.
0
 
LVL 5

Expert Comment

by:usarian
Comment Utility
Do you have access to Query Editor or Enterprise Manager or TOAD for SQL Server or Microsoft SQL Server Management Studio or Visual Studio, and anythign else that can execute SQL commands?
If all else fails there's OSQL

Quick outline of where I'm going:
SQL Server has a BACKUP DATABASE command that can be executed the same manner in which you execute queries.
Although there is a way to backup directly to another computer on a network, it is better practice to save the file locally (gotta have enough disk space for the backup file of course), then copy/move it to where you want it to go.

0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
All good questions.  I have MSDE installed on this server as it came with Websense, so I am not sure what access we have to Query Editor, etc.  I seriously never thought having a script that would stop a service and it's dependencies and then start them up again could be this hard.  I just need the services to stop and start using task manager.  While the services are offline, I copy the database somewhere else.  I admit that I am a network and server engineer more then a developer or programmer.  Am I misunderstanding how complex this is?
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Ok.  I think I figured it out.  Using PSSERVICE.exe, I can stop and start the services.  I stop sql first and then mssql second.  Vice-versa on start them.  Wow....  What a jungle of a mess.  

I appreciate everyone's help and suggestions.  I am thinking how to divide up the points.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Do you know how to use psservice in a script?  I will award full points if you can help with that.  Otherwise I don't see a solution.  Thanks usarian.
0
 
LVL 5

Accepted Solution

by:
usarian earned 500 total points
Comment Utility
I'm happy you found a way to start and stop services.
Take a look at using OSQL to run BACKUP DATABASE though,  It enables you to perform backups without shutting down the database.

https://conference.websense.com/docs/releasenotes/v6.3/ReportingReleaseNotes.htm#0_pgfId-2589913

under the header:
Microsoft MSDE backup



another link:

http://www.microforge.net/kb/135

Your instance name is:
websense

Happy Holidays!
Usarian
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Complete!

To stop it:
c:\tools\psservice \\w2k3websec01 stop mssql$websense
ping -n 5 127.0.0.1 >NUL
c:\tools\psservice \\w2k3websec01 stop sqlagent$websense

To start it:
c:\tools\psservice \\w2k3websec01 start mssql$websense
ping -n 5 127.0.0.1 >NUL
c:\tools\psservice \\w2k3websec01 start sqlagent$websense

Thanks everyone!!  Have a great Christmas or other Holiday!
0
 
LVL 9

Author Closing Comment

by:samiam41
Comment Utility
usarian, thanks for the ideas and for sticking with the question.  I am awarding you the points because I am going to look into the OSQL Backup Database option.  Take care!!

Best regards,
Aaorn
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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

7 Experts available now in Live!

Get 1:1 Help Now