PowerShellスクリプトを実行して別のパラメータを渡す優れたMSBuildタスクについて知っていますか?良いPowerShell MSBuildタスクはありますか?
B# .NET Blog: Invoking PowerShell scripts from MSBuildが見つかりましたが、もう少し洗練されたものを期待しています。
私が何かを見つけることができないならば、私はもちろん先に進んで、そのブログポストをスターターとして使って自分自身を磨きます。
PowerShellスクリプトを実行して別のパラメータを渡す優れたMSBuildタスクについて知っていますか?良いPowerShell MSBuildタスクはありますか?
B# .NET Blog: Invoking PowerShell scripts from MSBuildが見つかりましたが、もう少し洗練されたものを期待しています。
私が何かを見つけることができないならば、私はもちろん先に進んで、そのブログポストをスターターとして使って自分自身を磨きます。
- PowerShellベースのビルド環境を参照することもできます。
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 galleryが MSBuild 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
を持っています。
Duplicate Question and Answer I Postedここでは、閉会投票の後のためのものです。主な違いは、この質問がOOTBに制限されていて、自分の自己回答がその制約内にとどまっていることです。
PowerShellが正しくPowerShellのではない発信者との相互運用が可能な方法で任意のコマンドと、その後のバブルアップ解析と実行エラーでそれをトリガーするための簡単な方法を持っていないようだ - 例えば、cmd.exe
、TeamCityなど
私の質問は簡単です。 OOTB MSBuild v4とPowerShell v3を使って私にとって最良の方法は何ですか(提案されているように、適切に生産されたMSBuildタスクは除外されますが、PowerShell Task Factoryサンプルを取ることは簡単です)小さなスクリプトセグメント、または(最も一般的には).ps1
スクリプトの呼び出しのいずれかを実行するには、次のように入力します。
私は、
<Exec
IgnoreStandardErrorWarningFormat="true"
Command="PowerShell "$(ThingToDo)"" />
悲しげに動作しません: -
ThingToDo
が解析に失敗した場合、それはThingToDo
が存在していないスクリプトの呼び出しであれば、それはあなたがERRORLEVEL
ベース.cmd
結果を伝播する場合、それは毛深い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で動作するということです。
私のコートを手に入れます。楽しいのビットで
は、私はこの作品を作るのはかなりきれいな方法を思い付くことをどうにか:
<?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 -& 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>
あなたの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
私はブログ投稿をスターターとして使用したいと思います。私たちはマイクロソフトのコンサルタントと仕事をしています。私はcodeplexでPowershell MSBuildタスクを試しました.Bart De Smetが作成したタスクで動作するPowershellスクリプトでエラーになります。 –