我从以下站点获取了以下 VB 脚本:http://www.itninja.com/question/how-do-i-stop-start-service-in-vbscript
我在这个VB中做了一些小改动(取消了功能)
这个 VB 的目标是控制服务——停止或启动服务
所以我试着停止 Alerter 服务
而且我不明白为什么服务不会停止或者还有其他问题?在 VB 脚本中?
'// Name: ControlServiceWMI
'// Purpose: Controls services using WMI
'// Input: strComputer - String - the computer on which we want to control the service. Null means local machine
'// strService - String - the name of the service. If blank, the function will use strDisplayName
'// strDisplayName - String - the display name of the service. If blank, the function will use strService
'// strOperation - String - the operation for the service: START, STOP, PAUSE or CONTINUE
'// intWaitTime - Integer - nbr of times to loop (effectively, the nbr of seconds to wait
'// for 'strOperation' to complete
'// Output: strError - String - contains error text, if operation fails
'// Returns: True/False
'// =========================================================================================================
Dim strComputer
Dim strService
Dim strDisplayName
Dim strOperation
Dim intWaitTime
Dim strError
strComputer = pc
strService = Alerter
strDisplayName = Alerter
strOperation = "STOP"
intWaitTime = 3
'// Define WMI *state* constants these are for the 'State' property
Const WMI_SERVICE_STOPPED = "Stopped"
Const WMI_SERVICE_STARTED = "Running"
Const WMI_SERVICE_START_PENDING = "Start Pending"
Const WMI_SERVICE_STOP_PENDING = "Stop Pending"
Const WMI_SERVICE_RUNNING = "Running"
Const WMI_SERVICE_CONTINUE_PENDING = "Continue Pending"
Const WMI_SERVICE_PAUSE_PENDING = "Pause Pending"
Const WMI_SERVICE_PAUSED = "Paused"
Const WMI_SERVICE_ERROR = "Unknown"
'// Define WMI *status* constants these are for the 'Status' property
Const WMI_SERVICE_OK = "OK"
Const WMI_SERVICE_DEGRADED = "Degraded"
Const WMI_SERVICE_UNKNOWN = "Unknown"
Const WMI_SERVICE_PRED_FAIL = "Pred Fail"
Const WMI_SERVICE_STARTING = "Starting"
Const WMI_SERVICE_STOPPING = "Stopping"
Const WMI_SERVICE_SERVICE = "Service" '// ?
'// Define string constants for service methods
Const START_SERVICE = "START"
Const STOP_SERVICE = "STOP"
Const PAUSE_SERVICE = "PAUSE"
Const CONTINUE_SERVICE = "CONTINUE"
Const SET_AUTOMATIC = "AUTOMATIC"
Const SET_MANUAL = "MANUAL"
Const SET_DISABLED = "DISABLED"
'// Win32_Service Method Return Value Table
Const WMI_Success = 0
Const WMI_NotSupported = 1
Const WMI_AccessDenied = 2
Const WMI_DependentServicesRunning = 3
Const WMI_InvalidServiceControl = 4
Const WMI_ServiceCannotAcceptControl = 5
Const WMI_ServiceNotActive = 6
Const WMI_ServiceRequestTimeout = 7
Const WMI_UnknownFailure = 8
Const WMI_PathNotFound = 9
Const WMI_ServiceAlreadyRunning = 10
Const WMI_ServiceDatabaseLocked = 11
Const WMI_ServiceDependencyDeleted = 12
Const WMI_ServiceDependencyFailure = 13
Const WMI_ServiceDisabled = 14
Const WMI_ServiceLogonFailure = 15
Const WMI_ServiceMarkedForDeletion = 16
Const WMI_ServiceNoThread = 17
Const WMI_StatusCircularDependency = 18
Const WMI_StatusDuplicateName = 19
Const WMI_StatusInvalidName = 20
Const WMI_StatusInvalidParameter = 21
Const WMI_StatusInvalidServiceAccount = 22
Const WMI_StatusServiceExists = 23
Const WMI_ServiceAlreadyPaused = 24
'// Build an array of the possible return values
Dim strWMI_ReturnErrors
Dim arrWMI_ReturnErrors
strWMI_ReturnErrors = ""
strWMI_ReturnErrors = strWMI_ReturnErrors & "Success,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Not Supported,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Access Denied,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Dependent Services Running,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Invalid Service Control,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Cannot Accept Control,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Not Active,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Request Timeout,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Unknown Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Path Not Found,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Already Running,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Database Locked,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Dependency Deleted,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Dependency Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Disabled,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Logon Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Marked For Deletion,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service No Thread,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Circular Dependency,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Duplicate Name,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Name,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Parameter,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Service Account,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Service Exists,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Already Paused"
'// Just in case you left the trailing comma in place...
If Left(strWMI_ReturnErrors, 1) = "," Then
strWMI_ReturnErrors = Left(strWMI_ReturnErrors, Len(strWMI_ReturnErrors) - 1)
End If
arrWMI_ReturnErrors = Split(strWMI_ReturnErrors, ",")
Dim strQuery
Dim objComputer
Dim objServiceList
Dim objService
Dim intCounter
Dim blnServiceReturn
ControlServiceWMI = False
If Len(strService) = 0 And Len(strDisplayName) = 0 Then
strMsgText = ""
strMsgText = strMsgText & "Neither the service name or service display name were specified."
End If
On Error Resume Next
'// Get WMI objects and initial variables
'Set objComputer = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objComputer = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
'// Only interested in controllable services (i.e. not system services, drivers, etc)
'// (well, we would be if ANY service set its 'AcceptStop' flag...)
strQuery = ""
strQuery = strQuery & "Select * From Win32_Service"
strQuery = strQuery & " Where "
If Len(strService) > 0 Then
strQuery = strQuery & "Name = '" & strService & "'"
If Len(strDisplayName) > 0 Then
strQuery = strQuery & " And "
strQuery = strQuery & "DisplayName = '" & strDisplayName & "'"
End If
Else
strQuery = strQuery & "DisplayName = '" & strDisplayName & "'"
End If
'strQuery = strQuery & " And "
'strQuery = strQuery & "AcceptStop = True"
Set objServiceList = objComputer.ExecQuery (strQuery)
If Err.Number = 0 Then
If objServiceList.Count > 0 Then
For Each objService in objServiceList
'// Determine the operation and carry it out
With objService
Select Case (strOperation)
Case SET_AUTOMATIC
If (.StartMode <> SET_AUTOMATIC) Then
Err.Number = .ChangeStartMode("Automatic")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case SET_MANUAL
If (.StartMode <> SET_MANUAL) Then
Err.Number = .ChangeStartMode("Manual")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case SET_DISABLED
If (.StartMode <> SET_DISABLED) Then
Err.Number = .ChangeStartMode("Disabled")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case START_SERVICE
If (.State = WMI_SERVICE_STOPPED) Then
Err.Number = .StartService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case STOP_SERVICE
If (.State = WMI_SERVICE_RUNNING) Or (.State = WMI_SERVICE_PAUSED) Then
Err.Number = .StopService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STOPPED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case PAUSE_SERVICE
If (.State = WMI_SERVICE_RUNNING) Then
Err.Number = .PauseService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_PAUSED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
Case CONTINUE_SERVICE
If (.State = WMI_SERVICE_PAUSED) Then
Err.Number = .ContinueService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_RUNNING Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
End Select
End With
Next
Else
With Err
.Description = "The service name specified "
.Raise 999
If Len(strService) > 0 Then
If Len(strDisplayName) > 0 Then
.Description = .Description & "'" & strDisplayName & "'"
End If
Else
.Description = .Description & "=" & strService & "="
End If
.Description = .Description & " does not exist."
.Source = "ControlServiceWMI"
End With
End If
End If
End If
If Err.Number = 0 Then
ControlServiceWMI = True
Else
'// Loop through the error array and, when you hit what Err.Number is,
'// drop out and set the appropriate error text
For intCounter = 0 To UBound(arrWMI_ReturnErrors)
If Err.Number = intCounter Then
Err.Description = arrWMI_ReturnErrors(intCounter)
End If
Next
strMsgText = ""
strMsgText = strMsgText & "Error " & Err.Number & " occured."
If Len(Err.Description) > 0 Then
strMsgText = strMsgText & Err.Description
End If
strError = strMsgText
End If
On Error Goto 0
Sub Sleep(ByVal intDelayInSecs)
'// Sleep is here because, of course, one can't use WScript.Sleep in embedded VBS CAs
Dim datStart
Dim blnTimesUp
datStart = Now()
blnTimesUp = False
While Not blnTimesUp
If DateDiff("s", datStart, Now()) >= CInt(intDelayInSecs) Then
blnTimesUp = True
End If
Wend
End Sub
最佳答案
丢弃该代码并重新开始。严重地。如果您想做的只是启动/停止服务,那么与从头开始编写代码相比,剥离该代码会更麻烦。
service = "Alerter"
'action = "start"
action = "stop"
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Service WHERE Name='" & service & "'"
For Each s In wmi.ExecQuery(qry)
Select Case action
Case "start": If s.State = "Stopped" Then s.StartService
Case "stop" : If s.State = "Running" Then s.StopService
Case Else : WScript.Echo "Invalid action: " & action
End Select
Next
如果你想重启服务,你需要等到每个操作完成,因为它们是异步运行的(即调用立即返回,而操作在后台继续)。示例:
s.StopService
Do Until wmi.ExecQuery(qry & " AND State='Stopped'").Count > 0
WScript.Sleep 100
Loop
与启动服务类似,只是您检查 State='Running'。
向该循环添加超时可能是个好主意,这样您的脚本就不会在服务挂起时无限期地阻塞。
关于windows - 通过 VB 脚本停止/启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746932/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我有一个在Linux服务器上运行的ruby脚本。它不使用rails或任何东西。它基本上是一个命令行ruby脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru