Skip to content

Instantly share code, notes, and snippets.

@eizedev
Forked from jdhitsolutions/1-Get-DockerDiskUsage.ps1
Last active January 9, 2020 07:48
Show Gist options
  • Save eizedev/8556efe0f2e7548038e71d358212cc8b to your computer and use it in GitHub Desktop.
Save eizedev/8556efe0f2e7548038e71d358212cc8b to your computer and use it in GitHub Desktop.
A Powershell function that wraps Docker command line output to display disk usage. Save the files without the numeric prefix.
#requires -version 5.1
Function Get-DockerDiskUsage
{
[cmdletbinding()]
[alias("gddu")]
[OutputType("Get-DockerDiskUsage.DockerDiskUsage")]
Param(
[Parameter(Position = 0, HelpMessage = "Get specific usage types. The default is All")]
[ValidateSet("Images", "Containers", "Volumes", "Cache")]
[string[]]$Type
)
Begin
{
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
#define a class
Class DockerDiskUsage
{
[string]$Type
[int]$Total
[int]$Active
[double]$Size
[double]$Reclaim
[int]$ReclaimPercent
#these properties are hidden but they can be used in format.ps1xml files or Select-Object expressions
hidden [string]$SizeUnit
hidden [string]$ReclaimUnit
#methods are hidden because they are only used internally
hidden [string] GetUnit($value)
{
Write-Verbose "[CLASS] Getting unit string from $value"
[regex]$unitrx = "[KMGT]?B"
$r = $unitrx.match($value).value
Write-Verbose "[CLASS] Returning $r"
return $r
}
hidden [double] GetValue($value)
{
Write-Verbose "[CLASS] Getting numeric value from $value"
[regex]$szrx = "\d+(\.\d+)?"
$r = $szrx.match($value).value
Write-Verbose "[CLASS] Returning $r"
return $r
}
hidden [int] GetUnitValue([string]$SizeUnit)
{
Write-Verbose "[CLASS] Getting unit value from $SizeUnit"
$unit = Switch ($sizeUnit)
{
"TB" { 1TB; break }
"GB" { 1GB; break }
"MB" { 1MB; break }
"KB" { 1KB; break }
default { 1 }
}
Write-Verbose "[CLASS] Returning $unit"
return $unit
}
#the constructor
DockerDiskUsage ($Item)
{
Write-Verbose "[CLASS] Invoking constructor for $($item.type)"
$this.Type = $item.Type
$this.Total = $item.totalCount
$this.Active = $item.active
$this.SizeUnit = $this.getunit($item.size)
$this.reclaimUnit = $this.GetUnit($item.reclaimable)
$this.Size = $this.getValue($item.size) * $this.GetUnitValue($this.SizeUnit)
$this.Reclaim = $this.GetValue($item.reclaimable.split()[0]) * $this.GetUnitValue($this.ReclaimUnit)
$this.ReclaimPercent = $this.GetValue($item.reclaimable.split()[1])
}
} #close class definition
} #begin
Process
{
Write-Verbose "[PROCESS] Getting Docker Disk Usage"
$data = docker system df --format "{{json .}}" | ConvertFrom-Json
if ($data)
{
foreach ($item in $data)
{
$item | Out-String | Write-Verbose
#filter on type if specified via the Type parameter
if ( (-not $Type) -OR ( $item.type -match $($type -join "|")))
{
New-Object -TypeName DockerDiskUsage -ArgumentList $item
}
} #foreach item
}
else
{
Write-Warning "Failed to get any Docker disk usage data"
}
} #process
End
{
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
}
$format = "$PSSCRIPTROOT\docker.diskusage.format.ps1xml"
if (Test-Path -path $format)
{
Update-FormatData $format
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
format type data generated 04/05/2019 11:19:27
by BOVINE320\Jeff
-->
<Configuration>
<ViewDefinitions>
<View>
<!--Created 04/05/2019 11:19:27 by BOVINE320\Jeff-->
<Name>default</Name>
<ViewSelectedBy>
<TypeName>Get-DockerDiskUsage.DockerDiskUsage</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Type</Label>
<Width>15</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Total</Label>
<Width>7</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Active</Label>
<Width>7</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Size</Label>
<Width>17</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Reclaim</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>ReclaimPercent</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Type</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Total</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Active</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Size</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Reclaim</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>ReclaimPercent</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<!--Created 04/05/2019 13:41:17 by BOVINE320\Jeff-->
<Name>docker</Name>
<ViewSelectedBy>
<TypeName>Get-DockerDiskUsage.DockerDiskUsage</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Type</Label>
<Width>15</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Total</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Active</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Size</Label>
<Width>11</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Reclaimable</Label>
<Width>16</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Type</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Total</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Active</PropertyName>
</TableColumnItem>
<TableColumnItem>
<Scriptblock>"{0}{1}" -f $($_.size/$_.GetUnitValue($_.sizeunit)),$_.sizeunit</Scriptblock>
</TableColumnItem>
<TableColumnItem>
<Scriptblock>"{0}{1} ({2}%)" -f $($_.reclaim/$_.GetUnitValue($_.reclaimunit)),$_.reclaimunit,($_.ReclaimPercent)</Scriptblock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
@eizedev
Copy link
Author

eizedev commented Jan 9, 2020

Just place these two files in the same directory and without the numeric prefix in the filename.
f.e.
C:\scripts\Get-DockerDiskUsage.ps1 and C:\scripts\docker.diskusage.format.ps1xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment