站長(reterry)推薦此篇文章,想學(xué)vbscript的朋友,我建議到微軟的官方網(wǎng)站去看,那里的東西很不錯,生動幽默,我以前學(xué)vbscirpt的時候經(jīng)常去那里看,而且好多東西,不一定剛開始能看的懂,但不要灰心,把感覺不錯的,你可以用本子抄一遍,學(xué)習(xí)效果會更好,然后下載個vbscirpt幫助文件,微軟有的下,然后看看多練習(xí)。
花了半天時間在MS TechNet看《腳本的故事》,文章寫得很生動幽默,要是所有的有技術(shù)文章都以這種輕松的方式來寫就好了。
WMI -- Windows Management Instrumentation
相關(guān)鏈接:
微軟《腳本指南》: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,顯示本機總內(nèi)存
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" '運行記事本
調(diào)用命令程序(%COMSPEC%環(huán)境變量調(diào)用相應(yīng)操作系統(tǒng)的cmd.exe 或 command.exe)運行腳本,并保持console窗口:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%COMSPEC% /k ipconfig"
使用objShell的exec方法代替run方法可將運行返回一個WshScriptExec對象,可對結(jié)果顯示做更多的控制。
運行腳本exam.vbs:
在命令行下輸入:cscript exam.vbs
使用重定向符將腳本運行結(jié)果輸出到文本文件:
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