samiam41
asked on
Script to start or stop services
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("S elect * from Win32_Service where Name='mssql$websense'")
For Each objService in colService
if objService.State = "Stopped" Then
errReturn = objService.StartService()
Set colService2 = objWMIService.ExecQuery("A ssociators of " & "{Win32_Service.Name='mssq l$websense '} Where " & "AssocClass=Win32_Dependen tService " & "Role=Antecedent" )
For each objService2 in colService2
'msgbox objService2.DisplayName
errReturn = objService2.StartService()
Next
Elseif objService.State = "Running" Then
Set colService3 = objWMIService.ExecQuery("A ssociators of " & "{Win32_Service.Name='mssq l$websense '} Where " & "AssocClass=Win32_Dependen tService " & "Role=Antecedent" )
For each objService3 in colService3
'msgbox objService3.DisplayName
errReturn=objService3.Stop Service()
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!!
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colService = objWMIService.ExecQuery("S
For Each objService in colService
if objService.State = "Stopped" Then
errReturn = objService.StartService()
Set colService2 = objWMIService.ExecQuery("A
For each objService2 in colService2
'msgbox objService2.DisplayName
errReturn = objService2.StartService()
Next
Elseif objService.State = "Running" Then
Set colService3 = objWMIService.ExecQuery("A
For each objService3 in colService3
'msgbox objService3.DisplayName
errReturn=objService3.Stop
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!!
Wow did I just let a brain fart rip thru this place.. sorry..
ASKER
: )
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.
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.
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
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.
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:
@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
ASKER
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?
ASKER
The first script you posted I get an error when I run it.
Line 8
Character 37
Error: Expected 'Then'
Line 8
Character 37
Error: Expected 'Then'
ASKER
Any help would be appreciated.
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
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
ASKER
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.
ASKER
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"
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"
ASKER
I can start and stop them manually. I just did it to make sure that it wasn't something else.
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?
Net Stop SQLAgent$websense
That is, Start-->Run-->CMD
type:
Net Stop SQLAgent$websense
hit enter, what response do you get?
ASKER
The SQLAgent$WEBSENSE service is stopping.
The SQLAgent$WEBSENSE service was stopped successfully.
The SQLAgent$WEBSENSE service was stopped successfully.
ASKER
Are services CasE sensitive?
ASKER
Nevermind. Dumb question. I should have known better.
Refresh my memory here.. why are we not using the SQL Server 'BACKUP DATABASE' TSQL command...?
After running the CMD command, does the agent service say "Started" or "Stopped"
(bear with me here)
(bear with me here)
ASKER
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.
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.
ASKER
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.
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.
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.
ASKER
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?
ASKER
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.
I appreciate everyone's help and suggestions. I am thinking how to divide up the points.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
ASKER
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
Best regards,
Aaorn
you can use NTBACKUP, any commercial backup software (which mostly use NTBACKUP behind the scenes), or use the SQL Server command BACKUP.