如何使用php获取主硬盘的序列号

时间:2018-10-23 12:51:13

标签: php windows

我正在使用:

$serial =  shell_exec('wmic DISKDRIVE GET SerialNumber 2>&1');

但是有了这个,我得到了所有硬盘的序列号,我只想要主硬盘的序列号,在我的情况下是“ c”。 我尝试过:

wmic path win32_diskdrive where deviceid="\\\\.\\PHYSICALDRIVE0" get serialnumber

它在Windows控制台中工作正常,但我尝试将其与exec一起使用,如下所示:

$serial =  exec('wmic path win32_diskdrive where deviceid="\\\\.\\PHYSICALDRIVE0" get serialnumber');

但是我只得到“”

1 个答案:

答案 0 :(得分:2)

exec仅返回输出的最后一行。给它提供输出参数,然后在每行中填充它,或者使用shell_exec代替。同时将您的backslashes加倍,以使它们正确逃脱。例如:

exec('wmic path win32_diskdrive where deviceid="\\\\\\\\.\\\\PHYSICALDRIVE0" get serialnumber', $out);
var_dump($out);

$serial = shell_exec('wmic path win32_diskdrive where deviceid="\\\\\\\\.\\\\PHYSICALDRIVE0" get serialnumber');
相关问题