【问题标题】:Can't stop a service using vbscript无法使用 vbscript 停止服务
【发布时间】:2011-09-06 11:05:33
【问题描述】:

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

51处的代码行是:

 objService.StopService()

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

谢谢

EDIT 脚本的完整代码(感谢 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"""""

最新编辑

我已获取您的代码,但仍然无法正常工作。我注意到这一行:

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

在我添加的“winmgmts:\”处丢失了一个\,我希望您检查是否有(x86)目录,因为我正在32位服务器上测试它,但将它移到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
字符: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"

这是一种享受。

感谢布兰登的帮助。

【问题讨论】:

  • 我遇到了同样的问题。结果发现该服务与服务器具有相同的名称更改了服务名称,我现在可以从脚本启动和停止服务

标签: vbscript


【解决方案1】:

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

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

objShell.Run "sc start LiveContent"

这是一种享受。

感谢布兰登的帮助。

【讨论】:

    【解决方案2】:

    有几个小问题:

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

    2.) 文件循环中不匹配的 If ... Then & End 语句。 (我可能会为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"""""

    【讨论】:

    • 布兰登,对不起,我没有给你太多的信息。我已将您的答案包含在我原来的问题中,并将整个脚本粘贴进去。
    • 谢谢布兰登,我明天会试试这个。我们有一个充满“挑战”的日子,所以有点忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多