Public/Get-IBCLILicenses.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
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
function Get-IBCLILicenses
{
    [CmdletBinding()]
    param(
        [Parameter(
            ParameterSetName='NewStream',
            Mandatory=$true,
            Position=0,
            HelpMessage='Enter the Hostname or IP Address of an Infoblox appliance.'
        )]
        [ValidateNotNullOrEmpty()]
        [string]
        $ComputerName,
        [Parameter(
            ParameterSetName='ExistingStream',
            Mandatory=$true,
            Position=0,
            HelpMessage='Enter the ShellStream object returned by Connect-IBCLI.'
        )]
        [ValidateNotNull()]
        [Renci.SshNet.ShellStream]
        $ShellStream,
        [Parameter(
            ParameterSetName='NewStream',
            Mandatory=$true,
            Position=1,
            HelpMessage='Enter the credentials for the appliance.'
        )]
        [PSCredential]
        $Credential,
        [Parameter(
            ParameterSetName='NewStream'
        )]
        [Switch]
        $Force
    )

    Write-Verbose "Fetching 'show license csv' output from $($ShellStream.Session.ConnectionInfo.Host)"
    <#
        'show license csv' returns CSV formatted output of all the licenses
        on that member. If the appliance is a grid master, it will return
        all the licenses in the grid. Sample output:
 
        public_ip,license_type,exp_date,replaced_hardware_id,license_string
        10.1.1.1,Grid,11/09/2016,,GQAAAEm0SGfKtggLHTJvy3v5iA/jTWP/Ezo7w8E=
        10.1.1.1,vNIOS (model IB-VM-810),11/09/2016,,GQAAAFq0VW3LukhREm5vy3i0hw/gWWP4Sz9qysE=
        10.1.1.1,DNS,11/09/2016,,EgAAAEi0T36K9QZbEmUjyH750kvoSw==
        10.1.1.1,DHCP,11/09/2016,,EwAAAEiyX3LE9EkeVyshyXmzzUrkSzQ=
        10.2.2.2,Grid,11/09/2016,,GQAAAHkkdF6RlSaK81y3fgDfOPvJkaP5jpOBov0=
        10.2.2.2,vNIOS (model IB-VM-810),11/09/2016,,GQAAAGokaVSQmWbQ/AC3fgOSN/vKhaP+1pbQq/0=
        10.2.2.2,DNS,11/09/2016,,EgAAAHgkc0fR1ija/Av7fQXfYr/Clw==
        10.2.2.2,DHCP,11/09/2016,,EwAAAHgiY0uf12efuUX5fAKVfb7Ol/Q=
    #>


    if ($PSCmdlet.ParameterSetName -eq 'NewStream') {
        $ShellStream = Connect-IBCLI $ComputerName $Credential -Force:$Force -ErrorAction Stop
    }

    try {

        # the current hardware ID is only returned when you use 'show license'
        # by itself. But we can still get it separately first.
        $hwid = Get-IBCLIHardwareID $ShellStream
        Write-Verbose $hwid

        # If this is a grid master, the 'show license csv' command will return
        # all licenses in the grid differentiated by IP address of the member.
        # So we need to get this member's IP to filter the results with.
        $ip = (Get-IBCLIStatus $ShellStream).IPAddress

        # get the command output and parse the csv
        $output = Invoke-IBCLICommand 'show license csv' $ShellStream
        $csv = $output[0..($output.length-2)] | ConvertFrom-Csv

        $ret = $csv | Where-Object { $_.public_ip -eq $ip } |
            Select-Object `
            @{L='LicenseType';E={$_.license_type}}, `
            @{L='LicenseString';E={$_.license_string}}, `
            @{L='HardwareID';E={$hwid}}, `
            @{L='Expiration';E={
                $outdate = [DateTime]::MinValue
                if ([DateTime]::TryParse($_.exp_date,[ref]$outdate)) {
                    $outdate
                } else {
                    # unparseable usually means 'Permanent'
                    [DateTime]::MaxValue
                }
            }}

        # inject the type name for each result
        $ret | ForEach-Object {
            $_.PSObject.TypeNames.Insert(0,'IBCLI.License')
        }

        return $ret

    } finally {
        # disconnect if we initiated the connection here
        if ($PSCmdlet.ParameterSetName -eq 'NewStream') {
            Disconnect-IBCLI $ShellStream
        }
    }



    <#
    .SYNOPSIS
        Get the licenses installed on an Infoblox appliance.
 
    .DESCRIPTION
        Runs the 'show license csv' command on the target appliance and returns the parsed result as a set of License objects.
 
    .PARAMETER ComputerName
        Hostname or IP Address of the Infoblox appliance.
 
    .PARAMETER ShellStream
        A Renci.SshNet.ShellStream object that was returned from Connect-IBCLI.
 
    .PARAMETER Credential
        Username and password for the Infoblox appliance.
 
    .PARAMETER Force
        Disable SSH host key checking
 
    .OUTPUTS
        A IBCLI.License object for each license with all of the parsed values returned from the command. Permanent licenses will have Expiration set to DateTime.MaxValue (https://msdn.microsoft.com/en-us/library/system.datetime.maxvalue(v=vs.110).aspx).
            [string] LicenseType
            [string] LicenseString
            [DateTime] Expiration
            [string] HardwareID
 
    .EXAMPLE
        Get-IBCLILicenses -ComputerName 'ns1.example.com' -Credential (Get-Credential)
 
        Get the license objects from the target appliance.
 
    .EXAMPLE
        $ShellStream = Connect-IBCLI -ComputerName 'ns1.example.com' -Credential (Get-Credential)
        PS C:\>Get-IBCLILicenses $ShellStream
 
        Get the license object using an existing ShellStream from the target appliance.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-IBCLI
 
    #>

}