Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any msbuild-task to get free space in a unit in TFS Builds (TFS2008)?

My builds are failing because some times I have no disk-space in my build server. The problem is that the error message is not clear. It fails in any random part and log is not available when this happens.

I was looking for a task to get free space of a unit so I can put a message if disk space is running low... but I can't find any.

Is there any msbuild-task to get free space in a unit in TFS Builds?

I know I can develop a task in C# and do it myself.. but I don't have the time right now.

Thanks.

like image 700
Oscar Foley Avatar asked Dec 09 '25 03:12

Oscar Foley


2 Answers

You can use the MSBuild Extension Pack to do this:

<!--- 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" />
like image 94
Mike Veigel Avatar answered Dec 11 '25 02:12

Mike Veigel


There is also another way. Add a new target, which will execute PowerShell script. This PowerShell script will check for free space.

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

And create powershell script 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
} 

There is possibility also to use just one liner command. But I did not test it properly:

"& 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}}}"
like image 29
Color Avatar answered Dec 11 '25 01:12

Color



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!