Windows services 使用批处理文件启用/禁用Windows服务

Windows services 使用批处理文件启用/禁用Windows服务,windows-services,Windows Services,是否有任何方法可以使用批处理文件启用和禁用windows服务。 我说的是更改服务的启动类型,即使用Net Start和Net Stop命令启用和禁用服务,而不是启动和停止服务。sc config Start=disabled sc config <service_name> start= disabled 这里ServiceName是服务的名称,flag有一个值auto,demand。或残疾。例如,要将服务设置为手动运行,命令如下 sc config ServiceName st

是否有任何方法可以使用批处理文件启用和禁用windows服务。 我说的是更改服务的启动类型,即使用Net Start和Net Stop命令启用和禁用服务,而不是启动和停止服务。

sc config Start=disabled
sc config <service_name> start= disabled
这里ServiceName是服务的名称,flag有一个值auto,demand。或残疾。例如,要将服务设置为手动运行,命令如下

sc config ServiceName start= demand
请注意,等号后面必须有一个空格。参数ServiceName的正确值可能并不总是显而易见的,可以使用下一个命令为所有服务查找它

sc \\servername config <service_name> Start= auto >> c:\temp\sc.txt

启动服务并将输出记录在
c:\temp\sc.txt

中。我有一个答案和一个问题。
sc \\servername start <service_name>  >> c:\temp\sc1.txt
我将这一行放在一起,以禁用正在运行的服务

sc query "wsearch"| find "RUNNING" >nul 2>&1 && net stop "wsearch" && sc config "wsearch" start= disabled
这将在尝试禁用和停止之前检查服务。有些我喜欢改成手动

有人能帮我把这个片段放到一个循环中吗

for loop sc query "wsearch"| find "RUNNING" >nul 2>&1 && net stop "wsearch" && sc config "wsearch" start= disabled
服务名称1 服务名称2 服务名称3 endif


通过这种方式,我可以在一个文件中创建一个段并输入我想要更改状态的所有服务。

我认为这有点晚了,但对于将来可能遇到这种情况的任何人来说,这是我用DB后端编写的一个小应用程序片段,构建阵列,循环并发送

   For SrvLoop As Integer = 0 To UBound(SrverName) - 1
        services = ServiceController.GetServices(SrverName(SrvLoop))
        For Each ChkLV In myobj.Items
            Srv = ChkLV.SubItems(3).Text
            i = ChkLV.SubItems(0).Text
            If ChkLV.Selected = True And Srv = SrverName(SrvLoop) Then
                Select Case Command
                    Case 1
                        If services(i).Status <> ServiceControllerStatus.Running Then
                            services(i).Start()
                        Else
                            MsgBox("Cannot Start a Service that is already Running", MsgBoxStyle.Information)
                        End If
                    Case 2
                        'If services(i).CanStop Then
                        If services(i).Status <> ServiceControllerStatus.Stopped Then
                            services(i).Stop()
                            'Else
                            '    If services(i).Status <> ServiceControllerStatus.Stopped Then
                            '        MsgBox("Service not able to be stopped currently" & vbCrLf & "Please try again in a few seconds", MsgBoxStyle.Information)
                            '    End If
                        End If
                End Select
                Progress.PB_Progress_Bar.Value += 1
            End If
        Next
    Next
    Progress.Dispose()
对于SrvLoop作为整数=0到UBound(SrverName)-1
services=ServiceController.GetServices(SrverName(SrvLoop))
对于myobj.项目中的每个ChkLV
Srv=ChkLV.子项(3).文本
i=ChkLV.子项(0).文本
如果ChkLV.Selected=True且Srv=SrverName(SrvLoop),则
选择案例命令
案例1
如果服务(i).状态ServiceControllerStatus.正在运行,则
服务(i).Start()
其他的
MsgBox(“无法启动已在运行的服务”,MsgBoxStyle.Information)
如果结束
案例2
'如果服务(i).可以停止,则
如果服务(i).状态服务控制器Status.停止,则
服务(一)停止
”“否则呢
'如果服务(i).Status ServiceControllerStatus.停止,则
'MsgBox(“当前无法停止服务”&vbCrLf&“请几秒钟后重试”,MsgBoxStyle.Information)
"完"
如果结束
结束选择
Progress.PB_Progress_Bar.Value+=1
如果结束
下一个
下一个
Progress.Dispose()
for loop sc query "wsearch"| find "RUNNING" >nul 2>&1 && net stop "wsearch" && sc config "wsearch" start= disabled
   For SrvLoop As Integer = 0 To UBound(SrverName) - 1
        services = ServiceController.GetServices(SrverName(SrvLoop))
        For Each ChkLV In myobj.Items
            Srv = ChkLV.SubItems(3).Text
            i = ChkLV.SubItems(0).Text
            If ChkLV.Selected = True And Srv = SrverName(SrvLoop) Then
                Select Case Command
                    Case 1
                        If services(i).Status <> ServiceControllerStatus.Running Then
                            services(i).Start()
                        Else
                            MsgBox("Cannot Start a Service that is already Running", MsgBoxStyle.Information)
                        End If
                    Case 2
                        'If services(i).CanStop Then
                        If services(i).Status <> ServiceControllerStatus.Stopped Then
                            services(i).Stop()
                            'Else
                            '    If services(i).Status <> ServiceControllerStatus.Stopped Then
                            '        MsgBox("Service not able to be stopped currently" & vbCrLf & "Please try again in a few seconds", MsgBoxStyle.Information)
                            '    End If
                        End If
                End Select
                Progress.PB_Progress_Bar.Value += 1
            End If
        Next
    Next
    Progress.Dispose()