PowerShell 示例
可通过已存储在 .csv 文件中的电子邮件地址批量邀请外部用户加入组织。
准备 .csv 文件
创建新的 .csv 文件并将其命名为 invitations.csv。 在此示例中,该文件保存在 C:\data 中,并包含以下信息:
名称 |
InvitedUserEmailAddress |
Gmail B2B 被邀请者 |
b2binvitee@gmail.com |
Outlook B2B 被邀请者 |
b2binvitee@outlook.com |
获取最新的 Microsoft Graph PowerShell
若要使用新的 cmdlet,必须安装更新的 Microsoft Graph PowerShell 模块。 有关详细信息,请参阅安装 Microsoft Graph PowerShell SDK
登录到租户
Connect-MgGraph -Environment China -ClientId 'YOUR_CLIENT_ID' -TenantId 'YOUR_TENANT_ID' -Scopes "User.Invite.All"
运行 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 的其他功能包括:
- 电子邮件中的自定义文本
- 包括受邀用户的显示名称
- 将邮件发送给抄送人或完全禁止电子邮件
代码示例
此代码示例演示如何调用邀请 API 和获取兑换 URL。 使用兑换 URL 发送自定义邀请电子邮件。 可使用 HTTP 客户端撰写电子邮件,以便自定义其外观并通过 Microsoft Graph API 发送。
POST https://microsoftgraph.chinacloudapi.cn/v1.0/invitations
Content-type: application/json
{
"invitedUserEmailAddress": "david@fabrikam.com",
"invitedUserDisplayName": "David",
"inviteRedirectUrl": "https://myapp.contoso.com",
"sendInvitationMessage": true
}
using System;
using System.Threading.Tasks;
using Microsoft.Graph;
using Azure.Identity;
namespace SampleInviteApp
{
class Program
{
/// <summary>
/// This is the tenant ID of the tenant you want to invite users to.
/// </summary>
private static readonly string TenantID = "";
/// <summary>
/// This is the application id of the application that is registered in the above tenant.
/// </summary>
private static readonly string TestAppClientId = "";
/// <summary>
/// Client secret of the application.
/// </summary>
private static readonly string TestAppClientSecret = @"";
/// <summary>
/// This is the email address of the user you want to invite.
/// </summary>
private static readonly string InvitedUserEmailAddress = @"";
/// <summary>
/// This is the display name of the user you want to invite.
/// </summary>
private static readonly string InvitedUserDisplayName = @"";
/// <summary>
/// Main method.
/// </summary>
/// <param name="args">Optional arguments</param>
static async Task Main(string[] args)
{
string InviteRedeemUrl = await SendInvitation();
}
/// <summary>
/// Send the guest user invite request.
/// </summary>
private static async string SendInvitation()
{
/// Get the access token for our application to talk to Microsoft Graph.
var scopes = new[] { "https://microsoftgraph.chinacloudapi.cn/.default" };
var clientSecretCredential = new ClientSecretCredential(TenantID, TestAppClientId, TestAppClientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// Create the invitation object.
var invitation = new Invitation
{
InvitedUserEmailAddress = InvitedUserEmailAddress,
InvitedUserDisplayName = InvitedUserDisplayName,
InviteRedirectUrl = "https://www.microsoft.com",
SendInvitationMessage = true
};
// Send the invitation
var GraphResponse = await graphClient.Invitations
.Request()
.AddAsync(invitation);
// Return the invite redeem URL
return GraphResponse.InviteRedeemUrl;
}
}
}
安装以下 npm 包:
npm install express
npm install isomorphic-fetch
npm install @azure/identity
npm install @microsoft/microsoft-graph-client
const express = require('express')
const app = express()
const { Client } = require("@microsoft/microsoft-graph-client");
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const { ClientSecretCredential } = require("@azure/identity");
require("isomorphic-fetch");
// This is the application id of the application that is registered in the above tenant.
const CLIENT_ID = ""
// Client secret of the application.
const CLIENT_SECRET = ""
// This is the tenant ID of the tenant you want to invite users to. For example fabrikam.partner.onmschina.cn
const TENANT_ID = ""
async function sendInvite() {
// Initialize a confidential client application. For more info, visit: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-a-service-principal-with-a-client-secret
const credential = new ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET);
// Initialize the Microsoft Graph authentication provider. For more info, visit: https://learn.microsoft.com/graph/sdks/choose-authentication-providers?tabs=Javascript#using--for-server-side-applications
const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: ['https://microsoftgraph.chinacloudapi.cn/.default'] });
// Create MS Graph client instance. For more info, visit: https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md
const client = Client.initWithMiddleware({
debugLogging: true,
authProvider,
});
// Create invitation object
const invitation = {
invitedUserEmailAddress: 'david@fabrikam.com',
invitedUserDisplayName: 'David',
inviteRedirectUrl: 'https://www.microsoft.com',
sendInvitationMessage: true
};
// Execute the MS Graph command. For more information, visit: https://learn.microsoft.com/graph/api/invitation-post
graphResponse = await client.api('/invitations')
.post(invitation);
// Return the invite redeem URL
return graphResponse.inviteRedeemUrl
}
const inviteRedeemUrl = await sendInvite();