How to run a file using VisualBasicScript (.vbs)

后端 未结 6 1548
你的背包
你的背包 2020-12-10 06:57

How can I run a file with VisualBasicScript (.vbs)?

The file is \'file.bat\' and it\'s located in the same dir as the .vbs.

6条回答
  •  执笔经年
    2020-12-10 07:32

    See many examples on technet Script Center Script Repository.

    A simple example is Select and Ping Computers Using a Text File:

    On Error Resume Next
    
    Const ForReading = 1
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)
    
    strComputers = objTextFile.ReadAll
    objTextFile.Close
    
    arrComputers = Split(strComputers, vbCrLf)
    Set objShell = CreateObject("WScript.Shell")
    
    For Each strComputer In arrComputers
    
        strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer
        Set objExecObject = objShell.Exec(strCommand)
        strText = objExecObject.StdOut.ReadAll
        If Instr(strText, "Reply") > 0 Then
    
        ' =====================================================================
        ' Insert your code here
        ' =====================================================================
    
            Set objWMIService = GetObject _
                ("winmgmts:\\" & strComputer & "\root\cimv2")
            Set colItems = objWMIService.ExecQuery _
                ("Select * From Win32_OperatingSystem")
            For Each objItem In ColItems
                Wscript.Echo strComputer & ": " & objItem.Caption
            Next
    
    
        Else
            Wscript.Echo strComputer & " could not be reached."
        End If
    
    Next
    

提交回复
热议问题