Advertisement
Guest User

Azure AD B2B custom email invite from CSV script

a guest
Aug 17th, 2017
3,638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #From https://eschrader.com/2017/06/12/sharepoint-online-azure-ad-b2b-custom-email-invites-for-users-using-powershell/
  2. #1.) Install Azure AD PS module – https://www.powershellgallery.com/packages/AzureADPreview
  3. #Install-Module -Name AzureADPreview
  4.  
  5. #2.) provide O365 tenant admin cred
  6.  
  7. $cred = Get-Credential
  8.  
  9. Connect-AzureAD -Credential $cred
  10.  
  11. #2.a) second cred for O365 email account (merge var with above if for non-demo O365 tenant)
  12.  
  13. $adminemailcred = get-credential
  14.  
  15.  
  16. #2.b) External User Security Group ID
  17. #get-azureadgroup | where-object {$_.DisplayName -ilike "External Users"}
  18. $groupID = "c9a04711-e307-4370-af42-f48db58f80c5"
  19.  
  20. #3.) import CSV, update url and csv location below.
  21.  
  22. $invitations = import-csv C:\azure_ad_b2b.csv
  23.  
  24. foreach ($email in $invitations) {
  25.     #loop over each user in the CSV and create an invite for that user but does not email the user
  26.     $result= New-AzureADMSInvitation -InvitedUserEmailAddress $email.InvitedUserEmailAddress -InvitedUserDisplayName $email.Name -InviteRedirectUrl https://eschrader.sharepoint.com/sites/extranet -InvitedUserMessageInfo $messageInfo -SendInvitationMessage $false
  27.     $inviteurl = $result.InviteRedeemUrl
  28.     $userid = $result.InvitedUser.Id
  29.  
  30.     #automatically add the new user to your Security Group
  31.     Add-AzureADGroupMember -ObjectId $groupID -RefObjectId $userid
  32.  
  33.     #send the user a custom email from your Office 365 tenant. Supports HTML.
  34.    
  35.    
  36.     #Configuration variables for Email
  37.     $EmailTo = $result.InvitedUserEmailAddress
  38.     $EmailFrom = "Eric Schrader <eric@eschradrr.onmicrosoft.com>"
  39.     $EmailSubject = "INVITE: Eschrader Extranet"
  40.     $SupportEmail = "support@eschradrrr.com"
  41.     $SmtpServer = "smtp.office365.com"
  42.     $InviteUrl = $inviteurl
  43.     $DisplayName = $result.Name
  44.  
  45.     #HTML Template
  46.     $EmailBody = @"
  47.  
  48.     <header>
  49.         <img src="https://ericschrader.files.wordpress.com/2017/05/sileo-logo.png" />
  50.     </header>
  51.  
  52.     <div>
  53.       <p style="margin-top:20px;">Congrats $DisplayName,</p>
  54.  
  55.       <p>
  56.         You have been invited to access the Eschrader extranet.
  57.       </p>
  58.       <p>
  59.         <div><a href="$InviteUrl">Sign Up</a></div>
  60.       </p>
  61.       <p>
  62.         If you need further assistance, please contact us <a href="mailto:$SupportEmail">$SupportEmail</a>
  63.       </p>
  64.     </div>
  65.  
  66.     "@
  67.  
  68.     Send-MailMessage -To $EmailTo -from $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHtml -smtpserver $SmtpServer -usessl -Credential $adminemailcred -Port 587
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement