-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupUsers.ps1
More file actions
72 lines (58 loc) · 2.81 KB
/
Copy pathSetupUsers.ps1
File metadata and controls
72 lines (58 loc) · 2.81 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
# Sets up a new user
# -Create user in Active Directory
# -Create on-premise Exchange mailbox
# -Migrate a mailbox from on-premise Exchange to Office 365
Import-Module ActiveDirectory
Import-Module MSOnline
$Users = Import-Csv -Path $args[0]
$O365Username = ""
$O365Password = ""
$OnPremUsername = ""
$OnPremPassword = ""
$Domain = ""
$RemoteHostName = ""
$MailboxDatabase = ""
$TargetDeliverySubDomain = ""
$TargetDeliveryDomain = $TargetDeliverySubDomain + ".mail.onmicrosoft.com"
$O365SecurePassword = $O365Password | ConvertTo-SecureString -AsPlainText -Force
$OnPremSecurePassword = $OnPremPassword | ConvertTo-SecureString -AsPlainText -Force
$O365CREDS = New-Object System.Management.Automation.PSCredential($O365Username,$O365SecurePassword)
$OnPremCREDS = New-Object System.Management.Automation.PSCredential($OnPremUsername,$OnPremSecurePassword)
$SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Authentication Basic -AllowRedirection -Credential $O365CREDS
Import-PSSession $SESSION
Connect-MsolService -Credential $O365CREDS
Write-Host "Connected to Office 365"
ForEach($User in $Users) {
$Username = $User.Username
$DisplayName = $User.DisplayName
$UserPrincipalName = $User.UserPrincipalName
$GivenName = $User.GivenName
$Surname = $User.Surname
$Department = $User.Department
$Title = $User.Title
$Company = $User.Company
$Path = $User.Path
$Password = $User.Password
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$ChangePasswordAtLogon = $True
$PasswordNeverExpires = $False
Write-Host "Creating user: $GivenName $Surname"
New-ADUser -Name $DisplayName -UserPrincipalName $UserPrincipalName -SamAccountName $Username -GivenName $GivenName -DisplayName `
$DisplayName -SurName $Surname -Title $Title -Company $Company -Path $Path -AccountPassword $SecurePassword `
-Enabled $True -PasswordNeverExpires $PasswordNeverExpires -ChangePasswordAtLogon $ChangePasswordAtLogon
Write-Host "Creating mailbox for $GivenName $Surname"
Enable-Mailbox $UserPrincipalName -Database $MailboxDatabase
Write-Host "Migrating mailbox for $GivenName $Surname"
New-MoveRequest -Identity $Username -Remote -RemoteHostName $RemoteHostName -TargetDeliveryDomain $TargetDeliveryDomain -RemoteCredential $OnPremCREDS -BadItemLimit 100
Do
{
Start-Sleep -s 30
Write-Host "Migration not completed yet.. still waiting..."
$Status = Get-MoveRequest | Where-Object {$_.Alias -like $MailboxAliasToMove} | Select Status | Select -Expand Status
}
While($Status -ne "Completed")
Write-Host "Migration completed for $GivenName $Surname"
Set-MsolUser -UserPrincipalName $UserPrincipalName -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $UserPrincipalName -AddLicenses ""
}
Write-Host "Users have been created."