functions/license/Get-PSFLicense.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
function Get-PSFLicense
{
<#
 .SYNOPSIS
  Returns registered licenses
  
 .DESCRIPTION
  Returns all matching licenses from the PSFramework internal license cache.
  
 .PARAMETER Filter
  Default: "*"
  Filters for the name of the product. Uses the -like operator.
  
 .PARAMETER ProductType
  Only licenses of products for any of the specified types are considered.
  
 .PARAMETER LicenseType
  Only licenses of any matching type are returned.
  
 .PARAMETER Manufacturer
  Default: "*"
  Only licenses for products of a matching manufacturer are returned. Uses the -like operator for comparisons.
  
 .EXAMPLE
  PS C:\> Get-PSFLicense *Microsoft*
  
  Returns all registered licenses for products with the string "Microsoft" in their name
  
 .EXAMPLE
  PS C:\> Get-PSFLicense -LicenseType Commercial -PoductType Library
  
  Returns a list of all registered licenses for products that have commercial licenses and are libraries.
#>

    
    [CmdletBinding(PositionalBinding = $false)]
    [OutputType([PSFramework.License.License])]
    param (
        [Parameter(Position = 0)]
        [Alias('Product')]
        [String]
        $Filter = "*",
        
        [PSFramework.License.ProductType[]]
        $ProductType,
        
        [PSFramework.License.LicenseType]
        $LicenseType,
        
        [String]
        $Manufacturer = "*"
    )
    
    $licenses = [PSFramework.License.LicenseHost]::Get() | Where-Object { ($_.Product -like $Filter) -and ($_.Manufacturer -like $Manufacturer) }
    if ($PSBoundParameters.ContainsKey("ProductType"))
    {
        $temp = $licenses
        $licenses = @()
        
        :main foreach ($l in $temp)
        {
            foreach ($type in $ProductType)
            {
                if ($l.ProductType -eq $type)
                {
                    $licenses += $l
                    continue main
                }
            }
        }
    }
    
    if ($PSBoundParameters.ContainsKey("LicenseType"))
    {
        $licenses | Where-Object { ($_.LicenseType.ToString() -match $LicenseType.ToString()) -and ($_.Manufacturer -like $Manufacturer) -and ($_.Product -like $Filter) }
    }
    else
    {
        $licenses | Where-Object { ($_.Manufacturer -like $Manufacturer) -and ($_.Product -like $Filter) }
    }
}