*Requires O365 admin account with Licensing permission*
PS:
[string]$DateTime = (Get-Date).Tostring("dd-MM-yyyy_HHmmss")
[string]$FilePath = "C:\Temp\"
[string]$Filename1 = "MSOL_LicenseCheck"
[string]$OutputFilename1 = [string]::Format($FilePath + $Filename1 + "_{0}.csv",$DateTime)
[string]$AccountSkuID = "#######"
$msolUsername = "########@########.onmicrosoft.com"
$msolpassword = Get-Content "C:\TEMP\EncryptedPasswords\########.txt" | ConvertTo-SecureString
$msolcredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $msolUsername, $msolpassword
Write-Progress -Activity "Connecting to MSOL" -Status "Working..."
Connect-MsolService -Credential $msolcredential -WarningAction SilentlyContinue | Out-Null
Write-Progress -Activity "Connecting to MSOL" -Status "Working..." -Completed
Write-Progress -Activity "Getting MSOL users" -Status "Working..."
$msolusers = Get-MSOLUser -ALL
Write-Progress -Activity "Getting MSOL users" -Status "Working..." -Completed
[INT]$c = 0
[INT]$msoluserscount = ($msolusers).count
foreach($msoluser in $msolusers){
$c++
Write-Progress -Activity "Examining license status" -Status "$c of $msoluserscount" -PercentComplete ($c/$msoluserscount * 100)
$i = New-Object -TypeName PSObject
$i | Add-Member -MemberType NoteProperty -Name UserPrincipalName -Value $msoluser.UserPrincipalName
if ($msoluser.isLicensed -eq $false){
$i | Add-Member -MemberType NoteProperty -Name isLicensed -Value "False"
$i | Add-Member -MemberType NoteProperty -Name HasPlan -Value "False"
$i | Add-Member -MemberType NoteProperty -Name EXCHANGE_S_ENTERPRISE -Value "NA"
$i | Add-Member -MemberType NoteProperty -Name MCOSTANDARD -Value "NA"
}
if ($msoluser.isLicensed -eq $true -and $msoluser.licenses.AccountSkuId -notcontains $AccountSkuID){
$i | Add-Member -MemberType NoteProperty -Name isLicensed -Value "True"
$i | Add-Member -MemberType NoteProperty -Name HasPlan -Value "False"
$i | Add-Member -MemberType NoteProperty -Name EXCHANGE_S_ENTERPRISE -Value "NA"
$i | Add-Member -MemberType NoteProperty -Name MCOSTANDARD -Value "NA"
}
if ($msoluser.isLicensed -eq $true -and $msoluser.licenses.AccountSkuId -contains $AccountSkuID){
$i | Add-Member -MemberType NoteProperty -Name isLicensed -Value "True"
$i | Add-Member -MemberType NoteProperty -Name HasPlan -Value "True"
foreach($msoluserlicense in ($msoluser.licenses | ?{$_.accountskuid -eq $AccountSkuID})){
[INT]$servicestatuscount = ($msoluserlicense.servicestatus).count
0..($servicestatuscount - 1)| ForEach-Object{
if(($msoluserlicense.servicestatus[$_]).ServicePlan.ServiceName -eq "EXCHANGE_S_ENTERPRISE"){
[string]$status = ($msoluserlicense.servicestatus[$_]).provisioningstatus
$i | Add-Member -MemberType NoteProperty -Name EXCHANGE_S_ENTERPRISE -Value $status
}
if(($msoluserlicense.servicestatus[$_]).ServicePlan.ServiceName -eq "MCOSTANDARD"){
[string]$status = ($msoluserlicense.servicestatus[$_]).provisioningstatus
$i | Add-Member -MemberType NoteProperty -Name MCOSTANDARD -Value $status
}
}
}
}
$i | export-csv $outputfilename1 -NoTypeInformation -Append -Force
}