尝试运行以下PHP脚本时在Windows 7上出错

时间:2010-12-23 09:12:53

标签: php windows-7 wsh

if (strlen($log) > 0)
{
    // Use "WScript.Shell" to run the command with no command prompt window pop up.
    $wShell = new COM("WScript.Shell"); 
    $cmd = "cmd /c cscript.exe \"%DIR%\\bin\\eventquery.vbs\" /l \"" . $log . "\" > \"%DIR%\\Temp\\event.log\" 2>&1";
    ////echo $cmd;
    $return = $wShell->Run($cmd, 0, true);
    if ($return == 0 || $return ==254)
    {
        $handle = @fopen(getenv('DIR') . "\\Temp\\event.log", "r");
        if ($handle)
        {
            $linenum = 1;
            while (!feof($handle))
            {
                $buffer = fgets($handle);
                // Skip the first three lines
                if ($linenum > 3)
                {
                    echo $buffer;
                }

                $linenum++;
            }

            fclose($handle);
        }
    }
    else
    {
        echo "Error running \"" . $cmd . "\" command."; 
    }
}

它在Windows 7上出错:

  

错误:无法包含公共模块“CmdLib.Wsc”

当我尝试从命令行@ windows 7运行它时,它运行正常:

cscript.exe \"%DIR%\\bin\\eventquery.vbs\" /l \"" . $log . "\" > \"%DIR%\\Temp\\event.log\" 2>&1

2 个答案:

答案 0 :(得分:2)

尝试使用escapeshellarg

$cmd = "cmd /c cscript.exe \"%DIR%\\bin\\eventquery.vbs\" /l \"" . escapeshellarg($log) . "\" > \"%DIR%\\Temp\\event.log\" 2>&1";

我的猜测是你在$log中有shell元字符。“

答案 1 :(得分:0)

$a = 'all';
file_put_contents('ip.txt',shell_exec($dump = sprintf('ipconfig /%s',$a)));