我是VBS的新手,但是能够创建一个简单的脚本来确定PowerPoint是否在我的本地计算机上运行。但是,我的最终目标是检查多台远程计算机,并显示如下结果: Machine1:未运行 Machine2:正在运行 Machine3:未运行 等等。
那就是我被困住的地方。
Dim i
Dim strComputer
Dim FindProc
strComputer = "."
FindProc = "powerpnt"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
wscript.echo "Powerpoint is running"
else
wscript.echo "Powerpoint is not running"
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
答案 0 :(得分:0)
编辑:
关于输出,您始终可以按照自己的需要将其写入文件中,或者通过在每次迭代过程中串联每个计算机状态并随后Wscript.Echo
来构建所需格式的字符串。由于各种原因(例如,用于引用目标计算机的字符串格式错误),可能找不到该进程,如果没有更多上下文,我真的无法确定。我重写了解决方案,试图使其与您自己的代码尽可能清晰和相似。
Dim FSObject, MachineListObject, outFileObject
Dim processName, objWMIService, coolProcesses, strComputer
Set FSObject = CreateObject("Scripting.FileSystemObject")
Set MachineListObject = FSObject.OpenTextFile("C:\test\remote_hosts.txt")
Set outFileObject = FSObject.CreateTextFile("C:\test\out.txt", True)
processName = "notepad++.exe" 'Change this to your process of choice
Do While Not MachineListObject.AtEndOfStream
strComputer = MachineListObject.ReadLine()
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & processName & "'")
If colProcesses.Count <> 0 then
outFileObject.Write "Machine " & strComputer & ": Service is running" & vbCrLf
Else
outFileObject.Write "Machine " & strComputer & ": Service is not running" & vbCrLf
End If
Loop
outFileObject.Close
这些可能会帮助您:What is "strComputer"?和Win32_Process(向下滚动到示例部分,以防您只想使其尽快运行)