启动所有未运行的服务并以相同名称开头

时间:2016-08-10 18:49:30

标签: batch-file

我有5个以名称" test"开头的服务。 我需要一个批处理文件来检查每个文件并在服务停止时启动它。

我有这样的脚本在1个服务的情况下工作但是?我不知道如何将其更改为多个服务:

Sub deleteThreeColDupes()

Dim sourceRange As range
Dim colOne As range
Dim colTwo As range
Dim myCell As range
Dim checkCell As range

'Set the search ranges
Set colOne = range("B2", Cells(Rows.count, 2).End(xlUp))
Set colTwo = range("F2", Cells(Rows.count, 6).End(xlUp))
Set sourceRange = range("D2", Cells(Rows.count, 4).End(xlUp))

'Compare with the first column. If there is a match, clear the value and exit the loop.
'if no match in first column, compare with the second column.
For Each myCell In sourceRange
    For Each checkCell In colOne
        If myCell.Value = checkCell.Value Then
            myCell.Value = ""
            Exit For
        End If
    Next checkCell
    If myCell.Value <> "" Then
        For Each checkCell In colTwo
            If myCell.Value = checkCell.Value Then
                myCell.Value = ""
                Exit For
            End If
        Next checkCell
    End If
Next myCell

'Clear sets
Set colOne = Nothing
Set colTwo = Nothing
Set sourceRange = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

您可以将net start放入for循环中。

示例:

for /f "tokens=* delims= " %%a in ('net start ^| find /i "Test"') do net start "%%a"

如果没有从批处理文件运行,请将%%a替换为%a

修改:如果您还想使用时间戳记录它,那么我们需要setlocal enabledelayedexpansion

会更加棘手
setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%a in ('net start ^| find /i "Test"') do net start "%%a" && echo service restarts @ !date! !time! >> C:\checklog.txt

注意:&&表示只有在net start成功时才会执行。因此,如果服务无法启动,或者已经启动,则无法记录。