Microsoft Graph PowerShell 設定M365的使用者

發現M365 powershell 又停用了一些,其實真的好煩,網頁界面只能一次更新40位,沒辦法就要繼續學習,看了一下docs,powershell 轉移到Graph,例如之前能用的Set-MsolUserLicense 已經失效,需要使用microsoft graph 來更新。假如你不懂得,可以考慮使用Entra ID 來更新用戶

在 Azure 入口網站 中大量建立使用者 – Microsoft Entra ID | Microsoft Learn

安裝Graph PowerShell

Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery -Force

假如想指定某些 版本也可以使用

Install-Module -Name Microsoft.Graph -Scope CurrentUser -RequiredVersion 2.5.0

成功後就要開始登入進行授權,一般是使用 Connect-MgGraph,不過這樣直接登入只是查詢,基本權限很低,所以要登入並要求更多權限

Connect-MgGraph -Scopes "User.ReadWrite.All","Organization.Read.All","Team.ReadBasic.All"

這樣就成功了,接下來就是更新授權了,我看了一些外國的代碼,高手都寫成script,不過我一年只做一次就不折騰了,還是用csv 更新。有一些欄目名不太一樣,例如UserPrincipalName 換成了UserId,我懶得換原來的csv titile ,就只是命令那邊更換了UserId,csv 那邊title 就繼續使用UserPrincipalName ,在Grphy 這邊可以先看看一個用戶的授權碼,命令如下:

Get-MgUserLicenseDetail -UserId davidchew@contoso.com

我這次因為要批量去刪除授權,所以就想看了一下賬號有什麼授權,然後就讀取SkuId,然後就只根據csv 的賬號列表去刪除Skuid 資料

Import-Csv C:\rmlic.csv  | ForEach-Object {Set-MgUserLicense -UserId $_.UserPrincipalName -RemoveLicenses "Sukid號碼" -AddLicenses @()}

假如想增加授權就是RemoveLicenses 改成AddLicenses ,後面-AddLicenses @() 改成 –RemoveLicenses @() 就可以了,最後登出Disconnect-MgGraph

可以參考一下外國資料,很詳細,下面是我看的文章:

Manage Microsoft 365 Licenses with the Microsoft Graph PowerShell SDK (practical365.com)

Microsoft Graph PowerShell SDK: User and Group Operations (practical365.com)

還有寫好的Script

GitHub – 12Knocksinna/Office365itpros: Office 365 for IT Pros PowerShell examples

群組

Manage security groups with PowerShell – Microsoft 365 Enterprise | Microsoft Learn

賬號

Create Microsoft 365 user accounts with PowerShell – Microsoft 365 Enterprise | Microsoft Learn

作者

Rockfu

由小學年代已經沉迷電腦,喜歡分享電腦相關資訊。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。

This site uses Akismet to reduce spam. Learn how your comment data is processed.