Last active
October 22, 2019 12:19
-
-
Save grantcarthew/90a8832c4904421eefedc3f4997109c5 to your computer and use it in GitHub Desktop.
Office 365 User Provisioning
This file contains 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
User Name | First Name | Last Name | Display Name | Job Title | Department | Office Number | Office Phone | Mobile Phone | Fax | Address | City | State or Province | ZIP or Postal Code | Country or Region | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Porky | Porky | Pig | Porky Pig | Sidekick | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Daffy | Daffy | Duck | Daffy Duck | Bugs Bunny Sidekick | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Elmer | Elmer | Fudd | Elmer Fudd | Hunter | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Bugs | Bugs | Bunny | Bugs Bunny | Lead Character | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Tweety | Tweety | Tweety | Cute Bird | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | ||
Sylvester | Sylvester | Sylvester | Hungry Cat | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | ||
Yosemite | Yosmite | Sam | Yosmite Sam | Gun Slinger | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Foghorn | Foghorn | Leghorn | Forhorn Leghorn | Not Lunch | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Marvin | Marvin | Martian | Marvin Martian | Alien | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Road | Road | Runner | Road Runner | Fast Innocent Bird | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Ralph | Ralph | Wolf | Ralph Wolf | Hungry Wolf | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Sam | Sam | Sheepdog | Sam Sheepdog | Expert Wolf Catcher | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Speedy | Speedy | Gonzales | Speedy Gonzales | Smart Mouse | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA | |
Tazmanian | Tazmanian | Devil | Tazmanian Devil | Hungry | ACME | 12345 | 67890 | 1234567890 | 0987654321 | 4000 Warner Blvd | Burbank | CA | 91522 | USA |
This file contains 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
<# | |
.SYNOPSIS | |
Creates new ACME users in Office 365 | |
.DESCRIPTION | |
Creates new ACME users in our Office 365 tenancy and | |
adds them to the Cartoon email group by default. | |
Created by Grant Carthew for the DDLS Webinar 2017-11 | |
.EXAMPLE | |
New-ACMEUser -CSVFilePath D:\NewUsers.csv -GroupName Cartoon | |
#> | |
[CmdletBinding(SupportsShouldProcess=$true)] | |
[Alias()] | |
[OutputType([String])] | |
Param ( | |
# CSVFilePath is the path to the CSV file to import. | |
[Parameter(Mandatory=$false, Position=0)] | |
[ValidateNotNullOrEmpty()] | |
$CSVFilePath = '.\New-ACMEUser.csv', | |
# GroupName is the name of the group for this list of users. | |
[Parameter(Mandatory=$false, Position=1)] | |
[ValidateNotNullOrEmpty()] | |
$GroupName = 'Cartoon' | |
) | |
begin { | |
# Initialize processes here | |
$startTime = Get-Date | |
"Connecting to Office 365" | |
$cred = Get-StoredCredential -Target "MSOnline" | |
Connect-MsolService -Credential $cred | |
$exOnlineSession = New-PSSession -ConfigurationName Microsoft.Exchange ` | |
-ConnectionUri 'https://outlook.office365.com/powershell-liveid/' ` | |
-Credential $cred ` | |
-Authentication Basic ` | |
-AllowRedirection | |
Import-PSSession $exOnlineSession | |
$upnSufix = "@carthew.onmicrosoft.com" | |
if (Test-Path -Path $CSVFilePath) { | |
$newUsersCsv = Import-Csv -Path $CSVFilePath | |
} else { | |
Write-Error -Message "Invalid CSV file path: " + $CSVFilePath | |
return | |
} | |
"Creating user accounts..." | |
} | |
process { | |
# Adding the users UPN to the array | |
foreach ($newUser in $newUsersCsv) { | |
$upn = $newUser."User Name".ToLower() + $upnSufix | |
Add-Member -InputObject $newUser -MemberType NoteProperty -Name UPN -Value $upn | |
} | |
if ($pscmdlet.ShouldProcess("Office 365", "Create new ACME user")) { | |
# Creating the user accounts in Office 365 | |
foreach ($newUser in $newUsersCsv) { | |
New-MsolUser ` | |
-LicenseAssignment "carthew:DEVELOPERPACK" ` | |
-UsageLocation "AU" ` | |
-UserPrincipalName $newUser.UPN ` | |
-FirstName $newUser."First Name" ` | |
-LastName $newUser."Last Name" ` | |
-DisplayName $newUser."Display Name" ` | |
-Title $newUser."Job Title" ` | |
-Department $newUser.Department ` | |
-Office $newUser."Office Number" ` | |
-PhoneNumber $newUser."Office Phone" ` | |
-MobilePhone $newUser."Mobile Phone" ` | |
-Fax $newUser.Fax ` | |
-StreetAddress $newUser.Address ` | |
-City $newUser.City ` | |
-State $newUser."State or Province" ` | |
-PostalCode $newUser."ZIP or Postal Code" ` | |
-Country $newUser."Country or Region" | |
} | |
} | |
if ($pscmdlet.ShouldProcess("Office 365", "Add users to $GroupName group")) { | |
$startWait = Get-Date | |
$waitList = New-Object System.Collections.ArrayList | |
$waitList.AddRange($newUsersCsv.UPN) | |
"Waiting on mailbox creation: " + $waitList.Count | |
while ($waitList.Count -gt 0) { | |
foreach ($upn in $waitList.ToArray()) { | |
$mbxFound = Get-Mailbox -Filter "UserPrincipalName -eq '$upn'" | |
if ($mbxFound) { $waitList.Remove($upn) } | |
} | |
$elapsed = New-TimeSpan -Start $startWait -End (Get-Date) | |
"[$($elapsed.ToString('hh\:mm\:ss'))] Remaining: $($waitList.Count)" | |
} | |
"Adding users to $GroupName group..." | |
Add-UnifiedGroupLinks -Identity $GroupName -LinkType Member -Links ($newUsersCsv.UPN) | |
} | |
} | |
end { | |
# Clean up processes | |
# No clean up required for this example | |
"Script completed" | |
$totalTime = New-TimeSpan -Start $startTime -End (Get-Date) | |
"Total running time: " + $totalTime.ToString('hh\:mm\:ss') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: