如何将 pubxml 文件中的 powershell 错误重定向到错误 window

How to redirect powershell errors in pubxml file to error window

我有一个自定义 pubxml 文件,其中包含一个执行 powershell 脚本的目标。在部署我的站点之前,此 powershell 命令已成功运行。

如果 powershell 脚本中出现错误,输出将放在输出 window 中,但构建会继续,最终消息将是 Publish: 1 succeeded。如果我的 powershell 脚本出错,我该如何停止发布?

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
    <PipelineDependsOn>
        CopyConfigFiles;
        $(PipelineDependsOn);
    </PipelineDependsOn>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl></publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
</PropertyGroup>
<Target Name="CopyConfigFiles">
    <Message Text="********************************** Deploying Config Files ***********************************" Importance="high"/>
    <Exec Command="powershell.exe -ExecutionPolicy ByPass -File &quot;$(SolutionDir)/Config/CopyConfigFiles.ps1&quot; -ConfigSource &quot;$(SolutionDir)/Config/&quot;"/>
</Target>

很明显!

如果您的 powershell 脚本 return 有退出代码,该错误将阻止发布。只需捕获您的 powershell 异常和 return 退出命令:

Try
{
    # Script goes here
}
Catch
{
   # Custom output or choice of error code number goes here
   exit 1
}