Public/Get-MsolUserLicense.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-MsolUserLicense {
    <#
.Synopsis
   Takes Microsoft.Online.Administration.User object and provides separated license info
.DESCRIPTION
   Long description
.EXAMPLE
   Get-MsolUser | Get-MsolUserLicense
.EXAMPLE
   $users = Get-MsolUser | Where-Object {$_.IsLicensed -eq $True}
   $users | Get-MsolUserLicense
   $users | Export-CSV C:\Temp\Users.csv
#>

    [CmdletBinding()]
    [Alias()]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        $LicensedUsers
    )

    Begin {
        $collection = @()
        $skuPartNumbers = (Get-MsolAccountSku).SkuPartNumber
    }
    Process {
        foreach ($l in $licensedUsers) {
            $userSkus = $l.Licenses.AccountSku.SkuPartNumber
            $obj = [pscustomobject]@{
                DisplayName       = $l.DisplayName
                UserPrincipalName = $l.UserPrincipalName
                SignInBlocked     = $l.BlockCredential
            }
            foreach ($skuPN in $skuPartNumbers) {
                $present = ""
                if ($skuPN -in $userSkus) {
                    $present = "X"
                }
                Write-Debug $skuPN
                Write-Verbose -Message "Adding license $365SkuTable[$skuPn] for user $($l.DisplayName)"
                $obj | Add-Member -Name $365SkuTable[$skuPN] -MemberType NoteProperty -Value $present
            }
            $obj
        }
    }
    End {
        $collection
    }
}