STDockerPS.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#requires -version 2
function DockerPs {
<#
.SYNOPSIS
    Turn "docker ps" output into custom PowerShell objects. By default omit the name column
    (so long as it remains the last one and names can't have spaces).
 
.DESCRIPTION
    The parsing to "objectify" relies on the column widths in this case, rather than my usual
    approach of using regular expressions, since it seems more robust for this
    case/output at hand.
 
    I omit the names of the containers by default (long story), but for now it's so it
    fits better into a default-sized PowerShell window. To include the names, add the parameter
    "-names" (will not be tab completed).
     
    Otherwise "docker ps --help" is what you should read for further information about the
    available parameters.
 
    -q / --quiet is special-cased to work identically as in the native "docker ps -q" (lists
    only container IDs).
 
.PARAMETER Names
    Include the container name column.
 
.EXAMPLE
dockerps
 
 
CONTAINER_ID : d92cb8edaf53
IMAGE : microsoft/nanoserver
COMMAND : "powershell"
CREATED : 19 hours ago
STATUS : Up 19 hours
PORTS :
 
CONTAINER_ID : 9c45b67925db
IMAGE : microsoft/nanoserver
COMMAND : "powershell"
CREATED : 19 hours ago
STATUS : Up 19 hours
PORTS :
 
CONTAINER_ID : 520649cfb9d7
IMAGE : microsoft/nanoserver
COMMAND : "cmd"
CREATED : 20 hours ago
STATUS : Up 20 hours
PORTS :
 
.EXAMPLE
$Var = dockerps
 
$Var.CONTAINER_ID # list all container IDs
$Var.COMMAND # list all commands
$Var.STATUS # list all statuses
$Var.IMAGE # list all image titles
 
.EXAMPLE
PS C:\temp\STDockerPs> @(dockerps | Where { $_.IMAGE -like "microsoft/*" }).Count
3
 
PS C:\temp\STDockerPs> @(dockerps | Where { $_.IMAGE -like "microsoft/*" }).Command
"powershell"
"powershell"
"cmd"
 
 
.EXAMPLE
(dockerps -names)[2].Names
temp1
 
.EXAMPLE
(dockerps -names)[0]
 
 
CONTAINER_ID : d92cb8edaf53
IMAGE : microsoft/nanoserver
COMMAND : "powershell"
CREATED : 19 hours ago
STATUS : Up 19 hours
PORTS :
NAMES : temp3
#>
  
    if ($Args -match '-q|--quiet') {
        docker ps -q
        return
    }
    
    elseif ($Args -match '-names') {
        $DockerPSOutput = @(docker ps ($Args -replace '-names'))
    }
    
    else {
        $DockerPSOutput = @(docker ps $Args) -replace '\s+\S+$'
    }
    
    $DockerPSTitles = $DockerPSOutput | Select-Object -First 1
    
    # Since we ((for) now) have a predictable and logical starting point for each
    # column (same char as the header), I will play with SubString to PSobjectify the stuff...
    
    $Indexes = @()
    $Headers = @("CONTAINER ID", "IMAGE", "COMMAND", "CREATED", "STATUS",
        "PORTS")
    if ($Args -match '-names') {
        $Headers += "NAMES"
    }
    $Indexes += $DockerPSTitles.IndexOf($Headers[0])
    $Indexes += $DockerPSTitles.IndexOf($Headers[1])
    $Indexes += $DockerPSTitles.IndexOf($Headers[2])
    $Indexes += $DockerPSTitles.IndexOf($Headers[3])
    $Indexes += $DockerPSTitles.IndexOf($Headers[4])
    $Indexes += $DockerPSTitles.IndexOf($Headers[5])
    if ($Args -match '-names') {
        $Indexes += $DockerPSTitles.IndexOf($Headers[6])
    }
    $Indexes += $DockerPSTitles.Length # max name length was 5... (the length of "NAMES")
    $DockerPSOutput | Select-Object -Skip 1 | ForEach-Object {
        Write-Verbose -Message "Current line: $_ (length: $($_.Length))."
        $Indexes[-1] = ([String]$_).Length
        # Avoid spaces in the titles for easier access later.
        $Object = "" | Select-Object -Property ($Headers -replace ' ', '_')
        foreach ($i in 0..($Indexes.Count - 2)) {
            $CurrentHeader = $Headers[$i] -replace ' ', '_'
            $Object.$CurrentHeader = $_.PadRight($Indexes[-1], " ").SubString(
                $Indexes[$i], ($Indexes[$i + 1] - $Indexes[$i])
            ).TrimEnd()
        }
        $Object
    }

}