我的构建失败了,因为有时我的构建服务器中没有磁盘空间。问题是错误信息不清楚。它在任何随机部分都失败,并且发生这种情况时日志不可用。
我正在寻找一个任务来获得一个单元的可用空间,这样我就可以在磁盘空间不足时发出一条消息......但我找不到任何东西。
是否有任何 msbuild 任务可以在 TFS 构建中的一个单元中获取可用空间?
我知道我可以用 C# 开发一项任务并自己完成。但我现在没有时间。
谢谢。
我的构建失败了,因为有时我的构建服务器中没有磁盘空间。问题是错误信息不清楚。它在任何随机部分都失败,并且发生这种情况时日志不可用。
我正在寻找一个任务来获得一个单元的可用空间,这样我就可以在磁盘空间不足时发出一条消息......但我找不到任何东西。
是否有任何 msbuild 任务可以在 TFS 构建中的一个单元中获取可用空间?
我知道我可以用 C# 开发一项任务并自己完成。但我现在没有时间。
谢谢。
您可以使用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" />
还有另一种方式。添加一个新目标,它将执行 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 "& invoke-command -scriptblock {
&'$(ScriptLocation)'}
""/>
</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}}}"