Azure Active Directory V2 PowerShell - Finding all licenced Office 365 users

孤者浪人 提交于 2019-12-24 10:05:08

问题


I am trying to find all users of which have been assigned a specific license type. I am basically trying to convert my Azure module v1 commands to Azure module v2 commands. how to get the same result as Azure module v1 commands ?

Azure V1:

$OutputFile = "C:\Export\O365LicensedADEnabledUsers.csv"
$T1 = @()
$O365Users = Get-MsolUser -All
ForEach ($O365User in $O365Users)
{
    $ADuser = Get-ADUser -Filter { UserPrincipalName -eq $O365User.UserPrincipalName } -Properties whenCreated, Department, Company, Enabled
    If (($ADUser.Enabled -eq $true) -and ($O365User.isLicensed -eq $true))
    {
        $T1 += New-Object psobject -Property @{
            CollectDate = $(Get-Date);
            ADUserUPN = $($ADUser.UserPrincipalName);
            O365UserUPN = $($O365User.UserPrincipalName);
            ADUserCreated = $($ADUser.whenCreated);
            ADUserDepartment = $($ADUser.Department);
            ADUserCompany = $($ADUser.Company);
            ADUserEnabled = $($ADUser.Enabled);
            O365Licensed = $($O365User.isLicensed)
        }
    }
}
$T1 = $T1 | Sort-Object -Property ADUserCreated
$T1 | Format-Table
$T1 | Export-Csv -Path $OutputFile -NoTypeInformation
Write-Host "Output to $OutputFile"

Azure V2 :

AFAIK , there is no isLicensed property in Azure AD V2 powershell. I find AssignedLicenses property instead of this. but I am not sure.

    $OutputFile = "C:\Export\O365LicensedADEnabledUsers.csv"
    $T1 = @()
    $O365Users = Get-AzureADUser -All $true
    ForEach ($O365User in $O365Users)
    {
        $ADuser = Get-ADUser -Filter { UserPrincipalName -eq $O365User.UserPrincipalName } -Properties whenCreated, Department, Company, Enabled
        If (($ADUser.Enabled -eq $true) -and ($O365User.AssignedLicenses -ne $null))
        {
            $T1 += New-Object psobject -Property @{
                CollectDate = $(Get-Date);
                ADUserUPN = $($ADUser.UserPrincipalName);
                O365UserUPN = $($O365User.UserPrincipalName);
                ADUserCreated = $($ADUser.whenCreated);
                ADUserDepartment = $($ADUser.Department);
                ADUserCompany = $($ADUser.Company);
                ADUserEnabled = $($ADUser.Enabled);
                O365Licensed = $($O365User.AssignedLicenses)
            }
        }
    }
    $T1 = $T1 | Sort-Object -Property ADUserCreated
    $T1 | Format-Table
    $T1 | Export-Csv -Path $OutputFile -NoTypeInformation
    Write-Host "Output to $OutputFile"

回答1:


Another easy way to check the whether user is licences via Azure AD V2 PowerShell is check the count of AssignedLicenses instead of check whether it is null.

This property is a array and as juunas mentioned, this property is not null-able. You can refer the code below to modify the piece of code:

 If (($ADUser.Enabled -eq $true) -and ($O365User.AssignedLicenses.Count -ne 0))


来源:https://stackoverflow.com/questions/46035299/azure-active-directory-v2-powershell-finding-all-licenced-office-365-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!