【问题标题】:Run powershell script during MSBuild在 MSBuild 期间运行 powershell 脚本
【发布时间】: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 &quot;&amp; { $(ScriptLocation) } &quot;" />
</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


【解决方案1】:

我如下编辑了 Target Name="BeforeBuild",而不是创建新的 Target。因此,在开始构建之前,会执行 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>

(已编辑以在窗口中添加“s”)

【讨论】:

  • 我将其作为 AfterPublish 任务进行了尝试,它会在记事本中打开脚本。这很奇怪,因为它与另一个几乎完全相同。
  • @Chris 很抱歉发布了 necroposting,但您遇到了这种奇怪的行为,因为 $(PowerShellExe) 和 $(ScriptLocation) 正在与行尾一起被解析。刚遇到同样的问题,当使用单行定义属性时,一切都像魅力一样。在这种情况下,M$ 示例具有误导性。
【解决方案2】:

通过更改以下内容,我能够让一切正常工作。这是基于 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="$(MSBuildProjectDirectory)" />
</Target>

对我来说,包含 PowerShellExe 和 ScriptLocation 变量都会导致错误。删除这些行并剥离 Exec 行解决了这个问题。如果 powershell 脚本抛出非零退出代码,包含 'ContinueOnError="False"' 和 'LogStandardErrorAsError="True"' 可确保构建过程停止。

【讨论】:

  • 这对我也有用powershell -NonInteractive -Executionpolicy Unrestricted .\MyScript.ps1
猜你喜欢
  • 1970-01-01
  • 2017-10-14
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多