WshShell.AppActivate doesn't seem to work in simple vbs script

后端 未结 5 1028
谎友^
谎友^ 2020-12-19 00:58

total vbs scripting newb here. I\'m trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what m

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 01:29

    Alternative solution using WMIService (no loop through all processes required):

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'") 
    
    If colItems.Count = 0 Then
        WScript.Echo "Process not found"
        Wscript.Quit
    End If
    
    For Each objProcess in colItems
        WshShell.AppActivate(objProcess.ProcessId)
        Exit For
    Next
    

提交回复
热议问题