0

I am working on a php script that tries to get the username of the person on a given computer on a server.

Here's the code im using
exec('wmic COMPUTERSYSTEM Get UserName', $user);
print_r($user);

And when ever this runs i always get back Array( )

Any help would be greatly appreciated.

3 Answers 3

0

You cannot use exec to return any output from your command.

See this answer.But you can use shell_exec() to return the output of your command.

You can have an array of computer:

$computer = array('comp1', 'comp2');
$comp1 =  array();

foreach($computer as $username)
{
    $comp1 = shell_exec("wmic /node: ". $username ." computersystem Get UserName");
    print_r($comp1);
}

But if there is only one:

$user = shell_exec('wmic /node: <yourcomputername> COMPUTERSYSTEM Get UserName');
print_r($user);

But i suggest to get logged users, there is a tool from sysinternals that i used to do certain task like executing process remotely, list info about the system through command prompt.

Checkout and install PSTools. Then you can use the PsLoggedon.exe

Sign up to request clarification or add additional context in comments.

6 Comments

So how should i be printing this? should i just echo the shell_exec? or is there a certain way i should do it?
do you have an array of $user that you want to automate?
can you elaborate what you mean by automate?
are you going to run the command with one user only or you have a list of user stored in an array and want to get their usernames?
I have a list of different computers, and im checking to see if there is ANYONE logged onto it. and i believe only one person can be logged on at a time.
|
0

exec always return a array, if the output argument is present

See: http://www.php.net/manual/en/function.exec.php

If your code return only one user, try this:

$user = shell_exec('wmic COMPUTERSYSTEM Get UserName');
echo $user;

shell_exec manual:

http://www.php.net/manual/en/function.shell-exec.php

It may be disabled if PHP is in safe mode.

Look: https://www.php.net/manual/en/features.safe-mode.functions.php

You can check your server's PHP settings with the phpinfo() function.

3 Comments

hmm when i tried it, i didnt get any output, ill keep trying :)
Should i put the full path for this command in the ()'s? if so what would that look like?
just checked and it says safe_mode is off.
0

You have to specify the array this way: print_r($user[1]);

exec('wmic COMPUTERSYSTEM Get UserName', $user);
print_r($user[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.