Tạo report user trên Ms365: lấy capacity, last login

Bước 1: chuẩn bị full data

Kết nối Ms365 để lấy data:

#Connect-MgGraph -Scopes "User.Read.All","Directory.Read.All","Reports.Read.All" 

Lấy report danh sách user theo GUID (mặc định) 

$mailReport = "$env:TEMP\mailUsage-guid.csv"
Get-MgReportMailboxUsageDetail -Period "D7" -OutFile $mailReport 
#D7=7 ngày
$mailUsage = Import-Csv $mailReport

Lấy report danh sách user theo UPN: vào admin center -> settings -> Org settings -> reports -> bỏ tích Display concealed user, group, and site names in all reports

$mailReport = "$env:TEMP\mailUsage-upn.csv"
Get-MgReportMailboxUsageDetail -Period "D7" -OutFile $mailReport #D7=7 ngày
$mailUsage = Import-Csv $mailReport 

Trả lại setting report như cũ 

Chạy mapping 2 file trên để lấy được danh sách tương ứng GUID với UPN/Email, lưu lại

$mailGuid = Import-Csv "$env:TEMP\mailUsage-guid.csv"
$mailUpn = Import-Csv "$env:TEMP\mailUsage-upn.csv"

$mapping = for ($i=0; $i -lt $mailGuid.Count; $i++) {
    [PSCustomObject]@{
        ObjectId = $mailGuid[$i].'User Principal Name'
        UPN      = $mailUpn[$i].'User Principal Name'
        Display  = $mailUpn[$i].'Display Name'
    }
}

$mapping | Export-Csv -Path "D:\OneDrive\Downloads\Documents\ReportM365\Mapping_Guid_Upn.csv" -NoTypeInformation -Encoding UTF8 

Xuất UPN + Description + homePhone + LastLogon từ AD on-premise, copy về máy mình. Chú ý sai số 9-14 ngày do các DC replicate:

Get-ADUser -Filter * -Properties `
    Description,
    HomePhone,
    LastLogonTimestamp |
Select-Object `
    UserPrincipalName,
    Description,
    HomePhone,
    @{Name="ADLastLogon";Expression={
        if ($_.LastLogonTimestamp) {
            [DateTime]::FromFileTime($_.LastLogonTimestamp)
        }
    }} |
Export-Csv "C:\Backup\AD_Report.csv" -NoTypeInformation -Encoding UTF8

Bước 2: chạy report

PowerShell admin: lưu ý path tới 2 file Mapping và AD report đã lấy ở trên:

# Connect-MgGraph -Scopes Reports.Read.All,User.Read.All,Directory.Read.All

$skus = Get-MgSubscribedSku | Select-Object SkuId, SkuPartNumber, SkuDisplayName

$mailReport = "$env:TEMP\mailUsage.csv"
Get-MgReportMailboxUsageDetail -Period "D7" -OutFile $mailReport
$mailUsage = Import-Csv $mailReport

$driveReport = "$env:TEMP\driveUsage.csv"
Get-MgReportOneDriveUsageAccountDetail -Period "D7" -OutFile $driveReport
$driveUsage = Import-Csv $driveReport

# Mapping file
$mapping = Import-Csv "D:\OneDrive\Downloads\Documents\ReportM365\Mapping_Guid_Upn.csv"

# AD Report file
$adReport = Import-Csv "D:\OneDrive\Downloads\Documents\ReportM365\AD_Report.csv"

$users = Get-MgUser -All -Property UserPrincipalName,DisplayName,AssignedLicenses,AccountEnabled,OnPremisesDistinguishedName,JobTitle,Department

function Convert-ToGB($bytes) {
    if ([string]::IsNullOrWhiteSpace($bytes)) { return "" }
    try { 
        return [math]::Round([double]$bytes / 1GB, 2) 
    } catch { 
        return "" 
    }
}

$result = foreach ($u in $users) {

    $licenses = ($u.AssignedLicenses | ForEach-Object {
        $lic = $_
        $sku = $skus | Where-Object { $_.SkuId -eq $lic.SkuId }
        if ($sku) { "$($sku.SkuPartNumber) - $($sku.SkuDisplayName)" }
    } | Select-Object -Unique) -join '; '

    $map = $mapping | Where-Object { $_.UPN -eq $u.UserPrincipalName }

    $adInfo = $adReport | Where-Object { $_.UserPrincipalName -eq $u.UserPrincipalName }
    $description = if ($adInfo) { $adInfo.Description } else { "" }
    $homePhone   = if ($adInfo) { $adInfo.homePhone } else { "" }
    $adlastlogon = if ($adInfo) { $adInfo.ADLastLogon } else { "" }

    $mailStorage    = ""
    $mailQuota      = ""
    $mailLastActive = ""
    $driveStorage   = ""
    $driveAllocated = ""

    if ($map) {
        $objId = $map.ObjectId

        $mail = $mailUsage | Where-Object { $_.'User Principal Name' -eq $objId }
        if ($mail) {
            $mailStorage    = Convert-ToGB $mail.'Storage Used (Byte)'
            $mailQuota      = Convert-ToGB $mail.'Issue Warning Quota (Byte)'
            $mailLastActive = $mail.'Last Activity Date'
        }

        $drive = $driveUsage | Where-Object { $_.'Owner Principal Name' -eq $objId }
        if ($drive) {
            $driveStorage   = Convert-ToGB $drive.'Storage Used (Byte)'
            $driveAllocated = Convert-ToGB $drive.'Storage Allocated (Byte)'
        }
    }

    [PSCustomObject]@{
        AccountStatus         = if ($u.AccountEnabled) {"Enabled"} else {"Disabled"}
        UserPrincipalName     = $u.UserPrincipalName
        DisplayName           = $u.DisplayName        
        JobTitle              = $u.JobTitle
        Department            = $u.Department
        Description           = $description
        HomePhone             = $homePhone
        Licenses              = $licenses
        MailUsageGB           = $mailStorage
        MailQuotaGB           = $mailQuota
        MailLastActivity      = $mailLastActive
        ADLastLogon           = $adlastlogon
        OneDriveUsageGB       = $driveStorage
        OneDriveAllocatedGB   = $driveAllocated
        OUPath                = $u.OnPremisesDistinguishedName
    }
}

$result | Export-Csv -Path "D:\OneDrive\Downloads\Documents\ReportM365\UsersLastLogon.csv" -NoTypeInformation -Force -Encoding UTF8

No comments:

Post a Comment

MS 365 chạm giới hạn số lượng xóa user cùng lúc

Để tránh xóa nhầm, trên hệ thống AAD Connect sẽ có giới hạn số lượng user xóa trong cùng 1 lượt:  https://learn.microsoft.com/en-us/entra/id...