Functions/Public/Get-RancherHost.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
function Get-RancherHost {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        "PSUseDeclaredVarsMoreThanAssignments", 
        "", 
        Justification = "Variable is used in process and end blocks"
    )]
    [CmdletBinding()]
    param(
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Id = '*'
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $EnvironmentId = '*'
        ,
        [Parameter()]
        [ValidateSet('active', 'inactive', 'reconnected', 'finishing-reconnect')]
        [string]
        $State = '*'
        ,
        [Parameter()]
        [switch]
        $Raw
    )

    $Hosts = Invoke-RancherApi -Path '/hosts'
    $Hosts = $Hosts | Where-Object { $_.id -like $Id -and $_.accountId -like $EnvironmentId -and $_.state -like $State }

    if ($Raw) {
        $Hosts
        return
    }
    
    $Hosts | ForEach-Object {
        [pscustomobject]@{
            Environment   = $_.accountId
            Id            = $_.id
            Name          = $_.hostname
            State         = $_.state
            Cores         = $_.milliCpu / 1000
            Storage       = $_.localStorageMb
            Driver        = $_.driver
            OS            = $_.data.fields.info.osInfo.operatingSystem
            StorageDriver = $_.engineStorageDriver
            Labels        = $_.labels.PSObject.Properties | ForEach-Object -begin { $hash = @{} } -process { $hash."$($_.Name)" = $_.Value } -end { $hash }
            Uuid          = $_.uuid
            PSTypeName    = 'RancherHost'
        }
    }
}