Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试同时执行文件操作_C#_Powershell_Sql Server 2008 R2_Asp.net 4.0_Powershell 2.0 - Fatal编程技术网

C# 正在尝试同时执行文件操作

C# 正在尝试同时执行文件操作,c#,powershell,sql-server-2008-r2,asp.net-4.0,powershell-2.0,C#,Powershell,Sql Server 2008 R2,Asp.net 4.0,Powershell 2.0,我正在尝试创建一个ASP.NET 4.0网站(C#),该网站可执行以下操作:在用户单击按钮后,获取用户输入(从文本框中)并在服务器上调用Powershell脚本。Powershell脚本输出记录到同一台计算机上的文本文件中。该脚本在SQL server上执行一些存储过程。脚本执行完毕后,页面将读取日志文件的内容并在页面中返回这些内容。我之所以这样记录是因为我需要捕获这些操作的“详细”输出 我遇到的问题是,如果另一个用户打开了站点并且正在尝试相同的操作,那么在用户单击按钮后运行的整个命令集将排队。

我正在尝试创建一个ASP.NET 4.0网站(C#),该网站可执行以下操作:在用户单击按钮后,获取用户输入(从文本框中)并在服务器上调用Powershell脚本。Powershell脚本输出记录到同一台计算机上的文本文件中。该脚本在SQL server上执行一些存储过程。脚本执行完毕后,页面将读取日志文件的内容并在页面中返回这些内容。我之所以这样记录是因为我需要捕获这些操作的“详细”输出

我遇到的问题是,如果另一个用户打开了站点并且正在尝试相同的操作,那么在用户单击按钮后运行的整个命令集将排队。我不清楚为什么会发生这种情况。在网站之外运行时,Powershell脚本将并行执行。应用程序写入的日志文件使用时间戳和用户输入的信息,以确保它们根据输入信息的人而不同

以下是单击按钮时执行的代码:

protected void Test_Click(object sender, EventArgs e)
   {
        // Get the current date/time to use a timestamp for the log file
        DateTime curDate = DateTime.Now;
        string logFileName = "D:\\test1\\logs\\" + curDate.Year + curDate.Month + curDate.Day +
            curDate.Hour + curDate.Minute + curDate.Second + curDate.Millisecond + "_" +
            TextBox1.Text.Replace("\\", "_") + "_Test.txt";

        // Clean the Result TextBox
        ResultBox.Text = string.Empty;

        // Initialize PowerShell engine
        var shell = PowerShell.Create();

        // Add a script to the PowerShell shell object that will execute the instructions with the given input
        shell.Commands.AddScript("powershell.exe -file D:\\test1\\ps_script0.ps1 '" + TextBox3.Text + "' '" +
            TextBox1.Text + "' '" + TextBox2.Text + "' '" + TextBox4.Text + "' > " + logFileName);

        // Execute the script
        shell.Invoke();

        // Create a StreamReader object to read the results of the script instructions and then store the file's contents in a string
        StreamReader sr = File.OpenText(logFileName);
        String results = sr.ReadToEnd();
        results = results.Replace("VERBOSE: ", "");
        sr.Close();

        // Set the text of the result box to the string that was read in
        ResultBox.Text = results;
    }

我希望了解是否有关于允许多个用户单击此按钮并同时为每个用户运行后端脚本的信息。我研究了异步页面和线程,但我认为在我的例子中没有必要这样做,因为每个用户的页面都应该有自己的线程。非常感谢您的帮助。谢谢

我也想知道这个问题的答案。我的一个ASP.NET页面上有一个类似的东西,但它太小,用户太少,我从来没有注意到,但现在我想知道我的页面是否也没有同时运行。您可能想重新评估您正在尝试执行的操作。让网页执行ASP.NET进程中的脚本可能非常危险。在我的情况下,这是用于员工之间使用的intranet站点。具体地说,它与上面的一样,只是脚本本身是查询ESX服务器以找出哪些虚拟机位于哪些IP地址所必需的。如果有另一种方法可以通过内联网在网上实现,我洗耳恭听。数据库中可能有阻塞吗?运行正在阻止的sp_,查看blk列中是否有任何非0值。@CptSupermrkt编写一个应用程序,定期轮询要通过web公开的数据并将其存储(在数据库或XML文件中,甚至作为WCF服务存储在内存中),可能会更有意义,然后让你的网页查询这些数据。