批处理脚本任务因神秘输入而被取消
Batch Script task getting cancelled because of mysterious input
我在我的 Azure DevOps 管道中定义了一个批处理脚本任务。我可以从日志中看到它启动并且 运行s 是 bat 文件中的前几个命令,但突然间我在日志中看到“已取消”并且任务作为成功任务完成。
我的 bat 文件 运行 是一个自定义可执行文件,如果它检测到来自控制台的任何输入,则允许中途取消。这就是蝙蝠 运行 在管道旁时发生的情况。
为什么在 bat 仍在执行时管道会向控制台输入输入 window,我如何让它停止?
批处理脚本任务输出:
按回车键停止
运行 可执行文件
已取消
可执行文件完成,错误代码:-1
此时,我什至不确定 AzureDevOps 是否真的按照我认为的方式将输入输入控制台。不过,我最终发现的是,当控制台应用程序被 AzureDevOps 运行 时,它会重定向所有输入,这会阻止控制台应用程序捕获方向输入。我最终使用这段代码跳过了当输入为 re-directed.
时寻找“退出”运算符的逻辑
if (Environment.UserInteractive && !Console.IsInputRedirected)
{
// if don't test for UserInteractive, will run into issues when run by DevOps pipeline.
// DevOps pipeline causes IsInputRedirected to be true, which results in this exception.
// Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
Console.WriteLine("Press Enter to stop");
while (true)
{
var input = Console.ReadKey();
if (input.Key == ConsoleKey.Enter)
{
// quit once enter is entered
return;
}
}
}
else
{
// if console does not allow user input, just wait until the task is cancelled
ct.WaitHandle.WaitOne();
}
我在我的 Azure DevOps 管道中定义了一个批处理脚本任务。我可以从日志中看到它启动并且 运行s 是 bat 文件中的前几个命令,但突然间我在日志中看到“已取消”并且任务作为成功任务完成。
我的 bat 文件 运行 是一个自定义可执行文件,如果它检测到来自控制台的任何输入,则允许中途取消。这就是蝙蝠 运行 在管道旁时发生的情况。
为什么在 bat 仍在执行时管道会向控制台输入输入 window,我如何让它停止?
批处理脚本任务输出:
按回车键停止
运行 可执行文件
已取消
可执行文件完成,错误代码:-1
此时,我什至不确定 AzureDevOps 是否真的按照我认为的方式将输入输入控制台。不过,我最终发现的是,当控制台应用程序被 AzureDevOps 运行 时,它会重定向所有输入,这会阻止控制台应用程序捕获方向输入。我最终使用这段代码跳过了当输入为 re-directed.
时寻找“退出”运算符的逻辑 if (Environment.UserInteractive && !Console.IsInputRedirected)
{
// if don't test for UserInteractive, will run into issues when run by DevOps pipeline.
// DevOps pipeline causes IsInputRedirected to be true, which results in this exception.
// Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
Console.WriteLine("Press Enter to stop");
while (true)
{
var input = Console.ReadKey();
if (input.Key == ConsoleKey.Enter)
{
// quit once enter is entered
return;
}
}
}
else
{
// if console does not allow user input, just wait until the task is cancelled
ct.WaitHandle.WaitOne();
}