简体   繁体   English

在MSBuild期间运行Powershell脚本

[英]Run powershell script during MSBuild

I have a .NET Framework 4.5.2 WebAPI2 project. 我有一个.NET Framework 4.5.2 WebAPI2项目。 While building the project in Visual Studio, I want a powershell script I wrote to run before each and every build. 在Visual Studio中构建项目时,我希望在每次构建之前都运行我编写的Powershell脚本。 If the powershell script exits with any non-zero code, I want the build to fail and the error message from the powershell script visible in the Visual Studio output. 如果powershell脚本以任何非零代码退出,我希望构建失败,并且来自powershell脚本的错误消息在Visual Studio输出中可见。

This is how I've modified my csproj file so far: 到目前为止,这是我修改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 &quot;&amp; { $(ScriptLocation) } &quot;" />
</Target>

For simplicity's sake, my script is the following: 为了简单起见,我的脚本如下:

if(((get-date -Format "mm") % 2) -eq 0){ exit 0 } else { write-error "The minute is not equally divisible by 2.";exit 1 }

However, when I go to build the project, I now see my powershell script open in notepad and the build stops until I close notepad. 但是,当我去构建项目时,我现在看到我的Powershell脚本在记事本中打开,并且构建停止,直到我关闭记事本。 Once closed, I'm getting an error and I am not finding any way of resolving the error. 关闭后,我遇到了错误,但找不到任何解决错误的方法。 Any assistance would be much appreciated. 任何帮助将不胜感激。

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 

I was able to get everything working by changing the following. 通过更改以下内容,我可以使所有工作正常进行。 This builds off of Apoorva's answer: 这基于Apoorva的答案:

<Target Name="BeforeBuild">
  <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" /><!-- existing line, not part of answer -->
  <Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; { .\MyScript.ps1 } &quot;" LogStandardErrorAsError="True" ContinueOnError="False" WorkingDirectory="C:\Code" />
</Target>

For me, the inclusion of PowerShellExe and ScriptLocation variables were both causing an error. 对我来说,包含PowerShellExe和ScriptLocation变量都导致错误。 Removing these lines and stripping the Exec line down fixed the issue. 删除这些行并剥离Exec行即可解决此问题。 The inclusion of 'ContinueOnError="False"' and 'LogStandardErrorAsError="True"' ensured the build process would halt if a non-zero exit code was thrown by the powershell script. 包含'ContinueOnError =“ False”'和'LogStandardErrorAsError =“ True”'可确保如果Powershell脚本抛出非零退出代码,则构建过程将停止。

I edited the Target Name="BeforeBuild" as below instead of creating new Target. 我如下编辑目标名称=“ BeforeBuild”,而不是创建新的目标。 So before start of the build the powershell script is executed and build is passed or failed accordingly 因此,在开始构建之前,将执行powershell脚本,并相应地通过或失败构建

<Target Name="BeforeBuild">
   <PropertyGroup>
    <PowerShellExe Condition=" '$(PowerShellExe)'=='' "> 
     C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    </PowerShellExe>
    <ScriptLocation Condition=" '$(ScriptLocation)'=='' ">
      C:\Code\MyScript.ps1
    </ScriptLocation>
  </PropertyGroup>
  <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; { $(ScriptLocation) } &quot;" />
  </Target>

(Edited to add 's' to windows) (已编辑为在Windows中添加“ s”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM