为什么PowerShell无法构建我的.net解决方案? (“文件正由另一个进程使用”)

时间:2010-04-01 13:27:37

标签: .net powershell build build-automation

我编写了一个PowerShell脚本,一个接一个地构建了几个.net解决方案。它只是调用tfget(获取最新版本),然后调用devenv.exe(构建.sln文件)。

以下是代码:

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

几乎每次我运行脚本时,其中一个解决方案都无法构建,我从CSC.exe(?)收到错误说:

  

错误CS1606:程序集签名失败;   输出可能没有签名 -   进程无法访问该文件,因为   它被另一个进程使用。

即使我已经关闭了所有持有这些解决方案的Visual Studio实例,并且我的计算机上没有运行任何exes,也会发生这种情况。

我写的类似批处理文件工作得很好。只有PowerShell才会抱怨另一个进程正在使用该文件。

我怎样才能避免这种情况发生?有没有更好的例子通过PowerShell构建.net解决方案?

3 个答案:

答案 0 :(得分:14)

不要使用invoke-expression。只需直接在SLN文件上调用devenv.exe(或者只是使用MSBuild.exe,除非您有设置或其他不受支持的项目类型)。使用shell脚本语言的一个优点是它们被设计为可以无缝地与控制台exes一起使用。我们一直在PowerShell脚本中执行此操作:

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

我们通过Out-String运行输出,以便日志文件输出不会换行为80或120个字符(运行脚本的控制台的默认宽度)。

答案 1 :(得分:3)

那是因为devenv在后台运行。你必须运行它并等到它完成。

这应该有效:

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

我也用它来构建我的解决方案。

答案 2 :(得分:-1)

1684和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.

然后再次执行msbuild。我可以通过。