-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdd-AzureADTestUser.ps1
More file actions
108 lines (95 loc) · 3.35 KB
/
Copy pathAdd-AzureADTestUser.ps1
File metadata and controls
108 lines (95 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Function Add-AzureADTestUser
{
<#
.SYNOPSIS
Add test users to Azure Active Directory.
.DESCRIPTION
This cmdlet uses the AzureADPreview module to add test users to Azure Active Directory. All that you need to provide is the user's name and domain, plain text password is optional.
.PARAMETER Domain
Domain name for the user. This needs to be a verified domain on Azure Active Directory. . Required
.PARAMETER Names
Full name(s) of the test users to add to Azure Active Directory. Required
.PARAMETER Password
This password will be used to create usre(S). Note that this will not be a temporary password and will not need to be reset.
.NOTES
Author : Hannel Hazeley - hhazeley@outlook.com
.LINK
https://github.com/hhazeley/HannelsToolBox/blob/master/Functions/Add-AzureADTestUser.ps1
.EXAMPLE
Add-AzureADTestUser -domain azure.hazelnest.com -names "John Jones","Alex A. Smith"
This will add 2 user to Azure Active Directory and generated random password for each user that does not need to be reset.
.EXAMPLE
Add-AzureADTestUser -domain azure.hazelnest.com -names "Clark Kent","John B. Smith" -Password Str0ngP@55word13!
This will add 2 user to Azure Active Directory and use provided password for both users, password does not need to be reset.
#>
[cmdletbinding()]
Param (
[Parameter(Mandatory=$true)]
$Domain,
[Parameter(Mandatory=$true)]
$Names,
$Password
)
$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
#Function for error checks
Function ErrorCheck{
If ($errorck -ne $null)
{
Write-host
Write-host -ForegroundColor Red "ERROR: " -NoNewline
Write-Host -ForegroundColor Red $errorck
Write-host
Break
}
}
$DomainVerification = Get-AzureADDomain -Name $domain -ErrorVariable errorck
ErrorCheck
If ($DomainVerification.IsVerified -eq $false)
{
Write-host
Write-host -ForegroundColor Red "ERROR: " -NoNewline
Write-Host -ForegroundColor Red "Domain" $Domainverification.Name "is not verified and cannot be used. Please complete Domain verification and try again."
Write-host
Break
}
If ($Password -eq $null)
{
[Switch]$GeneratePassword = $true
}
Write-Host
foreach ($name in $names)
{
$fullname = $name.Split(' ')
$givenname = $fullname[0]
$surnname = $fullname[-1]
$mailnickname = $givenname.ToCharArray()[0] + $surnname
$mailnickname = $mailnickname.ToLower()
$count = (get-azureadUser | ?{$_.UserPrincipalName -like "$mailnickname`@*"}).count
If ($count -ge 1)
{
$n = 1
Do {
$mnname = $mailnickname+$n++
$count = (get-azureadUser | ?{$_.UserPrincipalName -like "$mnname`@*"}).count
}
While ($count -ne 0)
$mailnickname = $mnname
}
$upn = $mailnickname +"@"+ $domain
$upn = $upn.ToLower()
If ($GeneratePassword.IsPresent)
{
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$Password = [System.Web.Security.Membership]::GeneratePassword(12,2)
}
$uPassword = New-Object "Microsoft.Open.AzureAD.Model.PasswordProfile"
$upassword.ForceChangePasswordNextLogin = $false
$upassword.Password = $Password
$hout = New-AzureADUser -AccountEnabled $true -DisplayName $name -PasswordProfile $upassword -GivenName $givenname -Surname $surnname -UserPrincipalName $upn -MailNickName $mailnickname -ErrorVariable errorck
ErrorCheck
Write-Host -ForegroundColor Green "User $name created, UPN: $upn Password: $Password."
}
Write-Host
}
Export-ModuleMember -Function Add-AzureADTestUser