Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
Visual studio Visual Studio-生成后事件-引发错误_Visual Studio_Powershell_Post Build Event - Fatal编程技术网

Visual studio Visual Studio-生成后事件-引发错误

Visual studio Visual Studio-生成后事件-引发错误,visual-studio,powershell,post-build-event,Visual Studio,Powershell,Post Build Event,我有一个很好的powershell驱动的后期构建脚本,它在编译所有插件并与解决方案的主项目一起整理它们之间的依赖关系之后,为将它们拉到正确的位置提供了一些神奇的混乱 我的问题是,有时当我做了一些愚蠢的事情时,我可能会陷入脚本无法正确执行其操作的状态,并在powershell中抛出异常,详细说明出错的原因 有没有办法让这些异常拉到Visual Studio错误窗口,以便在后期生成失败时,vstudio会收到一个失败通知,并在窗口中将其与所有其他警告/错误一起很好地格式化 编辑:这是对我理想中所寻找

我有一个很好的powershell驱动的后期构建脚本,它在编译所有插件并与解决方案的主项目一起整理它们之间的依赖关系之后,为将它们拉到正确的位置提供了一些神奇的混乱

我的问题是,有时当我做了一些愚蠢的事情时,我可能会陷入脚本无法正确执行其操作的状态,并在powershell中抛出异常,详细说明出错的原因

有没有办法让这些异常拉到Visual Studio错误窗口,以便在后期生成失败时,vstudio会收到一个失败通知,并在窗口中将其与所有其他警告/错误一起很好地格式化

编辑:这是对我理想中所寻找内容的澄清。

拆下固定的影像棚链接


注意:如果您登陆这里寻找如何在Visual Studio中显示自定义错误消息(当您可以控制错误消息时),您可以查看以了解如何定制消息以显示。如果您有兴趣捕获无法控制的意外错误,或找到更完整的解决方案,请参阅公认的答案。

以下是两种在PowerShell脚本出错时中断生成的方法

使用
exit()
终止PowerShell进程 要从脚本返回状态代码(如果非零,将显示在错误列表中),请使用以下命令:

exit(45) # or any other non-zero number, for that matter.
这并不能准确地将错误文本放到错误列表中,但它会终止脚本,并在错误列表中获取一些信息,以指示哪个编译前或编译后命令失败

使用自定义MSBuild任务执行PowerShell脚本 我花了一点时间研究在MSBuild任务中执行PowerShell脚本的细节。我有。一定要查看它,因为它包括更多的讨论,以及一些关于如何处理示例项目的解释。这可能不是一个完整或完美的解决方案,但我用一个非常简单的脚本在我的MachineTM上实现了它

这种方法在报告PowerShell错误时提供了行和列精度,甚至支持双击将我带到我们在Visual Studio中习惯的文件行为。如果缺少,我相信您可以扩展它以满足您的需要。此外,根据您的Visual Studio版本,您可能需要处理程序集引用版本等详细信息

首先,在类库项目中生成自定义MSBuild任务。库应为MSBuild和PowerShell集成引用以下程序集。(请注意,此示例需要PowerShell 2.0。)

  • Microsoft.Build.Framework(GAC)
  • Microsoft.Build.Utilities.v3.5(GAC)
  • System.Management.Automation(来自C:\Program Files\Reference Assembly\Microsoft\WindowsPowerShell\v1.0)
构建任务类,并公开属性以指定PowerShell脚本的路径,如下所示:

using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class PsBuildTask : Task
{
    [Required]
    public string ScriptPath { get; set; }

    public override bool Execute()
    {
        // ...
    }
}
Execute()
方法中,启动PowerShell运行时,执行脚本并收集错误。使用
Log
属性记录错误。完成后,关闭运行空间,如果脚本未记录任何错误,则返回true

// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(". " + ScriptPath);

// execute the script and extract errors
pipeline.Invoke();
var errors = pipeline.Error;

// log an MSBuild error for each error.
foreach (PSObject error in errors.Read(errors.Count))
{
    var invocationInfo = ((ErrorRecord)(error.BaseObject)).InvocationInfo;
    Log.LogError(
        "Script",
        string.Empty,
        string.Empty,
        new FileInfo(ScriptPath).FullName,
        invocationInfo.ScriptLineNumber,
        invocationInfo.OffsetInLine,
        0,
        0,
        error.ToString());
}

// close the runspace
runspace.Close();

return !Log.HasLoggedErrors;
就这样。有了这个程序集,我们可以配置另一个项目来使用MSBuild任务

例如,考虑一个基于C#的类库项目(.csproj)。在生成后事件中集成任务只需要几件事

首先,在.csproj文件的
节点内注册任务,如下所示:

<UsingTask TaskName="PsBuildTask"
           AssemblyFile="..\Noc.PsBuild\bin\Debug\Noc.PsBuild.dll" />
<Target Name="AfterBuild">
    <PsBuildTask ScriptPath=".\script.ps1" />
</Target>
就这样。Visual Studio编译项目时,将加载自定义程序集和任务对象并执行任务。检索并报告管道引发的错误


要创建将出现在错误列表窗口中的错误、警告或消息,只需从生成前或生成后事件启动的脚本中以以下格式将消息记录到stdout或stderr。如果有官方规范,请评论或编辑;这是我通过反复试验和观察MSBuild的输出所能推断出的。方括号表示“可选”:

例如:

E:\Projects\SampleProject\Program.cs(18,5): error CS1519: Invalid token '}' in class, struct, or interface member declaration

有关更多示例,请参阅“输出”窗口,了解在Visual Studio中运行生成时可能出现的任何错误/警告/消息。

我已经让它显示生成输出的信息。我需要的是编译操作在出现错误时失败。如果可能的话,有关于失败原因的信息最终会出现在错误中List@Aren-谢谢你的回复。我已经用一些东西更新了这篇文章,这些东西应该有助于达到预期效果。@kbrimington-不知道如何让消息显示出来,是吗?@Aren-在生成宏或自定义MSBuild任务中运行PowerShell运行时,您可能会获得一些好处,但我不确定如何建议您开始。对于初学者,请尝试以下资源:对于宏-,对于MSBuild任务-。祝你好运从技术上讲,
exit
不是别名,而是关键字;但是,效果与退出PSSession相同。这非常有用。谢谢你花时间来解决这个问题。只是让我的后期构建步骤变得更好了,从(可能现在已删除)到。
E:\Projects\SampleProject\Program.cs(18,5): error CS1519: Invalid token '}' in class, struct, or interface member declaration