Public/Get-DockerInfo.ps1

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
<#
    .SYNOPSIS
     Gets info about the Docker-For-Windows Docker Server and Client on a Windows host. This is mainly
     used to determine if the Docker Server is running in Windows Container mode or Linux Container mode.
 
    .DESCRIPTION
        See .SYNOPSIS
 
    .EXAMPLE
        # Open an elevated PowerShell Session, import the module, and -
 
        PS C:\Users\zeroadmin> Get-DockerInfo
         
#>

function Get-DockerInfo {
    $DockerVersionInfo = docker version
    [System.Collections.ArrayList]$DockerClientInfo = @()
    [System.Collections.ArrayList]$DockerServerInfo = @()
    foreach ($line in $DockerVersionInfo) {
        if ($line -match "^Client") {
            $ClientOrServer = "Client"
        }
        if ($line -match "^Server") {
            $ClientOrServer = "Server"
        }
        if (![string]::IsNullOrEmpty($line)) {
            if ($ClientOrServer -eq "Client" -and $line -notmatch "^Client") {
                $null = $DockerClientInfo.Add($line.Trim())
            }
            if ($ClientOrServer -eq "Server" -and $line -notmatch "^Server") {
                $null = $DockerServerInfo.Add($line.Trim())
            }
        }
    }

    [pscustomobject]$DockerClientPSObject = @{}
    [pscustomobject]$DockerServerPSObject = @{}
    foreach ($Property in $DockerClientInfo) {
        $KeyValuePrep = $Property -split ":[\s]+"
        $key = $KeyValuePrep[0].Trim()
        $value = $KeyValuePrep[1].Trim()

        $null = $DockerClientPSObject.Add($key,$value)
    }
    foreach ($Property in $DockerServerInfo) {
        if ([bool]$($Property -match ":[\s]+")) {
            $KeyValuePrep = $Property -split ":[\s]+"
        }
        elseif ([bool]$($Property -match ":")) {
            $KeyValuePrep = $Property -split ":"
        }
        $key = $KeyValuePrep[0].Trim()
        if ($KeyValuePrep[1] -ne $null) {
            $value = $KeyValuePrep[1].Trim()
        }

        $null = $DockerServerPSObject.Add($key,$value)
    }

    [pscustomobject]@{
        DockerServerInfo    = $DockerServerPSObject
        DockerClientInfo    = $DockerClientPSObject
    }
}