利用WMI获取系统信息

发布时间:2020-07-31 来源: 工作计划 点击:

 用 利用 WMI 获取系统信息

 WMI(Windows Management Instrumentation)技术是微软提供的 Windows 下的系统管理工具。通过该工具可以在本地或者管理客户端系统中几乎一切的信息。很多专业的网络管理工具都是基于 WMI 开发的。该工具在 Win2000 以及WinNT 下是标准工具,在 Win9X 下是扩展安装选项。本文将介绍如何通过 VB 编程来访问 WMI 对象的编程。首先来看一个简单的通过 WMI 获取系统信息的范例,这个范例通过 WMI 对象获得系统中运行的的进程:Function Enum1()As String Dim WMI Set WMI=GetObject("WinMgmts:")Set objs=WMI.InstancesOf("Win32_Process")For Each obj In objs Enum1=Enum1+obj.Description+Chr(13)+Chr(10)Next End Function 在上面的代码中,首先通过 GetObject("WinMgmts:")获得 WMI 对象,在 WMI 对象下有很多的子项,在这里我们通过 WMI.InstancesOf("Win32_Process")获得系统中所有的进程列表子项。下面看一个完整的访问 WMI 对象的范例,这个范例获得计算机的信息。建立一个新工程,在 Form1 中添加一个 TextBox 控件以及一个CommandButton 控件,在 CommandButton 的 Click 事件中写入以下的代码:Private Sub Command1_Click()Dim s,System,item Dim iAs Integer Set System=GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")For Each item In System"List1.AddItem item.cputype s="Computer Info"&vbCrLf s=s&"*"&vbCrLf s=s&"计算机名称:"&item.name&vbCrLf s=s&"状态:"&item.Status&vbCrLf s=s&"类型:"&item.SystemType&vbCrLf s=s&"生产厂家:"&item.Manufacturer&vbCrLf s=s&"型号:"&item.Model&vbCrLf s=s&"内存:~"&item.totalPhysicalMemory24000&"mb"&vbCrLf s=s&"域:"&item.domain&vbCrLf"s=s&"工作组"&item.Workgroup&vbCrLf"获得工作组和域的选项不能同时用 s=s&"当前用户:"&item.username&vbCrLf s=s&"启动状态"&item.BootupState&vbCrLf s=s&"该计算机属于"&item.PrimaryOwnerName&vbCrLf s=s&"系统类型"&item.CreationClassName&vbCrLf s=s&"计算机类类型"&item.Description&vbCrLf For i=0 To 1"这里假设安装了两个系统s=s&Chr(5)&"启动选项"&i&":"&item.SystemStartupOptions(i)_&vbCrLf Next iNext Text1.Text=s End Sub 运行程序,点击 Command1,在 textBox 中

 就可以显示计算机的信息。在上面的代码中,程序通过GetObject("winmgmts:")获得 WMI 对象,然后获得下面的Win32_ComputerSystem 子项并通过访问 Win32_ComputerSystem 对象中的分项获得系统中的信息。需要说明的是,并不是所有的系统都支持 WMI,在有些系统中无法显示生产厂家等信息。现在的计算机以及网络组成十分复杂。例如系统硬件方面就有主板、硬盘、网卡.。软件方面有操作系统、系统中安装的软件、正在运行的进程等等。网络方面有域、工作组等等。利用 WMI 可以访问上面的全部信息,但是如果向上面一样的利用分项来访问的话会很麻烦。为此,WMI 提供了一种类似 SQL 语句的查询语句,可以通过查询语句获得 WMI 对象下的子项。下面是一个遍历系统中安装的网卡并返回网卡 MAC 地址的代码:Private Function MACAddress()As String Set objs=GetObject("winmgmts:").ExecQuery(_"SELECT MACAddress"&_"FROM Win32_NetworkAdapter"&_"WHERE"&_"((MACAddress Is Not NULL)"&_"AND(Manufacturer"&_""Microsoft"))")For Each obj In objs MACAddress=obj.MACAddress Exit For Next obj End Function 上面的代码获得 WMI 对象,然后运行 ExecQuery 执行一个 WMI 查询语句获得安装的网卡并返回网卡的 MAC 地址。WMI 还支持事件处理,让程序可以处理系统事件,例如程序运行、关闭,可移动驱动器的插入、取出等。下面是一个可以对系统中运行程序进行监控的程序。首先建立一个新工程,然后点击菜单的project|references 项,在 references 列表中选中 Microsoft WMI Scripting Library 将 WMI 对象库加入工程中。然后在 Form1 中加入一个 ListBox 控件,然后在 Form1 中加入以下代码:Option Explicit Dim Locator As SWbemLocator Dim Services As SWbemServices Dim WithEvents StatusSink As SWbemSink Private Sub KillEvents()StatusSink.Cancel Set StatusSink=Nothing End Sub Private Sub Form_Load()Dim Query As String Set StatusSink=New SWbemSink Set Locator=CreateObject("WbemScripting.SWbemLocator")Set Services=Locator.ConnectServer()Query="SELECT*FROM __InstanceCreationEvent"Query=Query+"WITHIN 1"Query=Query+"WHERE TargetInstance ISA"Win32_Process""Services.ExecNotificationQueryAsync StatusSink,Query End Sub Private Sub StatusSink_OnObjectReady(ByVal StatusEvent As SWbemObject,_ ByVal EventContext As

 SWbemNamedValueSet)Dim arr Dim strQue As String Dim iAs Integer List1.Clear arr=Split(StatusEvent.GetObjectText_,Chr(10))For i=LBound(arr)To UBound(arr)List1.AddItem arr(i)Next iEnd Sub Priv ate Sub StatusSink_OnCompleted(ByVal HResult As WbemErrorEnum,_ ByVal ErrorObject As SWbemObject,_ ByVal EventContext As SWbemNamedValueSet)If HResult wbemErrCallCancelled Then"错误处理 End If End Sub 在上面的程序中定义了一个 SWbemSink 对象 StatusSink,然后建立一个 SWbemServices 对象 Server,并将 StatusSink 连接到 Server 对象上。这样就可以通过 StatusSink 监控程序的运行。运行程序,然后任意运行一个程序,在 Form1 的 ListBox 中就可以列出运行的程序的信息。

 WMI 脚本高手不完全手册

 2006-10-08 12:02:39

 要成为 WMI 脚本高手当要认识一下什么叫 WMI 啦,下面将介绍一下有关WMI 的东西。Windows 管理规范(Windows Management Instrumentation)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。WMI 通过编程和脚本语言为日常管理提供了一条连续一致的途径。用户可以:1.在远程计算机器上启动一个进程。2.设定一个在特定日期和时间运行的进程。3.远程启动计算机。4.获得本地或远程计算机的已安装程序列表。5.查询本地或远程计算机的 Windows 事件日志。而 WMI 适用的运得环境也是有些限制的,WMI适用于所有最新版本的 Windows。WMI 附带在 Windows Me、Windows 2000、Windows XP 和 Windows Server 2003 之中。对于 Windows 98 和 Windows NT 4.0,可以访问并搜索"Windows Management Instrumentation(WMI)CORE 1.5(Windows 95/98/NT 4.0)"。注意:在 Windows NT 4.0 上安装并运行 WMI 之前,需要首先安装 Service Pack 4 或更高版本。WMI 需要的其他软件包括:1.Microsoft Internet Explorer 5.0 或更高版本。2.Windows script Host(WSH)。Windows 2000、Windows XP、Windows Server 2003、和 Windows Me 附带的 WSH,而不是 Windows NT4 或 Windows 98 附带的 WSH。您可以从以下地址下载 WSH WSH 的最新版本--包括在 Windows XP 和 Windows Server 2003 之中--是 WSH 5.6。要使 WMI 脚本可以正常的运行,Windows 里的 WMI 服务(winmgmt)保证是运行的,这样才可以实现 WMI 里的更多功能。好了,关于 WMI的一些基本的信息资料就说到这,要想看更多的可以到 MicroSoft 网站的 MSDN

 找。下面就简单的讲一下 WMI 脚本编写的基本要素,看看下面的代码://这个脚本是查看系统启动的引导配置参数,下面我们来看看关于 WMI 脚本编写的架构。On Error Resume Next//下面这行是比较重要的,它定义了主机的变量,可以是本机或远程主机,域上的机等,参数英文的"."是表示本机,要想实现其它机的可以填上其它机的主机名或 IP。strComputer="."//下面这行是通过GetObject 得到主机的 WMI 对象管理空间"\root\cimv2",如果是本机的是通过NT(Authentication)认证的,所以可以不用用户名和密码,而对于非本机或非域机的就要再加多几条参数,Set objWMIService=GetObject("winmgmts:\"&strComputer&"\root\cimv2")//执行 WMI 数据对象的查询//至于连接远程的要用下面的语句 Set objLocator=CreateObject("Wbems cripting.SWbemLocator")Set objService=objLocator.ConnectServer(strComputer,"root\cimv2","administrator","a")Set colItems=objWMIService.ExecQuery("Select*from Win32_BootConfiguration",48)

 //利用数组列出相关 For Each objItem in colItems Wscript.Echo"BootDirectory:"&objItem.BootDirectory Next 从上面的例子可以看出写一个 WMI 的要求:1.得到主机的 WMI 对像管理空间 2.执行 WMI 数据对象的查询 3.利用数组列出相关学习编写的架构并不难,只要练多几次就行了,但是学习 WMI 的第一个难题就是它的子集对象,因为我们并不知道它的子集对象是什么,这样写起程序来就会力不从心了。要一下子知道这样子集的对象也是不难的,只要在 MicroSoft 的 MSDN 找找会有不少,但是这样找下去的话可能要找很久或资料不够全,是不是有些难呢?其实 MicroSoft 公司的网站上有一个叫"scriptomatic"的工具,才 100 多 K,解压后你们发觉真正有用的是那个才 12k 的"scriptomatic.hta"文件,双击打开后你会发觉是一个子集的数据列表,且还有例子呢。

 以上就是查询"Win32_BIOS"里的子集参数,是不是很易实现 WMI 脚本的编写呢?朋友们,可曾记得大半年前是不是有一个这样的漏洞:就是一个 GUEST 用户权限可以用 WMI 的脚本实现加账号的例子,其实就是一个 WMI 命名空间的安全性出现问题。下面我们打开计算机上的 MMC 看看如何设置 WMI 的安全权限。在运行菜单上打"MMC",然后在"文件"菜单上选"添加/删除管理单元",然后在"独立"的选项卡(默认)上按"添加",之后来到"添加独立管理单元"列表。然后就

 一路按"添加"、"确定"就可以了。返回到 MMC 的主介面上,然后右击"WMI"单元选"属性"。在 WMI 控件属性对话框中单击安全选项卡。一个名为 Root,前面带加号(+)的文件夹将会出现。如果必要,展开这个树状结构,定位到想要设置权限的命名空间。单击安全设置按钮。一组用户和权限显示出来。如果用户在这个列表中,请按照需要修改权限。如果用户不再这个列表中,请单击添加按钮,然后从账户所在的位置(本地计算机、域等等)添加用户。小提示:为了查看和设置 NameSpace 安全性,用户必需拥有读取安全设置和编辑安全设置权限。系统管理员默认具备这些权限,并可以按照需要将权限赋予其他用户如果一个用户需要远程访问命名空间,必须为其选中远程启用权限。默认情况下,针对一个命名空间设置的用户权限只对该命名空间有效。如果希望用户可以访问该命名空间和其下所有子命名空间,或者只能访问子命名空间,请单击高级按钮。单击编辑并在出现的对话框中指定允许访问的范围。这样就可以防止此类事情的发生,但是透过此类的 WMI 命名空间的安全设置,也可以成为黑手会配置后门的地方,所以在架建一个安全的系统,这里不能不看。今天的 WMI 技术就介绍到这里,文章写得有些仓促,难免有问题,请各位多多指点小弟。

 利用 GetObject("WinMgmts:")获取系统信息

 利用 GetObject("WinMgmts:")获取系统信息收藏用 WMI 对象列出系统所有进程:

 --Instance.vbs--

 Dim WMI,objs Set WMI=GetObject("WinMgmts:")Set objs=WMI.InstancesOf("Win32_Process")For Each obj In objs Enum1=Enum1+obj.Description+Chr(13)+Chr(10)Next msgbox Enum1

 获得物理内存的容量:

 ---physicalMemory.vbs---

 strComputer="."

 Set wbemServices=GetObject("winmgmts:\"&strComputer)Set wbemObjectSet=wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

 For Each wbemObject In wbemObjectSet WScript.Echo"物理内存(MB):"&CInt(wbemObject.TotalPhysicalMemory/1024)Next

 取得系统所有服务及运行状态

 --service.vbs--Set ServiceSet=GetObject("winmgmts:").InstancesOf("Win32_Service")Dim s,infor infor=""for each sin ServiceSet infor=infor+s.Description+"=="+s.State+chr(13)+chr(10)next msgbox infor CPU 的序列号:

 ---CPUID.vbs---

 Dim cpuInfo cpuInfo=""set moc=GetObject("Winmgmts:").InstancesOf("Win32_Processor")for each mo in moc cpuInfo=CStr(mo.ProcessorId)msgbox"CPU SerialNumber is:"&cpuInfo next

 硬盘型号:---HDID.vbs---Dim HDid,moc set moc=GetObject("Winmgmts:").InstancesOf("Win32_DiskDrive")for each mo in moc HDid=mo.Model msgbox"硬盘型号为:"&HDid next

 网卡 MAC 物理地址:

 ---MACAddress.vbs---Dim mc set mc=GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")for each mo in mc if mo.IPEnabled=true then msgbox"网卡 MAC 地址是:"&mo.MacAddress exit for end if next

 测试你的显卡:

 On Error Resume Next Dim ye Dim yexj00 set yexj00=GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_VideoController")for each ye in yexj00 msgbox"型号:"&ye.VideoProcessor&vbCrLf&"厂商:"&ye.AdapterCompatibility&vbCrLf&"名称:"&ye.Name&vbCrLf&"状态:"&ye.Status&vbCrLf&"显存:"&(ye.AdapterRAM24000)&"MB"&vbCrLf&"驱动(dll):"&ye.InstalledDisplayDrivers&vbCrLf&"驱动(inf):"&ye.inf"版本:"&ye.DriverVersion next

 *

 "WinMgmts:"Prefix Microsoft Windows 2000 Scripting Guide WMI monikers can consist of three parts:one mandatory component and two optional co mponents.The mandatory component is the"winmgmts:"prefix.All WMI monikers must begin with"winmgmts:"as shown here:

 Set objSWbemServices=GetObject("winmgmts:")The moniker in this code is the string"winmgmts:",which is passed to the GetObject function.Although in this example the string is entered using all lowercase letters,you can use whatever case you like;that is,"WinMgmts:","WINMGMTS:",and"winmgmts:"all produce the same result.

 Specifying amoniker that consists only of the"winmgmts:"prefix is the most basic form of WMI moniker you can use.The result is always areference to an SWbemServices object,which represents aconnection to the Windows Management Instrumentation service on the local computer.Under the covers,the"winmgmts:"moniker:

 1.Retrieves the WMI CLSID from the registry subkey HKCR\WINMGMTS\CLSID.The CLSID({172BDDF8-CEEA-11D1-8B05-00600806D9B6})is the identifier used by the operating system to map WMI to the appropriate COM object.

 2.Retrieves the value from asecond registry entry,HKCR\CLSID\{172BDDF8-CEEA-11D1-8B05-00600806D9B6}\InProcServer32.This value(typically C:\Windows\System32\wbem\wbemdisp.dll)indicates the path to the COM object that exposes the SWbemServices object.

 3.Loads Wbemdisp.dll,the DLL containing the WMI scripting library that exposes SWbemServices.

 After you have obtained areference to SWbemServices,you can then invoke one of the object methods as shown here:

 Set objSWbemServices=GetObject("winmgmts:")Set colSWbemObjectSet=objSWbemServices.InstancesOf("Win32_LogicalDisk")In

 this example,a reference variable named objSWbemServices is initialized using the"winmgmts:"moniker.This reference variable is subsequently used to invoke the InstancesOf method provided by the SWbemServices object.

 Although the preceding example is perfectly acceptable,you do not have to use two lines of code to retrieve all Win32_LogicalDisk instances.This can also be done with the following single line of script:

 Set colSWbemObjectSet=GetObject("winmgmts:").InstancesOf("Win32_LogicalDisk")In this case,a user-defined variable(objSWbemServices in the example preceding this one)is not used to explicitly reference the SWbemServices object commonly returned by GetObject and the WMI moniker.Instead,the"winmgmts:"moniker creates an SWbemServices reference in memory and immediately uses the unnamed,memory based refer ence to call the SWbemServices InstancesOf method.

 In the end,both examples produce identical results in the form of an SWbemObjectSet collection containing all instances of the Win32_LogicalDisk class on the local computer.You can also call the ExecQuery method or any other method provided by the SWbemServices object.In fact,if the objective of your script is to simply enumerate and echo all Win32_LogicalDisk instances,you can get by with as little as the following:

 For Each objDisk In GetObject("winmgmts:").InstancesOf("Win32_LogicalDisk")Wscript.Echo objDisk.DeviceID Next In this case,user-defined variables are not used to reference SWbemServices or SWbemObjectSet.Both objects are still created,but only in memory and without an explicit object reference.By using the most basic WMI moniker,and by understanding the relationship of the moniker with the WMI scripting library,you can begin to construct

 concise yet powerful WMI statements.记录激动时刻,赢取超级大奖!点击链接,和我一起参加"2010:我的世界杯 Blog 日志"活动!

 特别声明:

 1 :资料来源于互联网,版权归属原作者 2 :资料内容属于网络意见,与本账号立场无关 3 :如有侵权,请告知,立即删除。

相关热词搜索:获取 利用 系统

版权所有 蒲公英文摘 www.zhaoqt.net