来自C#不';不完整

来自C#不';不完整,c#,powershell,process,C#,Powershell,Process,我正在尝试从C#运行PS脚本,并将Powershell作为一个进程,如下所示: ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = configFile.AppSettings.Settings["PSDir"].Value.ToString() + @"\powershell.exe"; startInfo.Arguments = @"& '" + sc

我正在尝试从C#运行PS脚本,并将Powershell作为一个进程,如下所示:

 ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName =   configFile.AppSettings.Settings["PSDir"].Value.ToString() + @"\powershell.exe";
        startInfo.Arguments = @"& '" + scriptPath + "'\"";
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;

        Process process = new Process();
        process.StartInfo = startInfo;            
        process.Start();

        return process;
        Process proc = RunPSScript(startupPath + "\\stats.ps1");
        proc.WaitForExit();
我这样称呼这个方法:

 ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName =   configFile.AppSettings.Settings["PSDir"].Value.ToString() + @"\powershell.exe";
        startInfo.Arguments = @"& '" + scriptPath + "'\"";
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;

        Process process = new Process();
        process.StartInfo = startInfo;            
        process.Start();

        return process;
        Process proc = RunPSScript(startupPath + "\\stats.ps1");
        proc.WaitForExit();
程序被困在以下位置:

proc.WaitForExit();
我可以说脚本的初始部分已经完成,但除此之外什么都没有。不是脚本不好,因为我在代码之外运行了它,它运行得非常好

当我删除WaitForExit()时,进程调用立即存在,程序继续运行,但这并不好,因为我需要让脚本生成数据,然后程序的其余部分才能继续运行

奇怪的是,如果我在启动PS调用后结束调试会话,脚本将继续运行,而锁定其完成的内容现在将消失,并继续运行到完成

任何想法都值得赞赏


谢谢

2件需要检查的事情,您的参数的确切输出是什么?在给定的条件下,以admin的身份从CMD提示符运行相同的脚本,并查看是否有相同的结果。您还可以以毫秒为单位将超时传递给等待退出。试试这个:

RunPSScript(startupPath + "\\stats.ps1").WaitForExit();

要检查的两件事,你的论点的确切输出是什么?在给定的条件下,以admin的身份从CMD提示符运行相同的脚本,并查看是否有相同的结果。您还可以以毫秒为单位将超时传递给等待退出。试试这个:

RunPSScript(startupPath + "\\stats.ps1").WaitForExit();

在进程退出之前,您可能需要读取进程的输出流。要添加到Mark W的注释中,请执行以下操作:。。。绝对是一个dupe.aarrgghh,问题是这行代码:startInfo.Arguments=@“&”+scriptPath+“\”;需要删除\“,现在它可以工作了。我真傻!在进程退出之前,您可能需要读取进程的输出流。要添加到Mark W的注释中,请执行以下操作:。。。绝对是一个dupe.aarrgghh,问题是这行代码:startInfo.Arguments=@“&”+scriptPath+“\”;需要删除\“,现在它可以工作了。我真傻!看我上面的评论,我在争论中有一个坏字符。但是谢谢!看我上面的评论,我在争论中有一个坏字符。但是谢谢!