|
| 1 | +<# |
| 2 | +Copyright 2017 Ryan Butler |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +#> |
| 16 | + |
| 17 | +function Get-NSLicenseExpiration { |
| 18 | + <# |
| 19 | + .SYNOPSIS |
| 20 | + Grabs Netscaler license expiration information via REST |
| 21 | + .DESCRIPTION |
| 22 | + Grabs Netscaler license expiration information via REST. |
| 23 | + .PARAMETER Session |
| 24 | + An existing custom NetScaler Web Request Session object returned by Connect-NSAppliance |
| 25 | + .EXAMPLE |
| 26 | + Get-NSlicenseExpiration -Session $Session |
| 27 | + .NOTES |
| 28 | + Author: Ryan Butler - @ryan_c_butler |
| 29 | + Date Created: 09-07-2017 |
| 30 | + #> |
| 31 | + [CmdletBinding()] |
| 32 | + param ( |
| 33 | + $Session = $Script:Session |
| 34 | + ) |
| 35 | + |
| 36 | + begin{ |
| 37 | + _AssertSessionActive |
| 38 | + # Grabs current time from Netscaler |
| 39 | + $currentnstime = Get-NSCurrentTime -Session $Session |
| 40 | + $results = @() |
| 41 | + } |
| 42 | + |
| 43 | + process { |
| 44 | + try { |
| 45 | + $lics = Get-NSSystemFile -Session $Session -Filelocation '/nsconfig/license' | Where-Object {$_.filename -like '*.lic'} |
| 46 | + } catch{ |
| 47 | + throw 'Error reading license(s)' |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($lic in $lics) { |
| 51 | + Write-verbose "Reading [$($lic.filename)]" |
| 52 | + |
| 53 | + # Converts returned value from BASE64 to UTF8 |
| 54 | + $lic = Get-NSSystemFile -Session $Session -Filelocation '/nsconfig/license' -Filename $lic.filename |
| 55 | + $info = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($lic.filecontent)) |
| 56 | + |
| 57 | + # Grabs needed line that has licensing information |
| 58 | + $lines = $info.Split("`n") | Where-Object {$_ -like '*INCREMENT*'} |
| 59 | + |
| 60 | + # Parses needed date values from string |
| 61 | + $licdates = @() |
| 62 | + foreach ($line in $lines) { |
| 63 | + $licdate = $line.Split() |
| 64 | + |
| 65 | + if ($licdate[4] -like 'permanent') { |
| 66 | + $expire = 'PERMANENT' |
| 67 | + } else { |
| 68 | + $expire = [datetime]$licdate[4] |
| 69 | + } |
| 70 | + |
| 71 | + # Adds date to object |
| 72 | + $temp = [pscustomobject]@{ |
| 73 | + expdate = $expire |
| 74 | + feature = $licdate[1] |
| 75 | + } |
| 76 | + $licdates += $temp |
| 77 | + } |
| 78 | + |
| 79 | + foreach ($date in $licdates) { |
| 80 | + if ($date.expdate -like 'PERMANENT') { |
| 81 | + $expires = 'PERMANENT' |
| 82 | + $span = '9999' |
| 83 | + } else { |
| 84 | + $expires = ($date.expdate).ToShortDateString() |
| 85 | + $span = (New-TimeSpan -Start $currentnstime -End $date.expdate).Days |
| 86 | + } |
| 87 | + |
| 88 | + $temp = [pscustomobject]@{ |
| 89 | + Expires = $expires |
| 90 | + Feature = $date.feature |
| 91 | + DaysLeft = $span |
| 92 | + LicenseFile = $lic.filename |
| 93 | + } |
| 94 | + $results += $temp |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + $results |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments