Retrieving Information from Task Manager using Powershell

六月ゝ 毕业季﹏ 提交于 2019-12-10 12:14:29

问题


Am currently working with a business automation software, the idea is Robots simulate user actions. Is there a way to access the task manager through powershell as the robots are meant to manipulate the task manager e.g Pass in a value to start a new task, end a process and view the performance of the cpu etc . I know this can be achieved using powershell scripts or better still vb script. Please how do I achieve this?

For the records, I am using a windows 7 machine with powershell version 3.0.


回答1:


Try get-process for a list of running processes.

With i.e. get-process myprocess |stop-process you can stop myprocess.

For new processes you can & them ( & C:\Windows\System32\taskmgr.exe) or use the start-process (check Get-help Start-Process for help ) cmdlet. You can also use Invoke-WmiMethodfor this :

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




回答2:


List running processes and terminate calculator.

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

Monitor for notepad exiting and restart it.

Also available is Win32_ProcessStartTrace and Win32_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

The WMIC command line program uses same classes as vbscript (and powershell), so you can use it for help.

wmic /?
wmic path win32_process /?

or using the only available in wmic aliases of process alias for win32_process

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

and for other uses of wmic

wmic /node /?
wmic /format /?


来源:https://stackoverflow.com/questions/37005306/retrieving-information-from-task-manager-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!