简体   繁体   中英

Why does the Php exec function, returns Array( )?

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.

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

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: http://us2.php.net/manual/en/features.safe-mode.functions.php

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM