Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Add a message box to vbscript

Hello,

I wanted to add a message box to the end of the script that would appear if the service wasn't running and it was restarted.  

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "Service"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'",

"WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
      If objItem.State <> "Running" Then objItem.StartService()
Next


If this restarts the service, it would pop a message box saying the service was restarted.

Thank you.
0
cbones
Asked:
cbones
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Martin LissRetired ProgrammerCommented:
I didn't test this but try this at the end

If Not IsNothing(objWMIService) Then
    Msgbox "Restarted"
End If

Open in new window

0
 
cbonesAuthor Commented:
MartinLiss,

Thank you for responding.  I added that to the end but it comes up with an error when I run it.  Any ideas?

Thank you.
0
 
Martin LissRetired ProgrammerCommented:
Sorry,

If objWMIService is nothing Then
    Msgbox "Restarted"
End If

Open in new window


If that gives an error please tell me what the error says.
0
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.

 
RobSampsonCommented:
To know if your code restarted it, you can change this:
For Each objItem In colItems
      If objItem.State <> "Running" Then objItem.StartService()
Next

Open in new window


to this
blnRestarted = False
For Each objItem In colItems
      If objItem.State <> "Running" Then
             objItem.StartService()
             blnRestarted = True
      End If
Next
If blnRestarted = True Then MsgBox "Service was restarted."

Open in new window


Regards,

Rob.
0
 
Bill PrewCommented:
If you really want to know if it was restarted, versus just tried to be restarted, then go through the loop again making sure it is now running.  Probably after a short delay to give it a chance to start.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "Service"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
    If objItem.State <> "Running" Then objItem.StartService()
Next

WScript.Sleep 1000

For Each objItem In colItems
    If objItem.State = "Running" Then MsgBox "Service was successfully restarted.", vbOkOnly
Next

Open in new window

~bp
0
 
cbonesAuthor Commented:
Hello,

Those both pop a message up saying the services has restarted but it actually doesn't star the service.

Thank you.
0
 
RobSampsonCommented:
Since WMI doesn't poll the method request to see the result of the StartService command, the most reliable way is to use Net Start and read the output from it.

Try this.

Regards,

Rob.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objShell = CreateObject("WScript.Shell")

strComputer = "."
strServiceName = "Service"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

blnStartRequired = False
For Each objItem In colItems
	If objItem.State <> "Running" Then
		blnStartRequired = True
	End If
Next

If blnStartRequired = True Then
	strCommand = "cmd /c net start " & strServiceName
	Set objExec = objShell.Exec(strCommand)
	While objExec.Status = 0
		WScript.Sleep 100
	Wend
	strMessage = objExec.StdOut.ReadAll & vbCrLf & objExec.StdErr.ReadAll
	If InStr(LCase(strMessage), "service was started successfully") > 0 Then
		WScript.Echo strServiceName & " service was restarted successfully."
	Else
		WScript.Echo strServiceName & " was not started successfully."
	End If
End If

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now