PowerShell 예제
.csv 파일에 저장하는 전자 메일 주소에서 외부 사용자를 조직에 대량 초대할 수 있습니다.
.csv 파일 준비
새 .csv 파일을 만들고 invitations.csv로 이름을 지정하십시오. 이 예제에서 파일은 C:\data에 저장되며 다음 정보를 포함합니다.
| 이름 |
초대된사용자이메일주소 |
| Gmail B2B 초대자 |
b2binvitee@gmail.com |
| Outlook B2B 초대자 |
b2binvitee@outlook.com |
최신 Microsoft Graph PowerShell 가져오기
새 cmdlet을 사용하려면 업데이트된 Microsoft Graph PowerShell 모듈을 설치해야 합니다. 자세한 내용은 Microsoft Graph PowerShell SDK 설치 참조하세요.
테넌시 계정에 로그인
Connect-MgGraph -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의 추가 기능은 다음과 같습니다.
- 전자 메일 메시지의 사용자 지정된 텍스트
- 초대된 사용자의 표시 이름 포함
- 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
}
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://graph.microsoft.com/.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.onmicrosoft.com
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://graph.microsoft.com/.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();
다음 단계