如何在 vbscript 中创建和执行批处理文件?

How to create and execute a batch file in vbscript?

我试过这样做:

Dim objFSO, outFile, wshShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outFile = objFSO.CreateTextFile("paint.bat", True)
outFile.WriteLine "taskkill /f /im mspaint.exe"
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "paint.bat", 0, false

那是可行的,但出现错误 "The file is already being used by another process"

您自己的 cscriptwscript 进程已在使用该文件。您应该在 run.

之前使用 outFile.Close(也许还有 Set outFile = Nothing

直接在 vbscript 中终止绘制。

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
    If objitem.Name = "mspaint.exe" Then
        msgbox objitem.name & " PID=" & objItem.ProcessID & " SessionID=" & objitem.sessionid
        objitem.terminate
    End If
Next