Microsoft Entra B2B 共同作業程序代碼和 PowerShell 範例

PowerShell 範例

您可以從儲存在.csv檔案中的電子郵件位址,大量邀請外部用戶到組織。

  1. 準備.csv檔案

    建立新的.csv檔案,並將其命名為invitations.csv。 在此範例中,檔案會儲存在 C:\data 中,並包含下列資訊:

    名稱 InvitedUserEmailAddress
    Gmail B2B 受邀者 b2binvitee@gmail.com
    Outlook B2B 受邀者 b2binvitee@outlook.com
  2. 取得最新的 Microsoft Graph PowerShell

    若要使用新的 Cmdlet,您必須安裝更新的 Microsoft Graph PowerShell 模組。 如需詳細資訊,請參閱 安裝 Microsoft Graph PowerShell SDK

  3. 登入您的租用

    Connect-MgGraph -Scopes "User.Invite.All"
    
  4. 執行 PowerShell Cmdlet

    $invitations = import-csv C:\data\invitations.csv
    $messageInfo = New-Object Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo
    $messageInfo.customizedMessageBody = "Hey there! Check this out. I created an invitation through PowerShell"
    foreach ($email in $invitations) {
       New-MgInvitation -InviteRedirectUrl "https://wingtiptoysonline-dev-ed.my.woodgrove.com" `
          -InvitedUserDisplayName $email.Name -InvitedUserEmailAddress $email.InvitedUserEmailAddress `
          -InvitedUserMessageInfo $messageInfo -SendInvitationMessage:$true
    }
    

此 Cmdlet 會將邀請傳送至invitations.csv中的電子郵件位址。 此 Cmdlet 的更多功能包括:

  • 電子郵件訊息中的自定義文字
  • 包含受邀用戶的顯示名稱
  • 將訊息傳送至 CC 或完全隱藏電子郵件訊息

程式碼範例

程式代碼範例說明如何呼叫邀請 API 並取得兌換 URL。 使用兌換 URL 來傳送自訂邀請電子郵件。 您可以使用 HTTP 用戶端撰寫電子郵件,以便自訂其外觀,並透過 Microsoft Graph API 傳送電子郵件。

POST https://graph.microsoft.com/v1.0/invitations
Content-type: application/json
{
  "invitedUserEmailAddress": "david@fabrikam.com",
  "invitedUserDisplayName": "David",
  "inviteRedirectUrl": "https://myapp.contoso.com",
  "sendInvitationMessage": true
}

下一步