【发布时间】:2019-02-25 05:39:52
【问题描述】:
我有一个 .NET Framework 4.5.2 WebAPI2 项目。在 Visual Studio 中构建项目时,我希望在每次构建之前运行我编写的 powershell 脚本。如果 powershell 脚本以任何非零代码退出,我希望构建失败并且来自 powershell 脚本的错误消息在 Visual Studio 输出中可见。
这就是我到目前为止修改我的 csproj 文件的方式:
<Project ToolsVersion="12.0" DefaultTargets="MyCustomTarget;Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Target Name="MyCustomTarget">
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">
%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
</PowerShellExe>
<ScriptLocation Condition=" '$(ScriptLocation)'=='' ">
C:\Code\MyScript.ps1
</ScriptLocation>
</PropertyGroup>
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "& { $(ScriptLocation) } "" />
</Target>
为简单起见,我的脚本如下:
if(((get-date -Format "mm") % 2) -eq 0){ exit 0 } else { write-error "The minute is not equally divisible by 2.";exit 1 }
但是,当我去构建项目时,我现在看到我的 powershell 脚本在记事本中打开,并且构建停止,直到我关闭记事本。关闭后,我收到一个错误,我找不到任何解决该错误的方法。任何帮助将不胜感激。
The command "
%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
-NonInteractive -executionpolicy Unrestricted -command "& {
C:\Code\MyScript.ps1
} "" exited with code 9009. MyWebApi2Project C:\Code\MySolution\MyWebApi2Project\MyWebApi2Project.csproj 269
【问题讨论】:
-
对于所有面临同样问题的人 - $(PowerShellExe) 和 $(ScriptLocation) 与行尾一起被解析(这并不明显,但很容易从控制台输出格式中获得) .有完全相同的问题,当我将变量定义重写为单行时,它就消失了。
标签: asp.net powershell msbuild csproj msbuild-task