WMI Minute -- Windows管理脚本学习
2004-07-13 14:35
435 查看
花了半天时间在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
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
相关文章推荐
- Windows管理脚本学习
- 用WMI实现Windows系统自动管理(多图)
- PowerShell 2.0之使用WMI管理Windows(一)WMI基础
- shell脚本攻略学习笔记(九)管理重任
- [11] Windows PowerShell DSC学习系列---如何定制一个基于PowerShell脚本实现的DSC Resource
- Win32汇编调用WMI (windows管理系统的核心 COM组件)
- 我的python学习之路----运行脚本文件(windows)
- Windows Bat批处理脚本学习一命令介绍
- windows batch 脚本学习
- Linux 学习笔记_6_1.3_RedHat系列软件包管理(下)_脚本安装
- PowerShell 2.0之使用WMI管理Windows(一)WMI基础
- 批处理学习-Windows管理实用批处理文件-很实用!
- node.js在windows下的学习笔记(8)---进程管理Process
- Shell脚本学习笔记(八)--进程管理和工作控制
- PowerShell 2.0之使用WMI管理Windows(一)WMI基础 推荐
- python 编写管理redmine的相关小脚本,python学习中
- 大数据学习笔记:windows下hadoop的启动脚本分析,start-all.cmd
- 一个很好的windows 管理脚本网站
- Windows分页管理机制的学习(一)实践2
- Linux系统命令及Shell脚本学习笔记二:用户管理