站长资源脚本专栏
Windows管理脚本学习
花了半天时间在MS TechNet看《脚本的故事》,文章写得很生动幽默,要是所有的有技术文章都以这种轻松的方式来写就好了。
WMI -- Windows Management Instrumentation
相关链接:
微软《脚本指南》:http://www.microsoft.com/china/technet/community/columns/scripts/default.mspx
MSDN WMI Scripting Primer:http://www.microsoft.com/china/technet/archives/columns/scripts/sg0103.asp
脚本示例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