用PHP远程查询WMI

用PHP远程查询WMI,php,windows,wmi,wmi-query,Php,Windows,Wmi,Wmi Query,我当前的代码如下所示: 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" ); } 我将把远

我当前的代码如下所示:

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" );
}
我将把远程_服务器的登录凭据放在哪里? 我知道这需要用户名和密码,但我不知道如何实现

任何帮助都将不胜感激

参考资料:



我知道这条线索不是最新的,但也许这些信息可以帮助别人。 如果您试图读取或写入远程计算机上的注册表项或已安装的软件类,则需要通过查询下的架构。你可以用这样的东西

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,网址为:

CONNECTING 适用于全球管理员

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 $Processor) {
  ++$CountCore;
  $Speed=$Processor->CurrentClockSpeed;
  $Socket=$Processor->SocketDesignation;
}
echo 'count core = '.$CountCore;
echo 'speed = ' . $Speed. 'Mhz';
echo 'socket = '.$Socket; 
获取其他简单信息-只需替换instancesof('Win32_Processor')的类

发送命令 链接:


您所说的“我知道用户名和密码需要在哪里,但我不知道如何实现它”是什么意思?如果你能看到密码会放在哪里-你怎么能不知道把它放在哪里?我看到它接受一个密码,但不知道它放在哪里。非常感谢您的帮助。您确实在php.ini中启用了DCOM,对吗?
$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; 
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; 
      }
}