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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#requires -version 2
function DockerPs {
<#
.SYNOPSIS
    Turn "docker ps" output into custom PowerShell objects.
 
.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.
 
    Use the parameter -OmitNames (will not be tab completed), to omit the name column for the
    containers. This is to get more condensed output. Just "-Omit" will work as a minimum.
     
    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 OmitNames
    Omit the container name column. Not tab completed. "-Omit" is the shortest allowed form.
 
.EXAMPLE
dockerps | select -first 3
 
 
CONTAINER_ID : 26b08e320d17
IMAGE : microsoft/nanoserver
COMMAND : "cmd"
CREATED : 17 hours ago
STATUS : Up 17 hours
PORTS :
NAMES : temp1234temp1234
 
CONTAINER_ID : d92cb8edaf53
IMAGE : microsoft/nanoserver
COMMAND : "powershell"
CREATED : 39 hours ago
STATUS : Up 39 hours
PORTS :
NAMES : temp3
 
CONTAINER_ID : 9c45b67925db
IMAGE : microsoft/nanoserver
COMMAND : "powershell"
CREATED : 40 hours ago
STATUS : Up 40 hours
PORTS :
NAMES : temp2
 
.EXAMPLE
$Var = dockerps
 
$Var.CONTAINER_ID # list all container IDs (but use -q for (only) that)
$Var.COMMAND # list all commands
$Var.STATUS # list all statuses
$Var.IMAGE # list all image titles
$Var.NAMES # list all container names
 
.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 -omit | select -first 1
 
 
CONTAINER_ID : 26b08e320d17
IMAGE : microsoft/nanoserver
COMMAND : "cmd"
CREATED : 17 hours ago
STATUS : Up 17 hours
PORTS :
 
.EXAMPLE
dockerps | select -first 1
 
 
CONTAINER_ID : 26b08e320d17
IMAGE : microsoft/nanoserver
COMMAND : "cmd"
CREATED : 17 hours ago
STATUS : Up 17 hours
PORTS :
NAMES : temp1234temp1234
 
 
 
.EXAMPLE
PS C:\temp\STDockerPs> (dockerps).CONTAINER_ID
26b08e320d17
d92cb8edaf53
9c45b67925db
520649cfb9d7
 
.EXAMPLE
(dockerps -a).CONTAINER_ID
26b08e320d17
d92cb8edaf53
9c45b67925db
520649cfb9d7
a0fc2bb9ad1b
e82a27f84cb6
 
#>
  
    if ($Args -match '-q|--quiet') {
        docker ps ($Args -replace '-Omit')
        return
    }
    
    else {
        $PreservedArgs = $Args | ForEach-Object { $_ } # deep copy
        $DockerPSOutput = @(@(docker ps ($Args -replace '-Omit')) | Where-Object {
            $_ -match '\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", "NAMES")
    foreach ($Header in $Headers) {
        $Indexes += $DockerPSTitles.IndexOf($Header)
    }
    $Indexes += 0 # dummy value, replaced later for each container line, small "trick"..
    $DockerPSOutput | Select-Object -Skip 1 | ForEach-Object {
        Write-Verbose -Message "Current line: $_ (length: $($_.Length))." #-Verbose
        $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 = $_.SubString(
                $Indexes[$i], ($Indexes[$i + 1] - $Indexes[$i])
            ).TrimEnd()
        }
        if ($PreservedArgs -match '-Omit') {
            $Object | Select-Object -Property @($Headers[0..5] -replace ' ', '_')
        }
        else {
            $Object
        }
    }
}