欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vbs实现的tasklist效果代码

程序员文章站 2022-06-17 22:17:12
this short script uses wmi to display the same information than in the tool "tasklist"...

this short script uses wmi to display the same information than in the tool "tasklist", but dumping the result on the console. it can dump remote tasks running on another computer,
这个简短的脚本使用wmi显示与工具“tasklist”中相同的信息,但只有在命令行下输出结果。它也可以在另一台计算机上查看远程任务,
文件名:tasklist.vbs
要求:无
作者:jean-luc antoine
提交日期:2005年7月14日
类别:4k

核心代码

option explicit 
if right(ucase(wscript.fullname),11)="wscript.exe" then
	wscript.echo "you should run this script from the command line (cmd)" & vbcrlf & "cscript " & wscript.scriptfullname
	wscript.quit
end if

dim strcomputer,objwmiservice,colprocesses,objprocess,processtime,strcreationdate,user,domain,strowner,h,m,s,chaine
strcomputer = "." 
set objwmiservice = getobject("winmgmts:" & "{impersonationlevel=impersonate}!\\" & strcomputer & "\root\cimv2") 
set colprocesses = objwmiservice.execquery ("select * from win32_process")    ' where name='iexplore.exe' 
chaine="process   |creation date time |handles|threads|owner       |priority|memoryusage|maxmemusage|memrequired|maxmemrequi|    swap|  max swap|cpu time|pid |pfaults|path" & vbcrlf 
for each objprocess in colprocesses 
    if objprocess.getowner ( user, domain ) = 0 then 
        strowner= domain & "\" & user 
    else 
        strowner="unknown" 
    end if 
    processtime=(csng(objprocess.kernelmodetime) + csng(objprocess.usermodetime)) / 10000000 
    h=right("0" & fix(processtime/60/60),2) 
    processtime=processtime-h*60*60 
    m=right("0" & fix(processtime/60),2) 
    s=right("0" & round(processtime-m*60),2) 
    strcreationdate=mid(objprocess.creationdate,7,2) & "/" & mid(objprocess.creationdate,5,2) & "/" & left(objprocess.creationdate,4) & " " & mid(objprocess.creationdate,9,2) & ":" & mid(objprocess.creationdate,11,2) & ":" & mid(objprocess.creationdate,13,2) 
    if strcreationdate="// ::" then strcreationdate=space(19) 
    chaine=chaine & left(objprocess.name & space(8),12) & "|" _ 
        & strcreationdate & "|" & right(space(6) & objprocess.handlecount,7) & "|" _ 
        & right(space(6) & objprocess.threadcount,7) & "|" _ 
        & left(strowner & space(14),19) & "|" _ 
        & left(objprocess.priority & space(7),8) & "|" _ 
        & right(space(10) & objprocess.pagefileusage ,11) & "|" _ 
        & right(space(10) & objprocess.peakpagefileusage ,11) & "|" _ 
        & right(space(8) & objprocess.workingsetsize ,11) & "|" _ 
        & right(space(8) & objprocess.peakworkingsetsize ,11) & "|" _ 
        & right(space(10) & objprocess.virtualsize ,11) & "|" _ 
        & right(space(10) & objprocess.peakvirtualsize ,11) & "|" _ 
        & h & ":" & m & ":" & s & "|" _ 
        & left(objprocess.processid & space(3),4) & "|" _ 
        & right(space(6) & objprocess.pagefaults ,7) & "|" & objprocess.executablepath 
    chaine=chaine & vbcrlf 
next 
wscript.echo chaine

代码运行方法,将上面的代码为tasklist.vbs

在cmd下 cscript tasklist.vbs 即可,也可以在cmd下 cscript tasklist.vbs >list.txt直接将结果保存为list.txt查看也比较容易

好了,代码到此结束

相关标签: tasklist