从C#

时间:2019-01-22 09:22:18

标签: c# powershell

当我尝试从C#代码下面运行PowerShell脚本时遇到以下错误。

错误: &:无法加载文件E:\ TestRepo \ build.ps1,因为此系统上禁用了运行脚本。 有关更多信息,请参阅https://go.microsoft.com/fwlink/?LinkID=135170上的about_Execution_Policies。 在第1行:3个字符 +&'E:\ TestRepo \ build.ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo:SecurityError:(:) [],PSSecurityException     + FullyQualifiedErrorId:UnauthorizedAccess

 public static void RunPowershellScript()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"powershell.exe";
        //provide powershell script full path
        startInfo.Arguments = @"& 'E:\\TestRepo\build.ps1'";
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        Process process = new Process();
        process.StartInfo = startInfo;
        // execute script call start process
        process.Start();
        // get output information
        string output = process.StandardOutput.ReadToEnd();

        // catch error information
        string errors = process.StandardError.ReadToEnd();
    }

0 个答案:

没有答案