File System - Check Disk Space

Octopus.Script exported 2020-12-14 by dandraka belongs to ‘File System’ category.

Checks all or specified fixed drives for free space, either as an absolute number (GB) or relative (%). If the available disk space does not meet the minimum requirements, as set in the parameters, as error is thrown.

Author: Jim (Dimitrios) Andrakakis, dandraka.com

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Required Disk Space (GB)

fschkSpaceGB = 1

Number, integer. The minimum required space in GB. Zero means do not check GB. Example: 2 which means 2GB.

Required Disk Space (%)

fschkSpacePercent = 0

Number, integer. The minimum required space in % of the total space. Zero means do not check %. Example: 10 which means 10%.

Drives

fschkDrives =

Comma separated list of drive names to check. Please use the drive label, i.e. the drive letter and a colon (C: , D: etc). Example: C:,D:,H: which means, the check will be done on C:, D: and H:, if they exist; if some of them do not exist in the system, they will be ignored; if none of them exist, an error is thrown.

Script body

Steps based on this template will execute the following PowerShell script.


# Jim (Dimitrios) Andrakakis
# dandraka.com
# December 2020

param([int]$pSpaceGB = $fschkSpaceGB, 
	[int]$pSpacePercent = $fschkSpacePercent, 
    [string]$pDrives = $fschkDrives)

# ================= PARAMETERS, CONSTANTS ETC =================

$ErrorActionPreference = "Stop"
Clear-Host

$win32_logicaldisk_LocalDiskDriveType = 3

Write-Host "Parameters: SpaceGB '$pSpaceGB'"
Write-Host "Parameters: SpacePercent '$pSpacePercent'"
Write-Host "Parameters: Drives '$pDrives'"

[bool]$checkSpaceAbsolute = $false
[bool]$checkSpacePercent = $false
[bool]$checkAllDrives = $true
$driveList = New-Object System.Collections.ArrayList

# ================= SANITY CHECKS =================

$allDrives = get-wmiobject -class win32_logicaldisk | Where-Object { $_.DriveType -eq $win32_logicaldisk_LocalDiskDriveType } 
Write-Host "Drives found in the system: $($allDrives | ForEach-Object { $_.DeviceID })"

if ($pSpaceGB -gt 0) {
    $checkSpaceAbsolute = $true
    Write-Host "Will check that space > $pSpaceGB"
}

if ($pSpacePercent -gt 0) {
    $checkSpacePercent = $true
    Write-Host "Will check that space > $pSpacePercent %"
}

if ((-not $checkSpaceAbsolute) -and (-not $checkSpacePercent)) {
    Write-Error "Neither Space(GB) nor Space(%) check was specified. Please specify at least one."
}

if ([string]::IsNullOrWhiteSpace($pDrives)) {
    foreach($d in $allDrives) { $driveList.Add($d) | Out-Null }
    Write-Host "Will check all fixed drives $($driveList | ForEach-Object { $_.DeviceID + " " })"
}
else {
    $checkAllDrives = $false
    foreach($d in $allDrives) { if ($pDrives.Contains($d.DeviceID)) { $driveList.Add($d) | Out-Null | Out-Null } }
    Write-Host "Will check fixed drives $($driveList | ForEach-Object { $_.DeviceID + " " })"
}

if ($driveList.Count -eq 0) {
    Write-Error "No drives were found or, most likely, the drive list parameter does not contain any of the existing drives."
}

# ================= RUN CHECKS =================
foreach($d in $driveList) {
    $driveDescr = "$($d.DeviceID) [$($d.VolumeName)]"
    $pDrivespaceGBFree = [Math]::Round(($d.FreeSpace / [Math]::Pow(1024,3)), 1)
    $pDrivespaceGBTotal = [Math]::Round(($d.Size / [Math]::Pow(1024,3)), 1)
    $pDrivespacePercentFree = [Math]::Round($pDrivespaceGBFree / $pDrivespaceGBTotal,1) * 100
    Write-Host "Drive $driveDescr : Free $pDrivespaceGBFree GB ($pDrivespacePercentFree%), Total $pDrivespaceGBTotal GB"

    if ($checkSpaceAbsolute) {
        if ($pDrivespaceGBFree -lt $pSpaceGB) { 
            Write-Error "Drive $driveDescr has less than the required space ($pSpaceGB GB)"
        }
    }
    if ($checkSpacePercent) {
        if ($pDrivespacePercentFree -lt $pSpacePercent) { 
            Write-Error "Drive $driveDescr has less than the required space ($pSpacePercent %)"
        }
    }
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "e74ff6a3-65ce-4a3a-8cbd-2224653af3a2",
  "Name": "File System - Check Disk Space",
  "Description": "Checks all or specified fixed drives for free space, either as an absolute number (GB) or relative (%).\nIf the available disk space does not meet the minimum requirements, as set in the parameters, as error is thrown.\n\nAuthor: Jim (Dimitrios) Andrakakis, [dandraka.com](https://dandraka.com)",
  "Version": 1,
  "ExportedAt": "2020-12-14T09:37:17.680Z",
  "ActionType": "Octopus.Script",
  "Author": "dandraka",
  "Packages": [],
  "Parameters": [
    {
      "Id": "640be7e1-aa4f-4cc8-b01c-4fe6ef1a3757",
      "Name": "fschkSpaceGB",
      "Label": "Required Disk Space (GB)",
      "HelpText": "Number, integer.\nThe minimum required space in GB.\nZero means do not check GB.\nExample: 2\nwhich means 2GB.",
      "DefaultValue": "1",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "0d28eda8-4fd0-47c5-8faf-6d1bd27a7247",
      "Name": "fschkSpacePercent",
      "Label": "Required Disk Space (%)",
      "HelpText": "Number, integer.\nThe minimum required space in % of the total space.\nZero means do not check %.\nExample: 10\nwhich means 10%.",
      "DefaultValue": "0",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "00037224-9932-46e3-9cfb-5723e0b8d702",
      "Name": "fschkDrives",
      "Label": "Drives",
      "HelpText": "Comma separated list of drive names to check.\nPlease use the drive label, i.e. the drive letter and a colon (C: , D: etc).\nExample: C:,D:,H:\nwhich means, the check will be done on C:, D: and H:, if they exist; if some of them do not exist in the system, they will be ignored; if none of them exist, an error is thrown.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "\n# Jim (Dimitrios) Andrakakis\n# dandraka.com\n# December 2020\n\nparam([int]$pSpaceGB = $fschkSpaceGB, \n\t[int]$pSpacePercent = $fschkSpacePercent, \n    [string]$pDrives = $fschkDrives)\n\n# ================= PARAMETERS, CONSTANTS ETC =================\n\n$ErrorActionPreference = \"Stop\"\nClear-Host\n\n$win32_logicaldisk_LocalDiskDriveType = 3\n\nWrite-Host \"Parameters: SpaceGB '$pSpaceGB'\"\nWrite-Host \"Parameters: SpacePercent '$pSpacePercent'\"\nWrite-Host \"Parameters: Drives '$pDrives'\"\n\n[bool]$checkSpaceAbsolute = $false\n[bool]$checkSpacePercent = $false\n[bool]$checkAllDrives = $true\n$driveList = New-Object System.Collections.ArrayList\n\n# ================= SANITY CHECKS =================\n\n$allDrives = get-wmiobject -class win32_logicaldisk | Where-Object { $_.DriveType -eq $win32_logicaldisk_LocalDiskDriveType } \nWrite-Host \"Drives found in the system: $($allDrives | ForEach-Object { $_.DeviceID })\"\n\nif ($pSpaceGB -gt 0) {\n    $checkSpaceAbsolute = $true\n    Write-Host \"Will check that space > $pSpaceGB\"\n}\n\nif ($pSpacePercent -gt 0) {\n    $checkSpacePercent = $true\n    Write-Host \"Will check that space > $pSpacePercent %\"\n}\n\nif ((-not $checkSpaceAbsolute) -and (-not $checkSpacePercent)) {\n    Write-Error \"Neither Space(GB) nor Space(%) check was specified. Please specify at least one.\"\n}\n\nif ([string]::IsNullOrWhiteSpace($pDrives)) {\n    foreach($d in $allDrives) { $driveList.Add($d) | Out-Null }\n    Write-Host \"Will check all fixed drives $($driveList | ForEach-Object { $_.DeviceID + \" \" })\"\n}\nelse {\n    $checkAllDrives = $false\n    foreach($d in $allDrives) { if ($pDrives.Contains($d.DeviceID)) { $driveList.Add($d) | Out-Null | Out-Null } }\n    Write-Host \"Will check fixed drives $($driveList | ForEach-Object { $_.DeviceID + \" \" })\"\n}\n\nif ($driveList.Count -eq 0) {\n    Write-Error \"No drives were found or, most likely, the drive list parameter does not contain any of the existing drives.\"\n}\n\n# ================= RUN CHECKS =================\nforeach($d in $driveList) {\n    $driveDescr = \"$($d.DeviceID) [$($d.VolumeName)]\"\n    $pDrivespaceGBFree = [Math]::Round(($d.FreeSpace / [Math]::Pow(1024,3)), 1)\n    $pDrivespaceGBTotal = [Math]::Round(($d.Size / [Math]::Pow(1024,3)), 1)\n    $pDrivespacePercentFree = [Math]::Round($pDrivespaceGBFree / $pDrivespaceGBTotal,1) * 100\n    Write-Host \"Drive $driveDescr : Free $pDrivespaceGBFree GB ($pDrivespacePercentFree%), Total $pDrivespaceGBTotal GB\"\n\n    if ($checkSpaceAbsolute) {\n        if ($pDrivespaceGBFree -lt $pSpaceGB) { \n            Write-Error \"Drive $driveDescr has less than the required space ($pSpaceGB GB)\"\n        }\n    }\n    if ($checkSpacePercent) {\n        if ($pDrivespacePercentFree -lt $pSpacePercent) { \n            Write-Error \"Drive $driveDescr has less than the required space ($pSpacePercent %)\"\n        }\n    }\n}"
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-check-disk-space.json",
  "Website": "/step-templates/e74ff6a3-65ce-4a3a-8cbd-2224653af3a2",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w/+uv/+OP/8pg/89A/8VQ/9+f/9+A/9qP/+/P/99//7cA/7IA/7wA/6sA/+/A39/f/64A2tjX//Tfn5+f/7MA/7sA/74A/60A/9Zw/7gA/74Q/7oA/7UA/7EAi6g4fwAAAuRJREFUeNrs21tT2mAUheEvaaCQQCSczyi29WzP7f//Z6UdZBIIM44us9hf13vbC+eRbLo3qnNKKaWUUkoppZRSSimllFJKKaWUwlVrLhsvbrLqnoai2+yFr2x+fwKOwasZm/oXdEczxDQnOyYhqgn3uQpxDZhz3gdCesQ3r3mIjDfwXagj7NEgKywkXLMgSzCE9mz1wBDaOzDYETYEEUSQ/wPSaz6rQa174pDnt1x5Atm8emtPIGG48gUClJAh/XtPILi36+L3Zz6oVdBF4w32/sIbYmWH6qAPX5fzjio/14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