问题
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-WmiMethod
for 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