使用PHP远程查询WMI

发布于 2024-09-26 01:16:16 字数 483 浏览 0 评论 0原文

我当前的代码如下所示:

define ( 'CPU_NAME', 'remote_server' );
$obj = new COM ( 'winmgmts:{impersonationLevel=impersonate}//' . CPU_NAME . '/root/cimv2' );
if ( is_object ( $obj ) ){ 
     $process = $obj->execquery ( "SELECT * FROM Win32_Process" );
}

我将把远程服务器的登录凭据放在哪里? 我看到它需要用户名和密码,但我不确定如何实现。

任何帮助将不胜感激。

参考:https://www.php.net/manual/en/class。 com.php

My current code looks like this:

define ( 'CPU_NAME', 'remote_server' );
$obj = new COM ( 'winmgmts:{impersonationLevel=impersonate}//' . CPU_NAME . '/root/cimv2' );
if ( is_object ( $obj ) ){ 
     $process = $obj->execquery ( "SELECT * FROM Win32_Process" );
}

Where would I put the login credentials for the remote_server?
I see that it would take a username and password, but I'm not sure how to implement that.

Any help would be appreciated.

Reference: https://www.php.net/manual/en/class.com.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风柔一江水 2024-10-03 01:16:16
<?php
    $obj = new COM ( 'winmgmts://localhost/root/CIMV2' );
    $fso = new COM ( "Scripting.FileSystemObject" );    
    $wmi_computersystem    =    $obj->ExecQuery("Select * from Win32_ComputerSystem");
    $wmi_bios              =    $obj->ExecQuery("Select * from Win32_BIOS");
    $processor             =    $obj->ExecQuery("Select * from Win32_Processor");
    $PhysicalMemory        =    $obj->ExecQuery("Select * from Win32_PhysicalMemory");
    $BaseBoard             =    $obj->ExecQuery("Select * from Win32_BaseBoard"); 
    $LogicalDisk           =    $obj->ExecQuery("Select * from Win32_LogicalDisk");


    foreach ( $wmi_computersystem as $wmi_call )
    {
        $model = $wmi_call->Model;
    }

    foreach ( $wmi_bios as $wmi_call )
    {
        $serial = $wmi_call->SerialNumber;
        $bios_version = $wmi_call->SMBIOSBIOSVersion;
    }

    foreach ( $processor as $wmi_processor )
    {
        $idprocessor = $wmi_processor->ProcessorId;
        $Architecture = $wmi_processor->Architecture;
        $Name = $wmi_processor->Name;
        $Version = $wmi_processor->Version;
    }
    foreach ( $PhysicalMemory as $wmi_PhysicalMemory )
    {
        $Capacity = $wmi_PhysicalMemory->Capacity;
        $PartNumber = $wmi_PhysicalMemory->PartNumber;
        $Name = $wmi_PhysicalMemory->Name;
    }

    foreach ( $BaseBoard as $wmi_BaseBoard )
    {
        $SerialNumber = $wmi_BaseBoard->SerialNumber;

    }
    foreach ( $LogicalDisk as $wmi_LogicalDisk )
    {
        $SerialNumberDisk = $wmi_LogicalDisk->VolumeSerialNumber;
        $FileSystem = $wmi_LogicalDisk->FileSystem;

    }

    echo "Bios version   : ".$bios_version."<br/>
          Serial number of bios  : ".$serial."<br/>
          Hardware Model : ".$model."<br/>
          ID-Processor : ".$idprocessor."<br/>
          Architecture-Processor : ".$Architecture."<br/>
          Name-Processor : ".$Name."<br/>
          Version-Processor : ".$Version."<br/>
          <hr>
          <hr>
          PhysicalMemory
          <hr>
          <hr>
          Capacity : ".$Capacity."<br/>
          Name : ".$Name."<br/>
          <hr>
          <hr>
          carte mere
          <hr>
          <hr>
          SerialNumber : ".$SerialNumber."<br/>
           <hr>
          <hr>
          disk
          <hr>
          <hr>
          SerialNumber : ".$SerialNumberDisk."<br/>
          FileSystem : ".$FileSystem."<br>
          ";

?>
<?php
    $obj = new COM ( 'winmgmts://localhost/root/CIMV2' );
    $fso = new COM ( "Scripting.FileSystemObject" );    
    $wmi_computersystem    =    $obj->ExecQuery("Select * from Win32_ComputerSystem");
    $wmi_bios              =    $obj->ExecQuery("Select * from Win32_BIOS");
    $processor             =    $obj->ExecQuery("Select * from Win32_Processor");
    $PhysicalMemory        =    $obj->ExecQuery("Select * from Win32_PhysicalMemory");
    $BaseBoard             =    $obj->ExecQuery("Select * from Win32_BaseBoard"); 
    $LogicalDisk           =    $obj->ExecQuery("Select * from Win32_LogicalDisk");


    foreach ( $wmi_computersystem as $wmi_call )
    {
        $model = $wmi_call->Model;
    }

    foreach ( $wmi_bios as $wmi_call )
    {
        $serial = $wmi_call->SerialNumber;
        $bios_version = $wmi_call->SMBIOSBIOSVersion;
    }

    foreach ( $processor as $wmi_processor )
    {
        $idprocessor = $wmi_processor->ProcessorId;
        $Architecture = $wmi_processor->Architecture;
        $Name = $wmi_processor->Name;
        $Version = $wmi_processor->Version;
    }
    foreach ( $PhysicalMemory as $wmi_PhysicalMemory )
    {
        $Capacity = $wmi_PhysicalMemory->Capacity;
        $PartNumber = $wmi_PhysicalMemory->PartNumber;
        $Name = $wmi_PhysicalMemory->Name;
    }

    foreach ( $BaseBoard as $wmi_BaseBoard )
    {
        $SerialNumber = $wmi_BaseBoard->SerialNumber;

    }
    foreach ( $LogicalDisk as $wmi_LogicalDisk )
    {
        $SerialNumberDisk = $wmi_LogicalDisk->VolumeSerialNumber;
        $FileSystem = $wmi_LogicalDisk->FileSystem;

    }

    echo "Bios version   : ".$bios_version."<br/>
          Serial number of bios  : ".$serial."<br/>
          Hardware Model : ".$model."<br/>
          ID-Processor : ".$idprocessor."<br/>
          Architecture-Processor : ".$Architecture."<br/>
          Name-Processor : ".$Name."<br/>
          Version-Processor : ".$Version."<br/>
          <hr>
          <hr>
          PhysicalMemory
          <hr>
          <hr>
          Capacity : ".$Capacity."<br/>
          Name : ".$Name."<br/>
          <hr>
          <hr>
          carte mere
          <hr>
          <hr>
          SerialNumber : ".$SerialNumber."<br/>
           <hr>
          <hr>
          disk
          <hr>
          <hr>
          SerialNumber : ".$SerialNumberDisk."<br/>
          FileSystem : ".$FileSystem."<br>
          ";

?>
望笑 2024-10-03 01:16:16
<?php
    $strComputer = "YOURREMOTEHOST";
    $objSWbemLocator = new COM ("WbemScripting.SWbemLocator");
    $objSWbemServices = $objSWbemLocator->ConnectServer($strComputer, "root\cimv2", "DOMAIN\USER", "Password");
    $objSWbemServices->Security_->ImpersonationLevel = 3;
    $obj = $objSWbemServices;
    $fso = new COM ( "Scripting.FileSystemObject" );

    //... insert your code here

    //... insert your code here
?>
<?php
    $strComputer = "YOURREMOTEHOST";
    $objSWbemLocator = new COM ("WbemScripting.SWbemLocator");
    $objSWbemServices = $objSWbemLocator->ConnectServer($strComputer, "root\cimv2", "DOMAIN\USER", "Password");
    $objSWbemServices->Security_->ImpersonationLevel = 3;
    $obj = $objSWbemServices;
    $fso = new COM ( "Scripting.FileSystemObject" );

    //... insert your code here

    //... insert your code here
?>
爱人如己 2024-10-03 01:16:16

连接

对于全局管理员

define("NAMECOMP", 'COMP1'); // COMP1 - name or ip of local or remote computer 
$WMI= new COM ( 'winmgmts:{impersonationLevel=impersonate}//'. NAMECOMP.'/root/cimv2' ); 

对于使用登录名和密码

$objLocator = new COM("WbemScripting.SWbemLocator");
$objService = $objLocator->ConnectServer(
                'ComputerName', //name/ip remote/local comp
                "root\cimv2",
                'login', //login remote/local comp
                'password', //password remote/local comp
                "MS_409",
                "ntlmdomain: YourDomain" //domain remote/local comp
            );

获取信息

$CountCore=0;
foreach ($WMI->instancesof ( 'Win32_Processor' ) as $proc ) {
  ++$CountCore;
}
echo 'Count Core = ' . $CountCore; 

添加速度和套接字处理器的信息

$CountCore=0;
foreach ($WMI->instancesof ( 'Win32_Processor' ) as $Processor) {
  ++$CountCore;
  $Speed=$Processor->CurrentClockSpeed;
  $Socket=$Processor->SocketDesignation;
}
echo 'count core = '.$CountCore;
echo 'speed = ' . $Speed. 'Mhz';
echo 'socket = '.$Socket; 

获取其他信息简单 - 只需替换instancesof('Win32_Processor')的类

WMI 类信息

发送命令

if ((($_GET['Reboot']==1) OR ($_GET['Shutdown']==1))) { 
 
  define("NAMECOMP", 'COMP1');
          
  $WMI= new COM('winmgmts:{impersonationLevel=impersonate,(Shutdown)}//'. NAMECOMP.'/root/cimv2'); 
  
  foreach($WMI->instancesof('Win32_OperatingSystem') as $mp)  {
      if ($_GET['Reboot']==1) {
          $mp->Reboot; 
      }
      if ($_GET['Shutdown']==1) {
          $mp->Shutdown; 
      }
}

链接:

WMI 不工作!

Component_Object_Model

Win32 类

对于 Yii 框架

CONNECTING

For global admins

define("NAMECOMP", 'COMP1'); // COMP1 - name or ip of local or remote computer 
$WMI= new COM ( 'winmgmts:{impersonationLevel=impersonate}//'. NAMECOMP.'/root/cimv2' ); 

For with login and password

$objLocator = new COM("WbemScripting.SWbemLocator");
$objService = $objLocator->ConnectServer(
                'ComputerName', //name/ip remote/local comp
                "root\cimv2",
                'login', //login remote/local comp
                'password', //password remote/local comp
                "MS_409",
                "ntlmdomain: YourDomain" //domain remote/local comp
            );

GET INFORMATION

$CountCore=0;
foreach ($WMI->instancesof ( 'Win32_Processor' ) as $proc ) {
  ++$CountCore;
}
echo 'Count Core = ' . $CountCore; 

add information of speed and socket processor

$CountCore=0;
foreach ($WMI->instancesof ( 'Win32_Processor' ) as $Processor) {
  ++$CountCore;
  $Speed=$Processor->CurrentClockSpeed;
  $Socket=$Processor->SocketDesignation;
}
echo 'count core = '.$CountCore;
echo 'speed = ' . $Speed. 'Mhz';
echo 'socket = '.$Socket; 

get other information simple - just replace class for instancesof ('Win32_Processor')

Information of classes WMI

SEND COMMAND

if ((($_GET['Reboot']==1) OR ($_GET['Shutdown']==1))) { 
 
  define("NAMECOMP", 'COMP1');
          
  $WMI= new COM('winmgmts:{impersonationLevel=impersonate,(Shutdown)}//'. NAMECOMP.'/root/cimv2'); 
  
  foreach($WMI->instancesof('Win32_OperatingSystem') as $mp)  {
      if ($_GET['Reboot']==1) {
          $mp->Reboot; 
      }
      if ($_GET['Shutdown']==1) {
          $mp->Shutdown; 
      }
}

Links:

WMI Isn't Working!

Component_Object_Model

Win32 Classes

For Yii Framework

赢得她心 2024-10-03 01:16:16

我知道这个帖子不是最新的,但也许这个信息可以帮助某人。
如果您尝试在远程计算机上读取或写入注册表项或已安装的软件类,则需要在应该运行的查询下传递体系结构。你可以使用这样的东西。

function Connect($server = "RemotePC",$namespace = "root/CIMV2",$impersonate = 3,$Architecture = 64,$userid = null,$password = null){
        try {
            $wbemnvs = new COM("WbemScripting.SWbemNamedValueSet");
            $wbemnvs->add("__ProviderArchitecture", $Architecture);
            $wbemnvs->add("__RequiredArchitecture", true);
            $wmiLocator = new COM("WbemScripting.SWbemLocator");
            $this->wmiNameSpace = $wmiLocator->ConnectServer($server, $namespace, $userid, $password,null,null,128,$wbemnvs);
            if($this->wmiNameSpace){
                $this->ConnectedServer = $server;
            }else{ return false; }
            if($impersonate){
                // mehr infos: http://msdn.microsoft.com/en-us/library/aa393618%28v=vs.85%29.aspx
                $this->wmiNameSpace->Security_->ImpersonationLevel = $impersonate;
            }
            return true;
        }
        catch(Exception $e){
            $this->wmiNameSpace = NULL;
            return false;
        }
    }

查看我的 wmi.class.php: http://scbbb.blogspot .de/2014/02/wmi-via-php.html

I know this thread is not the newest but maby this info helps someone.
If you try to read or write Registy keys or installed Software class on a Remote Computer you need to pass the Architecture under the query should run. You can use something like this.

function Connect($server = "RemotePC",$namespace = "root/CIMV2",$impersonate = 3,$Architecture = 64,$userid = null,$password = null){
        try {
            $wbemnvs = new COM("WbemScripting.SWbemNamedValueSet");
            $wbemnvs->add("__ProviderArchitecture", $Architecture);
            $wbemnvs->add("__RequiredArchitecture", true);
            $wmiLocator = new COM("WbemScripting.SWbemLocator");
            $this->wmiNameSpace = $wmiLocator->ConnectServer($server, $namespace, $userid, $password,null,null,128,$wbemnvs);
            if($this->wmiNameSpace){
                $this->ConnectedServer = $server;
            }else{ return false; }
            if($impersonate){
                // mehr infos: http://msdn.microsoft.com/en-us/library/aa393618%28v=vs.85%29.aspx
                $this->wmiNameSpace->Security_->ImpersonationLevel = $impersonate;
            }
            return true;
        }
        catch(Exception $e){
            $this->wmiNameSpace = NULL;
            return false;
        }
    }

check out my wmi.class.php at: http://scbbb.blogspot.de/2014/02/wmi-via-php.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文