找回密码
 加入
搜索
查看: 5170|回复: 8

[AU3基础] 用AU3启动服务,如何等待服务完全启动好再执行下一步操作?

  [复制链接]
发表于 2015-2-25 09:51:20 | 显示全部楼层 |阅读模式
请教,用AU3启动或停止服务,如何等待服务完全启动好或完全停止好再执行下一步操作?谢谢!
发表于 2015-2-25 09:56:58 | 显示全部楼层
while _Service_QueryStatus
 楼主| 发表于 2015-2-25 14:17:26 | 显示全部楼层
回复 2# netegg

这是什么函数,写上去没有反应哦。
发表于 2015-2-25 14:24:33 | 显示全部楼层
#Include <Services.au3>
while _Service_QueryStatus($sServiceName) = 1(已停止)/ 4(运行中)
 楼主| 发表于 2015-2-25 15:11:35 | 显示全部楼层
回复 4# netegg

我试了不可以,你确定可以吗?蛋蛋。
发表于 2015-2-25 21:28:07 | 显示全部楼层
本帖最后由 netegg 于 2015-2-25 21:30 编辑

while _Service_QueryStatus($sServiceName) =  4(运行中)
_Service_Stop( $sServiceNa)
sleep(100)
wend
。。。。。
发表于 2015-3-5 10:29:37 | 显示全部楼层
$TimeOut = 60
_ServiceCheck()

Func _ServiceCheck($flag = True)
        If $flag Then _Message('正在检查"Secondary Logon"服务状态')
        Local $Res[2]
        Local $starttime = TimerInit()
        Do
                $Res = _WMI_GetServiceState(".", "Secondary Logon")
                Sleep(10)
                _WMI_SetServiceState(".", "Secondary Logon", $Res)
                Sleep(10)
                $Passtime = TimerDiff($starttime)
        Until ($Res[1] == 'Running') Or ($Passtime > $TimeOut * 1000)
        If $Res[1] == 'Running' Then
                If $flag Then _Message('"Secondary Logon"服务正常')
                Sleep(1000)
                SplashOff()
                Return SetError(0, 1, True)
        ElseIf ($Passtime > $timeout * 1000) Then
                _Message('"Secondary Logon"服务未正常启动,等待超时')
                Sleep(1000)
                SplashOff()
                MsgBox(0, '失败', '关键服务"Secondary Logon"没有成功启动')
                Return SetError(0, 1, False)
        EndIf
EndFunc   ;==>_ServiceCheck


Func _WMI_GetServiceState($ip, $Caption)
        Local $Res[2], $colItems
        Local $objWMIService = ObjGet("winmgmts:\" & $ip & "\root\CIMV2")
        If @error Then
                $Res[0] = '未知'
                $Res[1] = '未知'
                $objWMIService = ''
                Return SetError(1, 0, $Res)
        Else
                $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", 48)
                For $objItem In $colItems
;~                         If $objItem.Caption =$Caption  Then Return $objItem.Started;表示服务是启动(true)还是停止(false)
                        If $objItem.Caption = $Caption Then
                                $Res[0] = $objItem.StartMode
                                $Res[1] = $objItem.State
                        EndIf
                Next
                $colItems = ''
                $objWMIService = ''
                Return SetError(0, 0, $Res)
        EndIf
EndFunc   ;==>_WMI_GetServiceState

Func _WMI_SetServiceState($ip, $Caption, $set)
        Local $Res[2], $colItems
        Local $objWMIService = ObjGet("winmgmts:\" & $ip & "\root\CIMV2")
        If @error Then
                $Res[0] = '未知'
                $Res[1] = '未知'
                $objWMIService = ''
                Return SetError(1, 0, $Res)
        Else
                $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", 48)
                For $objItem In $colItems
;~                         If $objItem.Caption =$Caption  Then Return $objItem.Started;表示服务是启动(true)还是停止(false)
                        If $objItem.Caption = $Caption Then
                                $Res[0] = $objItem.ChangeStartMode('Automatic')
                                Switch $set[1]
                                        Case 'Running'
                                        Case 'Paused'
                                                $Res[1] = $objItem.ResumeService()
                                        Case 'Stopped'
                                                $Res[1] = $objItem.StartService()
                                EndSwitch
                        EndIf
                Next
                $colItems = ''
                $objWMIService = ''
                Return SetError(0, 0, $Res)
        EndIf
EndFunc   ;==>_WMI_SetServiceState

Func _Message($message)
        Local $w = 200, $h = 50
        SplashTextOn("请稍候", $message, 400, 100, Default, Default, 16, 11)
        ControlSetText("请稍候", "", "Static1", $message)
        Sleep(2000)
EndFunc   ;==>_Message
回复 6# netegg
发表于 2015-3-5 13:48:55 | 显示全部楼层
If _ServGetState($s_Serv) <> "Running" Then _ServStart($s_Serv)
发表于 2015-3-5 14:03:42 | 显示全部楼层
不好意思,wmi我不会
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-12 19:26 , Processed in 0.076295 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表