无法使用vbscript停止服务

时间:2011-06-01 16:21:31

标签: vbscript

我正在使用以下代码尝试停止服务。我可以使用 WScript.Echo objService.State 显示服务状态,所以我知道我有正确的服务名称,并且可以找到它并将其状态确定为正在运行已停止但是当我运行此脚本时,我在第51行遇到错误:找不到错误代码80041002(见屏幕截图)

51处的代码行是:

 objService.StopService()

我哪里错了?我可以使用sc.exe通过命令行停止并启动它,并且能够控制其他服务,即Alerter,但是一旦我尝试控制这个特定的服务,它就会失败。

由于

  

编辑脚本中的完整代码(感谢Brandon Moretz指出   我没有发布完整的代码   行号并不意味着什么&   我已经更改了StartService()   停止,因为它原来是这样,现在你还有更多的事情要继续下去。遗憾!)

' 1. Check that the latest backup zip exists and store its name in LBZ (LatestBackupZip)
' 2. Stop the Livecontent Service
' 3. Remove .dbx, .lck and .log files from DataFolder
' 4. restart the service
' 5. Restore the database

Dim fileNewest
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell") 
'Set ImportFldr = fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data\Import")
Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export")
'Set DataFldr = fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data")
Set DataFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data")

For Each aFile In ImportFldr.Files
sExtension = fso.GetExtensionName(aFile.Name)
    If sExtension = "log" Then
            'Msgbox "The file extension is a " & sExtension
        Else
            'Msgbox "The file extension is a " & sExtension
                         If fileNewest = "" Then
                        Set fileNewest = aFile  
                     Else
                     'If fileNewest.DateCreated < aFile.DateCreated Then
                        If CDate(fileNewest.DateCreated) < CDate(aFile.DateCreated) Then

                            Set fileNewest = aFile
                     End If
                    End If
                End If
Next
Msgbox "The Newest File in the folder is " & fileNewest.Name & chr(13) & "Size: " & fileNewest.Size & " bytes" & chr(13) & "Was last modified on " & FileNewest.DateLastModified 

' Change to /Data
'WScript.Echo WshShell.CurrentDirectory 
WshShell.CurrentDirectory = DataFldr
'WScript.Echo WshShell.CurrentDirectory 

' Stop the Livecontent service
strComputer = "."
Dim objService
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name = 'LiveContent'")

For Each objService in colServiceList
WScript.Echo objService.State
    If objService.State = "Running" Then
        objService.StopService()
        'Wscript.Sleep 5000
    End If 
Next


'Dim objShell
'Set objShell = CreateObject("WScript.Shell")
'objShell.Run "%comspec% /k c: & cd ""C:\Program Files (x86)\XyEnterprise\SDL LiveContent\"" & """"loaddb RESTORE -'Dlc.file=C:\PROGRA~2\XYENTE~1\SDLLIV~1\data\Import\" & fileNewest.Name & " -Dlc.pswd=N2kAs72z"""""

script error when trying to stop service

最新编辑

我已经接受了你的代码,仍然无法让它工作。我注意到了这一行:

设置objWMIService = GetObject(“winmgmts:\”&amp; strComputer&amp;“\ root \ cimv2”)

在我输入的“winmgmts:\”中错过了一个\我想检查是否有(x86)目录,因为我在32位服务器上测试它,但是当它在64位服务器上进行测试时会将其移动到64它准备就绪,以便能很好地工作。

此部分也不起作用:

If fso.FolderExists( "C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data" ) Then
    Set DataFldr= fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data")
Else If fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data") Then
    Set DataFldr= fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data")
End If

但是如果我将ElseIf fso.GetFolder 更改为 fso.FolderExists

如果我注释掉第78行

,脚本运行正常

objService.StopService()

但是一旦我取消注释,我就会收到错误:

线:78
Char:9
错误:未找到 代码:80041002 来源:SWbemObjectEx

但可以找到服务行:WScript.Echo objService.State将其状态回显到屏幕。

现在真的很困惑。

' 1. Check that the latest backup zip exists and store its name in LBZ (LatestBackupZip)
' 2. Stop the Livecontent Service
' 3. Remove .dbx, .lck and .log files from DataFolder
' 4. restart the service
' 5. Restore the database

Dim fileNewest
Dim ImportFldr
Dim DataFldr
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")


If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data\Import" ) Then
    Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\Import")
  Else 
    WScript.Echo "Warning: Import Directory can not be found"   
End If

If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data\export" ) Then
    Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export")
Else 
    WScript.Echo "Warning: Export Directory can not be found"   
End If
If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data" ) Then
    Set DataFldr= fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data")
Else 
    WScript.Echo "Warning: Data Directory can not be found"     
End If    



For Each aFile In ImportFldr.Files
    sExtension = fso.GetExtensionName(aFile.Name)


If sExtension = "log" Then
        'Msgbox "The file extension is a " & sExtension
    Else
        'Msgbox "The file extension is a " & sExtension
                If fileNewest = "" Then
                    Set fileNewest = aFile  
                Else
                    If fileNewest.DateCreated < aFile.DateCreated Then
                        If CDate(fileNewest.DateCreated) < CDate(aFile.DateCreated) Then
                            Set fileNewest = aFile
                        End If
                    End If
                End If
    End If

Next
'Msgbox "The Newest File in the folder is " & fileNewest.Name & chr(13) & "Size: " & fileNewest.Size & " bytes" & chr(13) & "Was last modified on " & FileNewest.DateLastModified


' Change to /Data
WScript.Echo WshShell.CurrentDirectory
WshShell.CurrentDirectory = DataFldr
WScript.Echo WshShell.CurrentDirectory


' Stop the Livecontent service




strComputer = "."
Dim objService
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name = 'LiveContent'")

For Each objService in colServiceList
WScript.Echo objService.State
    If objService.State = "Running" Then
        objService.StopService()
        WScript.Echo objService.State
        Wscript.Sleep 5000
    End If
Next

编辑2

经过非常广泛的测试后,我们得出的结论是脚本没有任何问题,只是这个特定的服务不会停止使用这种方法。

为此,我们继续前进,现在正在使用

objShell.Run "sc start LiveContent"

这是一种享受。

感谢布兰登的帮助。

2 个答案:

答案 0 :(得分:1)

有几个小问题:

1。)不检查是否存在用于调用get文件夹的文件夹,这是导致“未找到”错误的原因。

2。)不匹配如果......那么&amp;文件循环中的结束语句。 (我可能会为vbscript选择一个更好的编辑器,程序员记事本和notepad ++非常有用。)

3.)我之前提到的StartService()/ StopService()混淆。


' 1. Check that the latest backup zip exists and store its name in LBZ (LatestBackupZip)
' 2. Stop the Livecontent Service
' 3. Remove .dbx, .lck and .log files from DataFolder
' 4. restart the service
' 5. Restore the database

Dim fileNewest

Dim fso Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Set WshShell = WScript.CreateObject("WScript.Shell")

Dim ImportFldr

If fso.FolderExists( "C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data\Import" ) Then Set ImportFldr = fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data\Import") Else If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data\export" ) Then Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export") End If

Dim DataFldr

If fso.FolderExists( "C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data" ) Then Set DataFldr= fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data") Else If fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data") Then Set DataFldr= fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data") End If

For Each aFile In ImportFldr.Files sExtension = fso.GetExtensionName(aFile.Name)

If sExtension = "log" Then
        'Msgbox "The file extension is a " & sExtension
    Else
        'Msgbox "The file extension is a " & sExtension
                If fileNewest = "" Then
                    Set fileNewest = aFile  
                Else
                    If fileNewest.DateCreated < aFile.DateCreated Then
                        If CDate(fileNewest.DateCreated) < CDate(aFile.DateCreated) Then
                            Set fileNewest = aFile
                        End If
                    End If
                End If
    End If

Next Msgbox "The Newest File in the folder is " & fileNewest.Name & chr(13) & "Size: " & fileNewest.Size & " bytes" & chr(13) & "Was last modified on " & FileNewest.DateLastModified

' Change to /Data 'WScript.Echo WshShell.CurrentDirectory WshShell.CurrentDirectory = DataFldr 'WScript.Echo WshShell.CurrentDirectory

' Stop the Livecontent service strComputer = "." Dim objService Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'LiveContent'")

For Each objService in colServiceList WScript.Echo objService.State If objService.State = "Running" Then objService.StopService() 'Wscript.Sleep 5000 End If Next

'Dim objShell 'Set objShell = CreateObject("WScript.Shell") 'objShell.Run "%comspec% /k c: & cd ""C:\Program Files (x86)\XyEnterprise\SDL LiveContent\"" & """"loaddb RESTORE -'Dlc.file=C:\PROGRA~2\XYENTE~1\SDLLIV~1\data\Import\" & fileNewest.Name & " -Dlc.pswd=N2kAs72z"""""

答案 1 :(得分:0)

经过非常广泛的测试后,我们得出结论:脚本启动和停止其他服务没有任何问题,只是这个特定的服务不会停止使用这种方法。

为此,我们继续前进,现在正在使用

objShell.Run“sc start LiveContent”

这是一种享受。

感谢布兰登的帮助。