为什么PowerShell中不能够培养我的.NET解决方案? ("文件正被另一个进程")我的、中不、进程、解决方案

2023-09-05 00:12:51 作者:孤千羽

我已经写了一个PowerShell脚本来建几个.NET解决方案一个接一个。它只是使多次打电话给tfget(以获得最新的),然后调用的devenv.exe(建的.sln文件)。

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).

这里的code:

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

几乎每次我运行的解决方案之一构建失败,脚本,我从CSC.EXE一个错误说(?):

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

错误CS1606:大会签名失败;   输出可能不会签署 - 在   过程不能访问,因为该文件   它正被另一进程。

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

这是即使我已经关闭的Visual Studio持有这些解决方案的所有实例,我已经在我的机器上运行,没有自己的前男友了。

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.

这是我写的作品就好了类似的批处理文件。就这么抱怨正在被另一个进程的文件只PowerShell的。

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.

如何避免有这种情况发生?通过PowerShell的构建.NET解决方案,有没有更好的例子了吗?

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

推荐答案

不要使用调用-EX pression。只需调用devenv.exe的直接对SLN文件(或与此有关的只是使用MSBuild.exe,除非你有安装或其它不支持的项目类型)。一个使用shell脚本语言之美的是,他们的目的与控制台的EXE工作,而无缝连接。我们这样做是PowerShell脚本所有的时间:

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

我们运行通过了字符串的输出,使得日志文件输出不换行80或120个字符(运行该脚本控制台的默认宽度)。

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).