windows wmi
There are many devices (servers, desktops, laptops, tablets, phones, etc) running a Windows operating system. Many of us who live in the nix based world have to work in this OS, or if we don't, we will, sooner or later. Besides the regular tools we can expect from a *nix system (say Apache, PHP, MySQL, C/C++ compilers, etc), Windows offers a set of unique features not present in any other OS, and WMI is one of them.
有许多运行Windows操作系统的设备(服务器,台式机,笔记本电脑,平板电脑,电话等)。 我们生活在基于nix的世界中的许多人都必须在此OS中工作,否则,我们迟早会这样做。 除了我们可以从* nix系统期待的常规工具(例如Apache,PHP,MySQL,C / C ++编译器等)之外,Windows还提供了一组其他操作系统没有的独特功能,WMI就是其中之一。
In this article, we will address the questions like: What is WMI? How to use WMI with PHP? We will have some minimal sample codes to go through the basic programming techniques.
在本文中,我们将解决以下问题:什么是WMI? 如何在PHP中使用WMI? 我们将通过基本的编程技术来提供一些最少的示例代码。
The MSDN site has its official definition of WMI in this article, out of which a few lines are extracted below:
MSDN站点在本文中有WMI的正式定义,下面摘录了几行:
Windows Management Instrumentation (WMI) is the Microsoft implementation of Web-Based Enterprise Management (WBEM), which is an industry initiative to develop a standard technology for accessing management information in an enterprise environment.
Windows Management Instrumentation(WMI)是Microsoft基于Web的企业管理(WBEM)的实现,WBEM是一项行业倡议,旨在开发用于在企业环境中访问管理信息的标准技术。
Three keywords here are: Web-Based, management information, enterprise environment. So if our managed IT environment is a large scale, Windows-based architecture and we want to retrieve management information of each individual node and present it in a web fashion, we will be required to interact with WMI. WMI is also capable of doing other things like spawning a process in a remote PC but that will be beyond the scope of this article.
这里的三个关键字是: 基于Web的 , 管理信息 , 企业环境 。 因此,如果我们的托管IT环境是基于Windows的大规模体系结构,并且我们希望检索每个单个节点的管理信息并以Web形式呈现,则将需要与WMI进行交互。 WMI还可以执行其他操作,例如在远程PC中生成进程,但这超出了本文的范围。
WMI provides the comprehensive knowledge of a machine, both hardware and software. It has the so called CIM (Common Information Model) to encapsulate the information in an object-oriented manner. It also provides several programming interfaces to retrieve said information. In a pure Windows environment, these will be PowerShell, VB Script and .NET languages. But in our case, it will be PHP.
WMI提供了机器的全面知识,包括硬件和软件。 它具有所谓的CIM( 通用信息模型 ),以面向对象的方式封装信息。 它还提供了几个编程接口来检索所述信息。 在纯Windows环境中,它们将是PowerShell ,VB脚本和.NET语言。 但是在我们的例子中,它将是PHP。
One of the fundamental questions when programming with WMI is: which "information" is available? In other words, which objects/classes are available? Luckily, Microsoft provides a full list of what WMI offers in terms of classes and their properties. Please visit here for a complete reference. In WMI programming, most of the time we are referring to Win32 Classes.
使用WMI进行编程时的基本问题之一是:哪些“信息”可用? 换句话说,哪些对象/类可用? 幸运的是,Microsoft提供了有关WMI提供的类及其属性的完整列表。 请访问此处以获取完整参考。 在WMI编程中,大多数时候我们指的是Win32类 。
On a host Windows machine, WMI must be installed to provide the CIM. By default, any Windows system newer than Windows XP should have WMI installed and enabled.
在主机Windows计算机上,必须安装WMI才能提供CIM。 默认情况下,任何比Windows XP更新的Windows系统都应安装并启用WMI。
We can verify that that is the case through the following two steps:
我们可以通过以下两个步骤验证这种情况:
Launch "Computer Management" in your Windows machine and see if the service called "Windows Management Instrumentation" is running. If not, start that service.
在Windows Computer Management启动“ Computer Management ”,然后查看名为“ Windows Management Instrumentation ”的服务是否正在运行。 如果没有,请启动该服务。
Launch "wbemtest" in your Command Prompt window. A dialog titled "Windows Management Instrumentation Test" will appear. A lot of the buttons in that dialog are currently disabled but we can click the "Connect..." button to invoke a new dialog similar to the one shown below:
在命令提示符窗口中启动“ wbemtest ”。 将出现一个标题为“ Windows Management Instrumentation Test ”的对话框。 该对话框中的许多按钮当前都处于禁用状态,但是我们可以单击“ Connect... ”按钮来调用一个类似于以下所示的新对话框:
Normally, we don't need to change a thing. root\cimv2 is the system built-in namespace for our WMI interface. Just click the "Connect" button in this dialog. It will bring us back to the previous window with all the buttons enabled.
通常,我们不需要更改任何东西。 root\cimv2是WMI接口的系统内置名称空间。 只需单击此对话框中的“ Connect ”按钮。 启用所有按钮后,它将带我们回到上一个窗口。
Being able to connect to a machine's WMI interface is just one of the pre-requisites. We also need to make sure the Windows Firewall will allow WMI calls to pass through.
能够连接到机器的WMI接口只是前提条件之一。 我们还需要确保Windows防火墙将允许WMI呼叫通过。
In Windows Firewall, choose "Advanced Settings" then enable both inbound and outbound access rules for WMI related entries. Please see the screenshots below.
在Windows防火墙中,选择“ Advanced Settings ”,然后为WMI相关条目启用入站和出站访问规则。 请查看下面的屏幕截图。
After we enable the WMI firewall rules in a remote machine, we can test the connection as illustrated in Step 2 above. To connect to a remote machine, we need to prefix the default namespace ("root\cimv2") with the IP or name of the PC we need to connect ("\\192.168.1.2\root\cimv2 for example") and provide the user name and password for that remote machine.
在远程计算机上启用WMI防火墙规则后,我们可以按照上面的步骤2中所示测试连接。 要连接到远程计算机,我们需要在默认名称空间(“ root\cimv2 ”) root\cimv2加上我们需要连接的PC的IP或名称(例如“ \\192.168.1.2\root\cimv2 ”)并提供该远程计算机的用户名和密码。
To use WMI (or more precisely, .NET functionality) in PHP, we will need to enable php_com_dotnet.dll. Add one line to the php.ini like this:
要在PHP中使用WMI(或更确切地说,.NET功能),我们需要启用php_com_dotnet.dll 。 像这样在php.ini添加一行:
extension=php_com_dotnet.dlland restart the web server.
并重新启动Web服务器。
Note: php_com_dotnet.dll is a Windows only extension. This means that we will have to run the WMI-calling PHP file in a WAMP-like environment, and not in a *nix environment. And, of course, the machines that we will manage via WMI all need to be Windows based.
注意: php_com_dotnet.dll是仅Windows的扩展名。 这意味着我们将必须在类似WAMP的环境中而不是在* nix环境中运行WMI调用PHP文件。 当然,我们将通过WMI管理的计算机都必须基于Windows。
Having done all the necessary preparation, and before we start programming WMI with PHP, we really need to get back to the fundamental question we raised earlier: What "information" is available?
在完成所有必要的准备工作之后,并且在我们开始使用PHP编程WMI之前,我们确实需要回到我们先前提出的基本问题: 哪些“信息”可用?
We can expect the WMI to provide information regarding the BIOS, CPU, disks, memory usage, etc. But how is this information presented?
我们可以期望WMI提供有关BIOS,CPU,磁盘,内存使用情况等的信息。但是如何显示这些信息?
Besides digging into the official documents provided, let's bring up the wbemtest dialog again and connect to our local machine. In the WMI Tester dialog, click the Enum Classes... button and bring up the below dialog:
除了深入研究提供的官方文档之外,让我们再次打开wbemtest对话框并连接到我们的本地计算机。 在“ WMI Tester对话框中,单击“ Enum Classes...按钮,并弹出以下对话框:
In this dialog, don't enter anything in the text box, choose Recursive and click OK. It should bring up another dialog like this:
在此对话框中,不要在文本框中输入任何内容,选择“ Recursive ,然后单击“ OK 。 它应该弹出另一个对话框,如下所示:
This is a very long list (1,110 objects in my Windows 8.1 PC). Your PC may give out a different list but should be more or less the same as this one. Please take some time to scroll through it and look at the names of the classes that WMI provides. For example, in the above image, we have highlighted a class Win32_LogicalDisk. This contains all the information related to the machine's logical disks. To get a deeper insight of what this class offers, please double click on that class and another Object editor dialog will appear:
这是一个很长的列表(在Windows 8.1 PC中为1,110个对象)。 您的PC可能会给出不同的列表,但应该与此列表大致相同。 请花一些时间浏览它,并查看WMI提供的类的名称。 例如,在上图中,我们突出显示了Win32_LogicalDisk类。 其中包含与计算机逻辑磁盘有关的所有信息。 要深入了解此类提供的内容,请双击该类,然后将出现另一个“ Object editor对话框:
Take a closer look at the Properties panel. All the properties listed here are those we can retrieve. For example, VolumeName will be the name we assigned for a logical disk.
仔细查看“属性”面板。 此处列出的所有属性都是我们可以检索的属性。 例如, VolumeName将是我们为逻辑磁盘分配的名称。
WMI's Win32 Classes have a lot of entries to look through. Some of the most frequently used are:
WMI的Win32类有很多条目可供浏览。 一些最常用的是:
Computer System Hardward Classes, including Cooling Device, Input Device (Keyboard, Mouse, etc), Mass Storage, Motherboard, Networking Device, Printing, Video & Monitor, etc. 计算机系统硬体类,包括冷却设备,输入设备(键盘,鼠标等),大容量存储,主板,网络设备,打印,视频和监视器等。 Installed Application Classes, including Font, etc. 已安装的应用程序类,包括字体等。 Operating System Classes, including Drivers, Memory, Processes, Registry, Users, etc. 操作系统类,包括驱动程序,内存,进程,注册表,用户等。 Performance Counter Classes, including all performance related classes. 性能计数器类,包括所有与性能相关的类。 etc, etc. 等等等We now have a much clearer picture of the structure of the WMI classes and their associated properties.
现在,我们对WMI类的结构及其相关属性有了更加清晰的了解。
The code snippet below shows some basic information about the logical disks of a remote machine on the IP 192.168.1.4:
下面的代码段显示了有关IP 192.168.1.4上的远程计算机的逻辑磁盘的一些基本信息:
<?php $pc = "192.168.1.4"; //IP of the PC to manage $WbemLocator = new COM ("WbemScripting.SWbemLocator"); $WbemServices = $WbemLocator->ConnectServer($pc, 'root\\cimv2', 'your account', 'your password'); $WbemServices->Security_->ImpersonationLevel = 3; $disks = $WbemServices->ExecQuery("Select * from Win32_LogicalDisk"); foreach ($disks as $d) { $str=sprintf("%s (%s) %s bytes, %4.1f%% free\n", $d->Name,$d->VolumeName,number_format($d->Size,0,'.',','), $d->FreeSpace/$d->Size*100.0); echo $str; }On my system, the above will print out something like:
在我的系统上,以上内容将打印出类似以下内容的内容:
C: (System) 104,864,059,392 bytes, 60.4% free D: (Data) 209,719,963,648 bytes, 84.3% free E: (Misc) 185,521,188,864 bytes, 95.3% freeThis is a very simple example but it lays down the fundamental structure and flow of a PHP WMI program.
这是一个非常简单的示例,但它列出了PHP WMI程序的基本结构和流程。
Firstly, a COM object instance of type WbemScripting.SWbemLocator is created.
首先,创建一个WbemScripting.SWbemLocator类型的COM对象实例。
Then the connection to the PC will be established via the ConnectServer method. The four parameters for this method call are self explanatory. Finally, we need to set the security impersonation to a proper level. Level 3 is the recommended level for WMI scripts. A detailed explanation of the level is documented here. Level 3 means "Impersonation", which means and we quote:
然后,将通过ConnectServer方法建立与PC的连接。 此方法调用的四个参数不言自明。 最后,我们需要将安全模拟设置为适当的级别。 建议将级别3用于WMI脚本。 有关级别的详细说明,请参见此处 。 第3级的意思是“ Impersonation ”,这意味着我们引用:
The server process can impersonate the client's security context on its local system. The server cannot impersonate the client on remote systems.
服务器进程可以在其本地系统上模拟客户端的安全上下文。 服务器无法模拟远程系统上的客户端。
In short, our script (and the service instance we created) are "impersonating" the user with the account/password provided. Perfect for what we need here.
简而言之,我们的脚本(以及我们创建的服务实例)使用提供的帐户/密码“模拟”用户。 非常适合我们在这里需要的东西。
Please note the code above is the way to create a remote COM connection to manage a remote PC. To manage a local PC, the syntax will be slightly different but not much:
请注意,上面的代码是创建远程COM连接以管理远程PC的方法。 要管理本地PC,语法会稍有不同,但差别不大:
<?php $pc = "."; $obj = new COM ("winmgmts:\\\\".$pc."\\root\\cimv2"); $disks = $obj->ExecQuery("Select * from Win32_LogicalDisk"); // Rest of the code is the same as previous remote connection sampleIt is somewhat simpler as we don't need to provide a credential and impersonate but this is based on the assumption that the user running this snippet has the Administrator privilege.
这有点简单,因为我们不需要提供凭据和模拟,但这是基于以下假设:运行此代码段的用户具有管理员权限。
To get the classes and their associated data, we have used a WQL (WMI Query Language) statement. It is very similar to SQL statements we issue to a MySQL server but in this case, we are retrieving data from WMI. Win32_LogicalDisk is one "table" in WMI that stores all information related to logical disks. To access data from other tables, please use the name listed in the Query Result dialog as shown above. This also allows us to filter the results. For example, Select * from Win32_LogicalDisk where size > 150000000000 will only return those logical devices with size over 150G (roughly).
为了获得类及其相关数据,我们使用了WQL(WMI查询语言)语句。 它与我们发布给MySQL服务器SQL语句非常相似,但是在这种情况下,我们正在从WMI检索数据。 Win32_LogicalDisk是WMI中的一个“表”,用于存储与逻辑磁盘有关的所有信息。 要访问其他表中的数据 ,请使用“ Query Result对话框中列出的名称,如上所示。 这也使我们可以过滤结果。 例如, Select * from Win32_LogicalDisk where size > 150000000000将仅返回大小超过150G(大约)的那些逻辑设备。
The ExecQuery statement, if successful, will return a variant typed object. One downside is that if we try to var_dump that object, PHP will simply print something like object (variant) #3.... Same thing happens when we try to var_dump the $d variable. There is actually nothing useful for further programming in the output.
如果成功,则ExecQuery语句将返回variant类型的对象。 缺点是,如果我们尝试var_dump该对象,PHP将只打印类似object (variant) #3... 。 当我们尝试var_dump $d变量时, var_dump发生同样的事情。 实际上,对于输出中的进一步编程没有任何用处。
In actuality, we just need to know that the object is iterable. In this case, when we use a foreach loop, every $d instance will hold an object reference to a logical disk. Then we can access the properties in that logical disk instance with the familiar -> notation. The properties list can be found in the Object editor dialog for that particular class as shown above.
实际上,我们只需要知道对象是可迭代的即可。 在这种情况下,当我们使用foreach循环时,每个$d实例将保存一个对逻辑磁盘的对象引用。 然后,我们可以使用熟悉的->符号访问该逻辑磁盘实例中的属性。 可以在该特定类的“ Object editor对话框中找到属性列表,如上所示。
Be sure to spell the class name (Win32_LogicalDisk) and property names (like Size, Name) correctly. Windows is not case sensitive but if we provide the wrong name, an error will be thrown and returned.
确保正确拼写类名( Win32_LogicalDisk )和属性名(如Size , Name )。 Windows不区分大小写,但是如果我们提供了错误的名称,则会引发错误并返回错误。
As we mentioned earlier, WMI programming can be done with other languages as well – languages like C#, VB Script, etc. However, the WMI COM interface is such a dynamic interface that we can't count on any of these languages to provide a code completion hint to have easy access to all the properties. We have to rely on the dialogues shown above.
正如我们前面提到的,WMI编程也可以使用其他语言(例如C#,VB Script等)来完成。但是,WMI COM接口是一种动态接口,我们不能依靠任何这些语言来提供代码完成提示可轻松访问所有属性。 我们必须依靠上面显示的对话。
One solution to help the programmers is to further encapsulate each WMI class into a PHP class with necessary methods. This should be a very straightforward task and I will leave it to those interested to play around with it.
帮助程序员的一种解决方案是使用必要的方法将每个WMI类进一步封装到PHP类中。 这应该是一个非常简单的任务,我将把它留给有兴趣的人一起玩。
WMI is a powerful tool holding some of the most hidden secrets kept by the Windows operating system. In a large scale network with homogeneous Windows based machines, we can rely on WMI to retrieve this vital information and help the system admins better manage all the machines.
WMI是一个功能强大的工具,其中包含Windows操作系统保留的一些最隐藏的秘密。 在具有基于Windows的同类计算机的大型网络中,我们可以依靠WMI检索此重要信息,并帮助系统管理员更好地管理所有计算机。
In this article, we only cover the very basics of WMI and PHP WMI programming but have laid down the fundamentals for further work.
在本文中,我们仅介绍WMI和PHP WMI编程的基础知识,但为进一步的工作奠定了基础。
Please leave your comments below if you'd like to see a more detailed WMI tutorial!
如果您想查看更详细的WMI教程,请在下面留下您的评论!
翻译自: https://www.sitepoint.com/php-wmi-dig-deep-windows-php/
windows wmi
相关资源:jdk-8u281-windows-x64.exe