Azure Active Directory B2B 共同作業程式碼與 PowerShell 範例

PowerShell 範例

您可以從您儲存在 .CSV 檔案中的電子郵件地址大量邀請外部使用者。

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

    名稱 InvitedUserEmailAddress
    Gmail B2B 受邀者 b2binvitee@gmail.com
    Outlook B2B 受邀者 b2binvitee@outlook.com
  2. 取得最新的 Azure AD PowerShell。若要使用新的 Cmdlet,您必須安裝已更新的 Azure AD PowerShell 模組 (可從 PowerShell 模組的發行頁面下載)

  3. 登入您的租用戶

    $cred = Get-Credential
    Connect-AzureAD -Credential $cred
    
  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-AzureADMSInvitation -InvitedUserEmailAddress $email.InvitedUserEmailAddress -InvitedUserDisplayName $email.Name -InviteRedirectUrl https://wingtiptoysonline-dev-ed.my.salesforce.com -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
}

後續步驟