使用Powershell从任务管理器中检索信息

时间:2016-05-03 13:18:26

标签: powershell vbscript taskmanager

我目前正在使用商业自动化软件,其想法是机器人模拟用户操作。有没有办法通过powershell访问任务管理器,因为机器人旨在操纵任务管理器,例如传入一个值来启动一个新任务,结束一个进程并查看cpu等的性能。我知道这可以使用powershell脚本或更好的vb脚本来实现。请问我该如何做到这一点?

对于记录,我使用的是带有PowerShell版本3.0的Windows 7机器。

2 个答案:

答案 0 :(得分:1)

列出正在运行的流程并终止计算器。

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    msgbox objItem.ProcessID & " " & objItem.CommandLine
    If objItem.name = "Calculator.exe" then objItem.terminate
Next

监控记事本退出并重新启动。

同样可用Win32_ProcessStartTraceWin32_ProcessStartStopTrace

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
    If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then 
        Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
        WshShell.Run "c:\Windows\notepad.exe", 1, false
    End If
Loop

WMIC命令行程序使用与vbscript(和powershell)相同的类,因此您可以使用它来寻求帮助。

wmic /?
wmic path win32_process /?

或使用[{1}}

process别名的wmic别名中唯一可用的
win32_process

以及wmic的其他用途

wmic process /?
wmic process call /?
wmic process get /?

答案 1 :(得分:0)

尝试使用get-process获取正在运行的进程列表。

使用ie get-process myprocess |stop-process,您可以停止myprocess

对于新流程,您可以&他们(& C:\Windows\System32\taskmgr.exe)或使用start-process(检查Get-help Start-Process获取帮助)cmdlet。 您也可以使用Invoke-WmiMethod

Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList taskmgr.exe'