Why does PowerShell fail to build my .net solution

2019-06-21 05:41发布

问题:

I've written a PowerShell script to build several .net solutions one after the other. It simply makes several calls to tfget (to get latest) followed by calls to devenv.exe (to build the .sln files).

Here's the code:

tfget -item $SolutionPath -overwrite -recurse -ev +errors
...
$out = invoke-expression "devenv.com /rebuild debug $SolutionPath"

Almost every time I run the script one of the solutions fails to build and I get an error from CSC.exe (?) saying:

error CS1606: Assembly signing failed; output may not be signed -- The process cannot access the file because it is being used by another process.

This happens even though I've closed all instances of Visual Studio holding these solutions and I've none of their exes running on my machine.

A similar batch file that I've written works just fine. It's only PowerShell that complains about the file being used by another process.

How can I avoid having this happen? Are there any better examples out there of building .net solutions through PowerShell?

回答1:

Don't use invoke-expression. Just call devenv.exe directly on the SLN file (or for that matter just use MSBuild.exe unless you have setup or other unsupported project types). One of the beauties of using a shell scripting language is that they are designed to work with console exes rather seamlessly. We do this all the time in PowerShell scripts:

msbuild.exe "R:\Source\Foo.sln" /t:build /p:Configuration=Debug `
    /v:detailed 2>&1 | Out-String -stream -width 1024 > $DebugBuildLogFile

We run the output through Out-String so that the log file output doesn't wrap at 80 or 120 chars (default width of the console that runs the script).



回答2:

That's because devenv is running in the background. You have to run it and wait until it finishes.

This should work:

$p = Start-Process -FilePath devenv -ArgumentList $solutionPath,"/Rebuild Debug" -PassThru
$null = $p.WaitForExit(-1)

I use it to build my solutions as well.



回答3:

1684 and 1606 for system.dll

step 1: Remove [ control panal-> Microsoft .NET Framework 3.5 SP1 and KB976769v2 under Microsoft .NET Framework 3.0 Service Pack 2]

step 2:  windows update-> express-> .NET versions 2.0 through 3.5 (KB951847) x86.

then do the msbuild again. I may pass.