我有一个.NET Framework 4.5.2 WebAPI2项目。在Visual Studio中构建该项目时,我希望运行我编写的PowerShell脚本以便在每次构建之前运行。如果PowerShell脚本以任何非零代码退出,则希望构建失败并将PowerShell脚本的错误消息显示在Visual Studio输出中。
以下是我目前已修改的csproj文件内容:
然而,当我开始构建项目时,我现在看到我的PowerShell脚本在记事本中打开,并且在关闭记事本之前,构建会停止。一旦关闭,我会收到一个错误,但我找不到任何解决错误的方法。非常感谢您的帮助。
以下是我目前已修改的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