ContainerInfo/Get-NavContainerIpAddress.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
<#
 .Synopsis
  Get the IP Address of a NAV/BC Container
 .Description
  Inspect the Container and return the IP Address of the first network.
 .Parameter containerName
  Name of the container for which you want to get the IP Address
 .Parameter networkName
  Specify network name if you want to get the IP Address for a specific network
 .Example
  Get-NavContainerIpAddress -containerName navserver
#>

function Get-NavContainerIpAddress {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true)]
        [string] $containerName,
        [Parameter(Mandatory=$false)]
        [string] $networkName = ""
    )

    Process {

        $ip = Invoke-ScriptInNavContainer -containerName $containerName -scriptblock {
            $ip = ""
            $ips = Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" -and $_.IPAddress -ne "127.0.0.1" }
            if ($ips) {
                $ips | ForEach-Object {
                    if ("$ip" -eq "") {
                        $ip = $_.IPAddress
                    }
                }
            }
            $ip
        }

        if ("$ip" -eq "") {
            $inspect = docker inspect $containerName | ConvertFrom-Json
            $networks = $inspect.NetworkSettings.Networks
            $networks | get-member -MemberType NoteProperty | Select-Object Name | % {
                $name = $_.Name
                if (("$ip" -eq "") -and ("$networkName" -eq "" -or "$networkName" -eq "$name")) {
                    $network = $networks | Select-Object -ExpandProperty $name
                    $ip = $network.IPAddress
                }
            }
        }
        return $ip
    }
}
Set-Alias -Name Get-BCContainerIpAddress -Value Get-NavContainerIpAddress
Export-ModuleMember -Function Get-NavContainerIpAddress -Alias Get-BCContainerIpAddress