Solved

Trying to use username and password for WMI look up in PHP code

Posted on 2011-09-28
6
897 Views
Last Modified: 2012-05-12
I am trying to add a username and password to my WMI lookup in my PHP page, can anyone help me with this? Thanks
<?php
			$displayasset = $row['asset'];
			$user = "username";
			$pass = "password";
			
    //$obj = new COM ( 'winmgmts://'. $displayasset .'/root/CIMV2' );
	$wmiLocator = new COM("WbemScripting.SWbemLocator");
	$obj = $wmiLocator->ConnectServer($displayasset, "root\cimv2", "$displayasset\$user", $pass);
	
   // $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 WHERE DeviceID='C:'");
    $software			   =	$obj->ExecQuery("SELECT * From Win32_Product");
	
    foreach ( $wmi_computersystem as $wmi_call )
    {
        $model = $wmi_call->Model;
		$username = $wmi_call->UserName;
    }

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

    foreach ( $processor as $wmi_processor )
    {
        $procname = $wmi_processor->Name;
    }
    foreach ( $PhysicalMemory as $wmi_PhysicalMemory )
    {
        $Capacity = $wmi_PhysicalMemory->Capacity;
		$Capacity = $Capacity /1024/1024+1 . " MB";;
    }

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

    }
    foreach ( $LogicalDisk as $wmi_LogicalDisk )
    {
        $size = $wmi_LogicalDisk->Size;
		$size = $size /1024/1024/1024+1 . " GB";
        $freespace = $wmi_LogicalDisk->FreeSpace;
		$freespace = $freespace /1024/1024/1024+1 . " GB";

    }


    echo "Bios version   : ".$bios_version."<br/>
          Serial number of bios  : ".$serial."<br/>
          Hardware Model : ".$model."<br/>
		  Username : ".$username."<br/>
          Processor : ".$procname."<br/>
          Total Ram : ".$Capacity."<br/>
          Total Disk : ".$size."<br>
		  Free Space : ".$freespace."<br>
		  <br>
		  <b>Software:</b>
		  <br>
          ";
	foreach ( $software as $wmi_software )
	{
		$softwarelist = $wmi_software->Name;
		echo $softwarelist . "<br>";
	}
?>

Open in new window

0
Comment
Question by:maximus81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36718769
Do you mean that you want to add an authentication step to password protect your script?
0
 

Author Comment

by:maximus81
ID: 36718778
The issue I am having is when I search for a computer the script runs and it comes back as access denied.
0
 

Accepted Solution

by:
maximus81 earned 0 total points
ID: 36718860
I figured it out:

                  $host = $row['asset'];
                  $user = "domain\administrator";
                  $pass = "pass";
                  
      $wmiLocator = new COM('WbemScripting.SWbemLocator');
      $obj = $wmiLocator->ConnectServer($host, 'root\cimv2', $user, $pass);
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Closing Comment

by:maximus81
ID: 36902265
I figured it out.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36718878
Try this:

   $displayasset = $row['asset'];
   $user = "username";
   $pass = "password";

   $wmiLocator = new COM('WbemScripting.winmgmts:{impersonationLevel=impersonate}');
   $obj = $wmiLocator->ConnectServer($displayasset, "root\cimv2", "$displayasset\$user", $pass);
//or
   $obj = $wmiLocator->ConnectServer($displayasset, "root\cimv2", $user, $pass);

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36718880
oh, too late.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question