Created
February 26, 2019 05:10
-
-
Save russcam/c763ef14fd742811a1ab5846f0e3b895 to your computer and use it in GitHub Desktop.
PowerShell script module for creating and adding guest users to Azure AD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-Module -Name GuestUsers -Scriptblock { | |
$modules = Get-Module -ListAvailable AzureAD* | |
if ($null -eq $modules) { | |
Write-Output "Install AzureADPreview module" | |
Install-Module AzureADPreview | |
} | |
elseif (($modules | ?{ $_.Name -eq "AzureAD" }).Count -eq 1) { | |
Write-Output "Uninstall AzureAD module and install AzureADPreview module" | |
Uninstall-Module AzureAD | |
Install-Module AzureADPreview | |
} | |
function New-GuestUser { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string] $DisplayName, | |
[Parameter(Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string] $EmailAddress | |
) | |
[PSCustomObject]@{ | |
PSTypeName = "GuestUser" | |
DisplayName = $DisplayName | |
EmailAddress = $EmailAddress | |
} | |
} | |
function Add-GuestUser { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string] $TenantId, | |
[Parameter(Mandatory=$true)] | |
[string] $AccountName, | |
[Parameter(Mandatory=$true)] | |
[string] $AccountDescription, | |
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | |
[ValidateCount(1, 65535)] | |
[PSTypeName("GuestUser")] | |
[PSCustomObject[]] | |
$User | |
) | |
Begin { | |
$collectedUsers = @() | |
} | |
Process { | |
$collectedUsers += $User | |
} | |
End { | |
Write-Output "Connect to Azure AD with Tenant ID $TenantId" | |
$account = Connect-AzureAD -TenantId $TenantId | |
$adUser = Get-AzureADUser -SearchString $account.Account.Id | |
foreach ($collectedUser in $collectedUsers) { | |
$existingUser = Get-AzureADUser -SearchString $collectedUser.EmailAddress | |
if ($null -eq $existingUser -or $existingUser.UserState -eq "PendingAcceptance") { | |
$messageInfo = New-Object Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo | |
$messageInfo.CustomizedMessageBody = @" | |
Hey $($collectedUser.DisplayName), | |
Please accept this invitation to join the $accountName. This account is used for $accountDescription. | |
Cheers, | |
$($adUser.DisplayName) | |
"@ | |
Write-Output "Send invite to $($collectedUser.EmailAddress)" | |
New-AzureADMSInvitation -InvitedUserEmailAddress $collectedUser.EmailAddress -InvitedUserDisplayName $collectedUser.DisplayName ` | |
-InviteRedirectUrl https://portal.azure.com ` | |
-InvitedUserMessageInfo $messageInfo -SendInvitationMessage $true | Out-Null | |
Write-Output "Invite sent to $($collectedUser.EmailAddress)" | |
} | |
else { | |
Write-Output "User $($collectedUser.DisplayName) ($($collectedUser.EmailAddress)) already exists with state $($existingUser.UserState)" | |
} | |
} | |
} | |
} | |
Export-ModuleMember New-GuestUser,Add-GuestUser | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To install