0

shell_execを使用してphpを使用して実行するpowershellスクリプトがあり、Ajax呼び出しがあるときにこのファイルを実行します。(私は codeigniter 2 環境を使用しています)

ps ファイルを実行するのに約 45 秒かかるため、Ajax で呼び出します。Ajax 経由で実行すると思ったので、ロード中であるというメッセージをユーザーに伝えることができます。

私の Ajax 関数は次のようになります。

<script>
    $(document).ready(function () {
        $.fn.colorbox({href:"#inline_content", inline:true, width:"650px", height:"235px", overlayClose: false, showClose: false});
        $('#cboxClose').remove();

        var csrf = $('[name="csrf_test_name"]').val();

        $.ajax({
            type: "POST",
            url: "/index.php/ajax/addAccount",
            data: { 
                    csrf_test_name: csrf
                  },
            cache:false,
            success: 
              function(data){
                  if(data == "True"){
                        window.location = "/index.php/private";  
                  }else{
                      $(window).colorbox.close();
                      alert("There was a problem creating your account.");
                  }
              },
            error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                    $('#Loading').hide();
                  }

        }); 


    });
</script>

CI Ajax コントローラーは、データを取得してモデルにフィードするだけです。

シェルの exec 関数は次のようになります。

function createCompany($companyID, $companyName, $domain){

    $psCompanyName = "t_".$companyID. "_".str_replace(" ", "", $companyName);

    $psScriptPath = "c:\\inetpub\\powershellscripts\\createCompany.ps1";
    $ps = shell_exec("powershell -command $psScriptPath -companyName '".$psCompanyName."' -domain '".$domain."'");
    return $ps;

}

PS1 ファイルは正常に実行されます。$ps には値 "True" または "False" が含まれている必要があります (ps1 の最後に write-output "True" または write-output "False" があり、チェックしたところ、すべてのタスクが正常に実行されました。

return $ps を return "True" または return "False" に置き換えると、成功: セクションは正常に動作し (エラーからも何も得られません: どちらも)、Ajax の外部で関数を実行すると、結果。

これは、Ajax がタイムアウトしたためでしょうか?

4

1 に答える 1

1

Fixed. It was executing the script but I needed to change the shell exec code to this:

    $ps = shell_exec("powershell -command $psScriptPath -companyName '".$psCompanyName."' -domain '".$domain."' < Nul");

Not sure what the < Nul bit does, but it was executing the powershell but caused error 500

于 2012-10-11T09:37:58.560 に答える