当前位置:网站首页>PHP and WMI – explore windows with PHP

PHP and WMI – explore windows with PHP

2022-06-25 06:14:00 allway2

There are many running Windows Operating system equipment ( The server 、 Desktop computer 、 laptop 、 The tablet 、 Telephone, etc ). Many of us live on the basis of nix Everyone in the world must work in this operating system , Or if we don't , We will do it sooner or later . Except that we can learn from *nix General tools available in the system ( such as Apache、PHP、MySQL、C/C++ Compiler, etc. ) outside ,Windows It also provides a set of unique features that do not exist in other operating systems ,WMI Is one of them .

In this paper , We will solve the following problems : What is? WMI? How to be in PHP Use in WMI? We will have some minimal sample code to understand basic programming techniques .

What is? WMI And why we need to deal with it

MSDN The website is in This article Yes WMI Official definition , The excerpts are as follows :

Windows Management Instrumentation (WMI) yes Microsoft The implementation is based on Web Business management in China (WBEM), It is an industry initiative , Designed to develop standard technologies for accessing management information in an enterprise environment .

The three key words here are :Web-Based Management information Business environment . therefore , If our trusteeship IT The environment is a large scale based on Windows Architecture of , And we want to retrieve the management information of each individual node and Web Way to present it , We will need to work with WMI Interaction .WMI Can also perform other operations , For example, at a remote location PC Build process in , But this is beyond the scope of this article .

WMI Provide comprehensive knowledge of the machine , Including hardware and software . It has the so-called CIM( General information model ) Encapsulate information in an object-oriented manner . It also provides several programming interfaces to retrieve the information . In pure Windows Environment , These will be PowerShell、VB Scripts and .NET Language . But in our case , It will be PHP.

Use WMI One of the basic problems in programming is : Which? “ Information ” You can use ? let me put it another way , Which objects / Class available ? Fortunately, ,Microsoft Provides WMI A complete list of classes and their attributes . Please visit here Get a complete reference . stay WMI Programming , Most of the time we mean Win32 class .

precondition

Host computer Windows On the machine , Must install WMI In order to provide CIM. By default , Anything more than Windows XP The updated Windows All systems should be installed and enabled WMI.

We can verify whether this is the case through the following two steps :

  1. Computer Management In your Windows Start on the machine " " And see the name " Windows Management Instrumentation" Is your service running . without , Please start the service .
  2. wbemtest Start... In a command prompt window “ ”. A message with the title “ ” The dialog Windows Management Instrumentation Test. Many buttons in this dialog are currently disabled , But we can click “ Connect...” Button to invoke a new dialog box similar to the one shown in the following figure :

Usually , We don't need to change anything .root\cimv2 It is our WMI Interface's system built-in namespace . Just click Connect In this dialog box “” Button . It will take us back to the previous window that enabled all the buttons .

Capable of being connected to the machine WMI Interface is just one of the prerequisites . We also need to make sure that Windows The firewall allows WMI Call through .

stay Windows In the firewall , choice “ Advanced Settings”, And then to WMI Related entries enable inbound and outbound access rules . See screenshot below .

Enable... On the remote machine WMI After firewall rules , We can follow the above steps 2 Test connection shown . To connect to a remote machine , We need the default namespace (“ root\cimv2”) Add what we need to connect PC Of IP Or name (“\\192.168.1.2\root\cimv2 for example ”), And provide the user name and password of the remote machine .

WMI (.NET) Of PHP Expand

To be in PHP Use in WMI( Or more precisely ,.NET function ), We need to enable php_com_dotnet.dll.  Add a line like this php.ini

extension=php_com_dotnet.dll

And restart Web The server .

Be careful : php_com_dotnet.dll Is a limited Windows An extension of . That means we have to do something like WAMP Run the call in the environment of WMI Of PHP file , Not in *nix Environment . Of course , We will pass WMI All managed machines need to be based on Windows.

Learn more WMI Features provided

After all necessary preparations have been made , Before we start using PHP To write WMI Before , We really need to go back to the basic questions we raised before : What are they? “ Information ” You can use ?

We can expect WMI Provide relevant BIOS、CPU、 disk 、 Memory usage, etc . But how is this information presented ?

In addition to in-depth study of the official documentation provided , Let us wbemtest Open the dialog again and connect to our local machine . stay WMI Tester In the dialog box , single click Enum Classes... Button and the following dialog box pops up :

In this dialog , Do not enter anything in the text box , choice Recursive And click OK. It should pop up another dialog , As shown below :

It's a long list ( my Windows 8.1 PC There is 1,110 Objects ). Your PC Different lists may be given , But it should be roughly the same as this list . Please take some time to scroll through it and view WMI The name of the class provided . for example , In the diagram above , We highlight a class Win32_LogicalDisk. This contains all the information related to the logical disk of the machine . To get a deeper understanding of what the course offers , Please double-click the course , then Object editor Another dialog box will appear :

go through “ attribute ” panel . All the attributes listed here are retrievable . for example ,VolumeName Will be the name we assigned to the logical drive .

WMI Of Win32 Class has many entries to view . Some of the most commonly used are :

  • Computer system hardware , Including heat dissipation equipment 、 input device ( keyboard 、 Mouse, etc. )、 Mass storage 、 a main board 、 Network devices 、 Print 、 Video, display, etc .
  • Installed application classes , Including fonts, etc .
  • Operating system class , Including drivers 、 Memory 、 process 、 The registry 、 The user etc. .
  • Performance counter class , Include all performance related classes .
  • Etc., etc.

We are now WMI A clearer understanding of the structure of the class and its related properties .

use PHP To write WMI

The following code snippet shows IP 192.168.1.4 Some basic information about the logical disk of the remote machine on the :

<?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 following will be printed on it :

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% free

This is a very simple example , But it laid the foundation PHP WMI The basic structure and flow of the program .

First , Create of type COM Object instances WbemScripting.SWbemLocator.

Then it will pass through the ConnectServer Method establishment and PC The connection of . The four parameters of this method call are self explanatory . Last , We need to set the security impersonation to the appropriate level . Level 3 yes WMI Recommended level of script . here A detailed description of this level is recorded . The first 3 Level representation “ Impersonation”, This means that we quote :

The server process can impersonate the client's security context on its local system . The server cannot impersonate a client on a remote system .

In short , Our script ( And the service instance we created ) Using the account provided / password “ simulation ” user . It is very suitable for what we need here .

Please note that , The code above is to create a remote COM Connect to manage remote PC Methods . To manage the local PC, The grammar will be slightly different , But the difference is not big :

<?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 sample

It's a little simple , Because we don't need to provide credentials and impersonation , But this is based on the assumption that the user running this snippet has administrator privileges .

To get the class and its related data , We used WQL(WMI query language ) sentence . It goes with us to MySQL From the server SQL The sentences are very similar , But in this case , We from WMI Retrieving data .Win32_LogicalDisk yes WMI One of them “ surface ”, Used to store all information related to the logical disk . To access other surface Query Result Data in , Please use the names listed in the dialog box shown above . This also allows us to filter the results . for example ,Select * from Win32_LogicalDisk where size > 150000000000 Only those larger than will be returned 150G( about ) Logical device for .

ExecQuery If it works , This statement will return a variant Typed objects . One drawback is , If we try to use var_dump This object ,PHP Will simply print something like object (variant) #3...var_dump When we try to use $d variable , The same thing will happen . In fact, it is not useful for further programming in the output .

actually , We just need to know that the object is iteratable . under these circumstances , When we use foreach loop , Every $d Each instance will hold an object reference to the logical disk .-> Then we can use familiar symbols to access the properties in the logical disk instance . The attribute list can be found in Object editor Found in the dialog box for this particular class , As shown above .

Please spell the class name correctly ( Win32_LogicalDisk) And property name ( Such as SizeName).Windows Case insensitive , But if we provide the wrong name , An error is raised and returned .

As we mentioned earlier ,WMI Programming can also be done in other languages —— image C#、VB Script Other languages . however ,WMI COM Interface is a dynamic interface , We can't expect any of these languages to provide code completion hints to easily access all properties . We must rely on the dialogue shown above .

One solution to help programmers is to further integrate each WMI Class is encapsulated into a with the necessary methods PHP class . This should be a very simple task , I will leave it for those who are interested to play .

Conclusion

WMI It's a powerful tool , It contains some information from Windows The most secret kept by the operating system . With isomorphism based on Windows In a large-scale network of machines , We can rely on WMI To retrieve this important information and help system administrators better manage all machines .

In this paper , We only introduce WMI and PHP WMI Basic knowledge of programming , But it has laid a foundation for further work .

原网站

版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201241059423.html