在TFS Builds(TFS2008)中有没有任何msbuild任务可以获取单位中的可用空间?

3
我的构建失败了,因为有时候我的构建服务器没有足够的磁盘空间。问题在于错误信息不清楚。它会在任意部分失败,并且当这种情况发生时日志不可用。
我正在寻找一个任务来获取单元的可用空间,以便在磁盘空间不足时放置一条消息...但我找不到任何相关的内容。
在TFS Builds中有任何用于获取单元中可用空间的msbuild任务吗?
我知道我可以开发一个C#任务并自己完成,但我现在没有时间。
谢谢。
2个回答

2
您可以使用MSBuild扩展包来完成此操作:
<!--- Check drive space -->
<MSBuild.ExtensionPack.Computer.SystemDrive TaskAction="CheckDriveSpace" Drive="DriveLetter:\" MachineName="Name" UserName="UserName" UserPassword="Password" MinSpace="SpaceToTriggerError EX: 500" Unit="Size EX: MB" ContinueOnError="false"/>

<!--- Check drive space on a remote machine -->
<MSBuild.ExtensionPack.Computer.SystemDrive TaskAction="GetDrives"  MachineName="Name" UserName="UserName" UserPassword="Password" />

谢谢。我将进行测试。但是我认为MSBuild ExtensionPack只支持TFS2010...(忘记在问题中提及) - Oscar Foley
扩展支持2008-不确定SystemDrive命名空间是在什么时候添加的。试一试,然后告诉我。 - Mike Veigel

1

还有另一种方法。添加一个新目标,该目标将执行PowerShell脚本。此PowerShell脚本将检查可用空间。

<Target Name="CheckFreeSpace">
    <PropertyGroup>
        <PowerShellExe Condition=" '$(PowerShellExe)'=='' ">
            %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
        </PowerShellExe>
        <ScriptLocation Condition=" '$(ScriptLocation)'=='' ">
            C:\Build\CheckFreeSpace.ps1
        </ScriptLocation>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
                     -command &quot;&amp; invoke-command -scriptblock { 
                              &amp;&apos;$(ScriptLocation)&apos;}
                              &quot;"/>  
</Target>

并创建PowerShell脚本C:\Build\CheckFreeSpace.ps1

Write-Output "Powershell scrip start."

$disks = Get-CimInstance -ClassName Win32_LogicalDisk

$freeSpace = $disks | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (MB)'; Expression= { [int]($_.FreeSpace / 1MB) }} -First 1

if($freeSpace.'FreeSpace (MB)' -gt 4000){ # Free space in MB
    Write-Output "There is more then 4000 MB free space."
    Exit 0
} else {
    Write-Output "Not enough free space."
    Exit -1
} 

也可以使用单行命令,但我没有进行适当的测试:

"& invoke-command -scriptblock { 
      &Get-CimInstance -ClassName Win32_LogicalDisk `
| Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (MB)'; Expression= { [int]($_.FreeSpace / 1MB) }} `
,@{'Name' = 'Enought'; Expression= { [int]($_.FreeSpace / 1MB) -gt 400000 }} -First 1 `
| %{if($_ -match "True"){Exit 0 }else{Exit -1}}}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接