2008-09-16 14 views
5

PowerShellスクリプトを実行して別のパラメータを渡す優れたMSBuildタスクについて知っていますか?良いPowerShell MSBuildタスクはありますか?

B# .NET Blog: Invoking PowerShell scripts from MSBuildが見つかりましたが、もう少し洗練されたものを期待しています。

私が何かを見つけることができないならば、私はもちろん先に進んで、そのブログポストをスターターとして使って自分自身を磨きます。

+0

私はブログ投稿をスターターとして使用したいと思います。私たちはマイクロソフトのコンサルタントと仕事をしています。私はcodeplexでPowershell MSBuildタスクを試しました.Bart De Smetが作成したタスクで動作するPowershellスクリプトでエラーになります。 –

答えて

7

- PowerShellベースのビルド環境を参照することもできます。

9

3.5の場合はhttp://powershellmsbuild.codeplex.com/を使用できます。 It'd be nice if there was a NuGet package for it that one could leverage via NuGet package restore

4.0は、あなたがin the code galleryMSBuild Extension Pack (one of the top task libraries - 400+ Tasks & recommended in Inside MSBuild)にロールバックされた取得することができ Windows Powershell Task Factoryは(PEEKを持っている the download section of this example releaseからヘルプファイルをダウンロード) PowerShellTaskFactoryを持っています。

4

Duplicate Question and Answer I Postedここでは、閉会投票の後のためのものです。主な違いは、この質問がOOTBに制限されていて、自分の自己回答がその制約内にとどまっていることです。

質問

PowerShellが正しくPowerShellのではない発信者との相互運用が可能な方法で任意のコマンドと、その後のバブルアップ解析と実行エラーでそれをトリガーするための簡単な方法を持っていないようだ - 例えば、cmd.exeTeamCityなど

私の質問は簡単です。 OOTB MSBuild v4とPowerShell v3を使って私にとって最良の方法は何ですか(提案されているように、適切に生産されたMSBuildタスクは除外されますが、PowerShell Task Factoryサンプルを取ることは簡単です)小さなスクリプトセグメント、または(最も一般的には).ps1スクリプトの呼び出しのいずれかを実行するには、次のように入力します。

私は、

<Exec 
    IgnoreStandardErrorWarningFormat="true" 
    Command="PowerShell &quot;$(ThingToDo)&quot;" /> 

悲しげに動作しません: -

  1. ThingToDoが解析に失敗した場合、それはThingToDoが存在していないスクリプトの呼び出しであれば、それはあなたがERRORLEVELベース.cmd結果を伝播する場合、それは毛深い
  2. を取得
  3. を失敗黙っ
  4. 失敗あなたはThingToDo"引用符を埋め込みたい場合は、それはそう

を動作しません、何のMSBuildからPowerShellを実行しているの防弾方法が可能になっていますか?私はできることは何ですか?PsGetすべてをOKにするには?

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

をそしてあなたがする必要がある 'すべては' です:

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

回答

Weeeeelllあなたがより良い方法を見つけるまで、あなたは長い間、このようったらしいものを使用することができます

これの(私が考えることができるすべてのエラータイプをトラップする以外の)これの単一の償還機能は、PowerShellバージョンとMSBuildバージョンのOOTBで動作するということです。

私のコートを手に入れます。楽しいのビットで

0

は、私はこの作品を作るのはかなりきれいな方法を思い付くことをどうにか:

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <!-- #1 Place this line at the top of any msbuild script (ie, csproj, etc) --> 
    <PropertyGroup><PowerShell># 2>nul || type %~df0|find /v "setlocal"|find /v "errorlevel"|powershell.exe -noninteractive -&amp; exit %errorlevel% || #</PowerShell></PropertyGroup> 

    <!-- #2 in any target you want to run a script --> 
    <Target Name="default" > 

    <PropertyGroup> <!-- #3 prefix your powershell script with the $(PowerShell) variable, then code as normal! --> 
     <myscript>$(PowerShell) 
     # 
     # powershell script can do whatever you need. 
     # 
     dir ".\*.cs" -recurse |% { 
     write-host Examining file named: $_.FullName 
     # do other stuff here... 
     } 
     $answer = 2+5 
     write-host Answer is $answer ! 
     </myscript> 
    </PropertyGroup> 

    <!-- #4 and execute the script like this --> 
    <Exec Command="$(myscript)" EchoOff="true" /> 
    </Target> 
</Project> 

注:あなたはまだ標準Execのタスク機能を使用することができます

  • を! (参照:https://msdn.microsoft.com/en-us/library/x8zx72cd.aspx)を
  • あなたのPowerShellスクリプトがちょうどCDATAラッパーの内容を置き、<>または&文字を使用する必要がある場合:

    <script2><![CDATA[ $(PowerShell) 
        # your powershell code goes here! 
        write-host "<<Hi mom!>>" 
    ]]></script2> 
    
  • あなたはMSBuildのスクリプトにアイテムを返すかどうすることができますそれらを得る:

    <script3>$(PowerShell) 
        # your powershell code goes here! 
        (dir "*.cs" -recurse).FullName 
    </script3> 
    
    <Exec Command="$(script3)" EchoOff="true" ConsoleToMSBuild="true"> 
        <Output TaskParameter="ConsoleOutput" PropertyName="items" /> 
    </Exec> 
    <Touch Files="$(items)" /> 
    

参照してください!これらのアイテムを別のmsbuildタスクで使用することができます。タスク:D

関連する問題