Azure Active Directory V2 PowerShell - 查找所有许可的Office 365用户

时间:2017-09-04 10:40:37

标签: powershell azure-active-directory export-to-csv

我正在尝试查找已为其分配特定许可类型的所有用户。 我基本上试图将我的Azure模块v1命令转换为Azure模块v2命令。如何获得与Azure模块v1命令相同的结果?

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,Azure AD V2 powershell中没有isLicensed属性。我找到AssignedLicenses属性而不是这个。但我不确定。

    $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 个答案:

答案 0 :(得分:1)

通过Azure AD V2 PowerShell检查用户是否为许可证的另一种简单方法是检查AssignedLicenses的计数,而不是检查它是否为空。

此属性是一个数组,并且如juunas所述,此属性不可为空。您可以参考下面的代码来修改这段代码:

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