Windows管理脚本学习
花了半天时间在ms technet看《脚本的故事》,文章写得很生动幽默,要是所有的有技术文章都以这种轻松的方式来写就好了。
wmi -- windows management instrumentation
相关链接:
微软《脚本指南》:
msdn wmi scripting primer:
脚本示例1,显示本机总内存
strcomputer = "."
set wbemservices = getobject("winmgmts:\\" & strcomputer)
set wbemobjectset = wbemservices.instancesof("win32_logicalmemoryconfiguration")
for each wbemobject in wbemobjectset
wscript.echo "total physical memory (kb): " & wbemobject.totalphysicalmemory
next
脚本示例2,
strcomputer = "."
set objwmiservice = getobject("winmgmts://" & strcomputer & "/root/cimv2")
strwql = "select * " & _
"from __instancecreationevent " & _
"within 2 " & _
"where targetinstance isa 'win32_process' " & _
"and targetinstance.name = 'notepad.exe'"
wscript.echo "waiting for a new instance of notepad to start..."
set objeventsource = objwmiservice.execnotificationquery(strwql)
set objeventobject = objeventsource.nextevent()
wscript.echo "a new instance of notepad was just started."
在脚本中使用外壳(shell)程序
set objshell = wscript.createobject("wscript.shell")
objshell.run "notepad" '运行记事本
调用命令程序(%comspec%环境变量调用相应操作系统的cmd.exe 或 command.exe)运行脚本,并保持console窗口:
set objshell = createobject("wscript.shell")
objshell.run "%comspec% /k ipconfig"
使用objshell的exec方法代替run方法可将运行返回一个wshscriptexec对象,可对结果显示做更多的控制。
运行脚本exam.vbs:
在命令行下输入:cscript exam.vbs
使用重定向符将脚本运行结果输出到文本文件:
cscript exam.vbs > output.txt //覆盖方式
cscript exam.vbs >> output.txt //保留添加方式
使用filesystemobject输出到文件:
set objfs = createobject("scripting.filesystemobject")
set objnewfile = objfs.createtextfile("output.txt")
objnewfile.writeline "header information -- date: " & now()
objnewfile.close
脚本主机script host:
wscript.exe 基于gui窗口
cscript.exe 基于控制台命令console