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
<# | |
Author: I. Strachan | |
Version: 1.0 | |
Version History: | |
Purpose: Import Exchange Online cmdlets and create a New Mailbox | |
#> | |
param( | |
[String] | |
$Alias = 'irwins', | |
[String] | |
$Name = 'irwins', | |
[String] | |
$FirstName = 'Irwin', | |
[String] | |
$LastName = 'Strachan', | |
[String] | |
$DisplayName = 'Irwin Strachan' | |
) | |
#region Exchange Online Functions | |
#https://practical365.com/exchange-server/powershell-function-connect-office-365/ | |
function Connect-EXOnline { | |
Param( | |
[PSCredential]$EXOnlineCred | |
) | |
$Url = 'https://ps.outlook.com/powershell' | |
$paramEXOSession = @{ | |
ConfigurationName = 'Microsoft.Exchange' | |
ConnectionUri = $Url | |
Credential = $EXOnlineCred | |
Authentication = 'Basic' | |
AllowRedirection = $true | |
Name = 'Exchange Online' | |
} | |
$EXOSession = New-PSSession @paramEXOSession | |
Import-PSSession $EXOSession -Prefix 'Online' | |
} | |
function Disconnect-EXOnline { | |
Remove-PSSession -Name 'Exchange Online' | |
} | |
#endregion | |
#region Get Credentials and Connect To Exchange Online | |
$paramCredential = @{ | |
Message = 'Enter Global O653 Admin account' | |
UserName = 'o365admin@yourtenant.onmicrosoft.com' | |
} | |
$o365Admin = Get-Credential @paramCredential | |
Connect-EXOnline -EXOnlineCred $o365Admin | |
Connect-MsolService -Credential $o365Admin | |
#endregion | |
#region Create a new Mailbox | |
$paramEXOMailBox = @{ | |
Alias = $Alias | |
Name = $Name | |
FirstName = $FirstName | |
LastName = $LastName | |
DisplayName = $DisplayName | |
MicrosoftOnlineServicesID = '{0}@yourtenant.onmicrosoft.com' -f $Alias | |
Password = $(ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) | |
ResetPasswordOnNextLogon = $true | |
} | |
New-OnlineMailbox @paramEXOMailBox | |
#endregion | |
#region Assign new Mailbox a license | |
#Get AccountSku. In my case I only have one | |
$SkuId = (Get-MsolAccountSku).AccountSkuId | |
#Get All unlicensed users | |
Get-MsolUser -All -UnlicensedUsersOnly | |
#Set MSOL User License | |
#Alternatively have a look at https://practical365.com/blog/managing-office-365-licenses-with-azure-ad-v2-powershell-module/ | |
Set-MsolUser -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -UsageLocation "NL" | |
Set-MsolUserLicense -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -AddLicenses $SkuId | |
#endregion | |
#region Disconnect | |
Disconnect-EXOnline | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment