Solved

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

Posted on 2011-09-28
6
878 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
  • 3
  • 2
6 Comments
 
LVL 109

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

785 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