locked
Wscript.Sleep - not working in VBScript RRS feed

  • Question

  • below procedure is working but my script is not sleeping for specified time - in Wscript.sleep.

    any help is appreciated.


    Sub ServiceStart(strServer,strService,intWait)
     
      If Not StartService(strServer,strService) Then
        
          Exit sub
       End If
      
       For i = 0 To 5
         
    '      WScript.Sleep(intWait)
            a = Int(intWait)/1000
         
          Do
             If Not CheckServiceStatus(serviceStatus, strService,strServer ) Then
                Exit Do
             End If
            
             If StrComp(serviceStatus, "Starting", vbTextCompare) = 0 Then
                    WScript.Sleep(intWait)
                     CheckServiceStatus serviceStatus, strService,strServer
                   
             End If
            
             If StrComp(serviceStatus, "Started", vbTextCompare) = 0 Then
               
                serviceStarted = True
                Exit For
             Else
                Exit Do
             End If
          Loop Until True
       Next

       If serviceStarted Then
          objLog.WriteLine(Now &  " " & "Service" & " " & strService & " " & " Successfully started On " & strServer)
       Else
          objLog.WriteLine(Now & " " & "Service" & " " & strService & " " & " is not started On " & strServer & " within " & a & " Seconds ")
       End If

    End Sub

    Function StartService(strServer,strService)
       Dim wmi, query, services, service, result
       StartService = False
       query       = "Select * from Win32_Service where Name='" & strService & "'"
       On Error Resume Next
          Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
          If Err.Number <> 0 Then
            objLog.WriteLine(Now & " " & " Failed to connect " & strService & " On " & strServer)
             Exit Function
          End If

          Set services = wmi.ExecQuery(query)
          If Err.Number <> 0 Then
       
             objLog.WriteLine(Now & " " & " Failed to Executing WMI Query on On " & strServer)
             Exit Function
          End If
          For Each service In services
             result = service.State
           
             If  result = "Running" Then
             objLog.WriteLine(Now & " " &  strService & " already running " & " On " & strServer)
                Exit function
             End If
            
             If result <> "Running" Then
             result1 = service.StartService()
             End if
            
             If result1 <> 0 Then
              objLog.WriteLine(Now & " Service " & strService & " is not starting On " & strServer)
                Exit Function
             End If
          Next
       On Error Goto 0
       StartService = True
    End Function

    Function CheckServiceStatus(serviceStatus,strService,strServer)
       Dim wmi, query, services, service
       CheckServiceStatus = False
       serviceStatus      = ""
       query              = "Select DisplayName, State From Win32_Service Where Name = '"& strService &"'"
       On Error Resume Next
          Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
          If Err.Number <> 0 Then
          
             objLog.WriteLine(Now & " Failed to Creating WMI object to connect to " & strServer)
             Exit Function
          End If
          Set services = wmi.ExecQuery(query)
          If Err.Number <> 0 Then
           
             objLog.WriteLine(Now & " Failed to Executing WMI Query on " & strServer)
            
             Exit Function
          End If
          For Each service in services
             serviceStatus = service.state
             If Err.Number <> 0 Then
            
                Exit Function
             End If
          Next
       On Error Goto 0
       CheckServiceStatus = True
    End Function

    Tuesday, September 8, 2009 10:24 PM

Answers

  • Running a vbs directly from an editor probably invokes the wrong host. I would guess it is using the embedded MS Script interpreter.  That does not instantiate the WScript object required for Sleep.  Does the script have a problem if run external to the editor?  It certainly shouldn't.

    Tom Lavedas
    • Marked as answer by IamMred Friday, January 8, 2010 2:17 AM
    Wednesday, September 9, 2009 2:03 PM

All replies

  • The Wscript.Sleep line is commented but I assume that is due to troubleshooting.

    Anyway, do you run this from a HTA, from in ASP or WSH?
    Wscript.Sleep is only valid in WSH (.vbs/.wsh files etc).
    Wednesday, September 9, 2009 5:15 AM
  • i am runnning this .vbs file in VBS Edit editor..

    but still same issue.

    thanks.
    Wednesday, September 9, 2009 1:51 PM
  • Running a vbs directly from an editor probably invokes the wrong host. I would guess it is using the embedded MS Script interpreter.  That does not instantiate the WScript object required for Sleep.  Does the script have a problem if run external to the editor?  It certainly shouldn't.

    Tom Lavedas
    • Marked as answer by IamMred Friday, January 8, 2010 2:17 AM
    Wednesday, September 9, 2009 2:03 PM