2013-08-06 60 views
3

我使用PowerShell为项目创建了一些新的构建脚本,并且在我将其称为并将其保存到文本文件时捕获MSBuild的输出。我试过,没有运气这样做,到目前为止,一对夫妇不同的方法 - 这是我最后一次尝试(写Buildlog只是手柄核销输出到日志):在PowerShell脚本中捕获MSBuild.exe输出

开始处理$ msBuildExecutable $ buildArgs -Wait | Write-Buildlog

尽管MSBuild运行正常,但根本没有输出被捕获。任何提示将不胜感激,因为我已经做了一些搜索,并没有发现任何有用的迄今,这是令人惊讶的:)

谢谢!

回答

3

导向错误如果MSBuild中的一个记录或处理cmdlet的要管输出,你不应该Start-Process首发,只是执行它正常。

PS> msbuild.exe $flag1 $flag2 $thingToBuild | Write-Buildlog 

您可能还需要重定向stderr以捕获更多输出。在这种情况下,你将需要添加2>&1

PS> msbuild.exe $flag1 $flag2 $thingToBuild 2>&1 | Write-Buildlog 

Start-Process将启动PowerShell的任何环境或托管之外你的过程,所以获得输出和发送消息到的cmdlet变得更加困难。如果你想在PowerShell中处理可执行的输出,那么最好是一直停留在PowerShell环境中。

+0

嗨,这似乎不工作: $ msBuildExecutable + $ buildArgs 2>&1 | Write-BuildLog 甚至似乎没有运行MSBuild? – DashRantic

+0

更多详细信息 - 我正在寻找运行特定版本的MSBuild,这就是为什么我将它放在一个变量中。但看起来我可以按照您提出的方式运行它,这就是为什么我首先使用Start-Process的原因 - 还有另一种方法可以在没有Start-Process的情况下执行此操作吗? – DashRantic

+0

也只是试过:&$ msBuildExecutable $ buildArgs 2>&1 | Write-BuildLog仍然没有任何东西 – DashRantic

2

Start-Process CmdLet你有-RedirectStandardOutput参数;你有没有测试它?

Start-Process -FilePath "C:\Windows\system32\ping.exe" -ArgumentList "MyMachine" -RedirectStandardOutput "c:\temp\p.txt" -NoNewWindow 

您还可以-RedirectStandardError

+0

谢谢!但是我想将输出发送到我的Write-Buildlog cmdlet,它看起来不像-RedirectStandardError可以做的那样? – DashRantic

+0

“-NoNewWindow”为我工作。谢谢! – suhendri

2

所有你需要的是:

& msbuild.exe .\yourproj.sln |Out-Host 

甚至: & msbuild.exe \ yourproj.sln |出文件C:\ log.txt的

如果写入文件是什么你要。

1

你可以做任何你想要的输出,如果你像这样运行:

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) 

说你想有输出到文件中。

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) | Out-File C:\text.txt 

或者它像这样写......

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) 2>&1 >> C:\text.txt 

注:2 “>>” 是指追加1 “>” 覆盖先前添加的行。

或者如果你只想从一堆输出中得到一个像“真”一样的单词。

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) 
if($MSBuid -match "true") 
{ 
    Write-Host "Whatever you want to say about what's true" 
} 

如果你想在控制台中看到它,你可以这样做。

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) | Out-Host 

或者......

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) 
Write-Host $MSBuild