﻿# 批次更換 Microsoft 365 授權的 PowerShell 腳本範例

這幾天公司的 Office 365 授權要到期了，必須全員更換到 Microsoft 365 授權，由於現在人比較多，連在 [Microsoft 365 admin center](https://admin.microsoft.com/) 想**全選**並**批次套用變更**都沒辦法，而且一個人一個人檢查現有授權也很麻煩，所以打算寫 PowerShell 來完成這個工作。

1.  首先，我們要先取得已經購買的授權清單
    
    ```bash
    (Get-AzureADSubscribedSku).SkuPartNumber
    ```
    
    這裡會列出許多 SKU 的 Part Number (零件編號)，簡單來說這就是微軟公司內部的產品名稱，PowerShell 必須使用這組名稱才能做設定。
    
    ```
    STREAM
    EMSPREMIUM
    DEVELOPERPACK
    POWERAPPS_INDIVIDUAL_USER
    POWER_BI_PRO
    AAD_BASIC
    WINDOWS_STORE
    ENTERPRISEPACK
    FLOW_FREE
    CCIBOTS_PRIVPREV_VIRAL
    POWERAPPS_VIRAL
    POWER_BI_STANDARD
    EMS
    SPE_E3
    RIGHTSMANAGEMENT_ADHOC
    ```
    
    這裡 `ENTERPRISEPACK` 就是 **Office 365 E3** 授權的 SKU 名稱，而 `SPE_E3` 就是 **Microsoft 365 E3** 的 SKU 名稱！
    
2.  列出特定一位使用者目前設定好的授權清單
    
    ```powershell
    $userUPN="username@miniasp.com"
    $userList = Get-AzureADUser -ObjectID $userUPN | Select -ExpandProperty AssignedLicenses | Select SkuID
    $userList | ForEach { $sku=$_.SkuId ; $licensePlanList | ForEach { If ( $sku -eq $_.ObjectId.substring($_.ObjectId.length - 36, 36) ) { Write-Host $_.SkuPartNumber } } }
    ```
    
3.  設定使用者授權
    
    接著就是取得全公司所有帳號，去掉 `Guest` 訪客帳號，經過一連串比對就可以決定該要怎樣調整使用者授權，最後我用 [Set-MsolUserLicense](https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users.actions/set-mguserlicense) 更新使用者的授權設定。
    
    ```powershell
    $AADName = 'miniasp'
    
    $users = Get-AzureADUser | Where-Object { $_.UserType -ne 'Guest' } | Select-Object UserPrincipalName
    
    foreach ($user in $users)
    {
        $userUPN = $user.UserPrincipalName
    
        Write-Host "############################# $userUPN #############################"
    
        $is_old_license=$false
        $is_new_license=$false
    
        $userList = Get-AzureADUser -ObjectID $userUPN | Select -ExpandProperty AssignedLicenses | Select SkuID
        $userList | ForEach {
            $sku=$_.SkuId
            $licensePlanList | ForEach {
                If ( $sku -eq $_.ObjectId.substring($_.ObjectId.length - 36, 36) ) {
                    If ( $_.SkuPartNumber -eq 'ENTERPRISEPACK' ) { $is_old_license = $true; Write-Host "Old License:" $_.SkuPartNumber }
                    If ( $_.SkuPartNumber -eq 'SPE_E3' )         { $is_new_license = $true; Write-Host "New License:" $_.SkuPartNumber }
                }
            }
        }
    
        if ( $is_old_license -and $is_new_license ) {
            # 兩個授權都有，只要刪除舊的授權
            Write-Host "The 'ENTERPRISEPACK' license should be removed."
            Set-MsolUserLicense -UserPrincipalName $userUPN -RemoveLicenses '${$AADName}:ENTERPRISEPACK'
        }
        elseif ( $is_old_license -and (-not $is_new_license)) {
            # 只有舊的授權，就置換成新的授權
            Write-Host "The 'ENTERPRISEPACK' license should be replaced with 'SPE_E3' license."
            Set-MsolUserLicense -UserPrincipalName $userUPN -AddLicenses '${$AADName}:SPE_E3' -RemoveLicenses '${$AADName}:ENTERPRISEPACK'
        }
        elseif ( (-not $is_old_license) -and ($is_new_license)) {
            # 只有新的授權，就代表設定正確！
            Write-Host "The new License 'SPE_E3' is applied correctly."
        }
        elseif ( (-not $is_old_license) -and (-not $is_new_license)) {
            # 兩個授權都沒有，基本上就不特別設定
            Write-Host "No License is available on this user."
        }
    }
    ```
    

### 相關連結

-   [View Microsoft 365 account license and service details with PowerShell - Microsoft 365 Enterprise | Microsoft Docs](https://learn.microsoft.com/en-us/microsoft-365/enterprise/view-account-license-and-service-details-with-microsoft-365-powershell?view=o365-worldwide&WT.mc_id=DT-MVP-4015686)
-   [Get-AzureADUser (AzureAD) | Microsoft Docs](https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0&WT.mc_id=DT-MVP-4015686)
-   [Set-MsolUserLicense (MSOnline) | Microsoft Docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users.actions/set-mguserlicense)
