In MsBuild, how do I run something PowerShell and have all errors reported [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 04:53:28

You can use the following example:

<InvokeScript Condition="..."
              PowerShellProperties="..."
              ScriptFile="[PATH TO PS1 FILE]"
              Function="[FUNCTION TO CALL IN PS1]"
              Parameters="..."
              RequiredOutputParams="...">
  <!-- You can catch the output in an Item -->
  <Output TaskParameter="OutputResults"
          ItemName="Output" />
</InvokeScript>

This can be used in MSBuild.

Weeeeelll, you could use something long winded like this until you find a better way:-

<PropertyGroup>
  <__PsInvokeCommand>powershell "Invoke-Command</__PsInvokeCommand>
  <__BlockBegin>-ScriptBlock { $errorActionPreference='Stop';</__BlockBegin>
  <__BlockEnd>; exit $LASTEXITCODE }</__BlockEnd>
  <_PsCmdStart>$(__PsInvokeCommand) $(__BlockBegin)</PsCmdStart>
  <_PsCmdEnd>$(__BlockEnd)"</PsCmdEnd>
</PropertyGroup>

And then 'all' you need to do is:

<Exec 
  IgnoreStandardErrorWarningFormat="true"
  Command="$(_PsCmdStart)$(ThingToDo)$(_PsCmdEnd)" />

The single redeeming feature of this (other than trapping all error types I could think of), is that it works OOTB with any PowerShell version and any MSBuild version.

I'll get my coat.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!