Hello,
You will not be able to create user mailboxes in Office 365 with the built-in Adaxes functionality, but you can use PowerShell scripts for this purpose. You can use the PowerShell scripts in Business Rules, Custom Commands and Scheduled Tasks with the help of the Run a program or PowerShell script action. For example, you can create a Business Rule triggered after creating a user and create mailboxes in Office 365 for all newly created users or use the script in a Custom Command to be able to create a mailbox for any user on demand.
For information on how to create a Business Rule that runs a script automatically after creating a user, see the following tutorial: http://www.adaxes.com/tutorials_Automat ... ngUser.htm.
To be able to create mailboxes, Adaxes needs the credentials of a user with the rights to create mailboxes in your Office 365 organization. You have two options for this: you can either store the credentials directly in the body of the script or store them in a secure storage.
The following script creates Office 365 mailboxes for users in your Active Directory. The credentials are stored directly in the body of the script:
# Credentials required to create mailboxes in your cloud-based organization
$userID = "user@domain.com" # TODO: modify me
$password = ConvertTo-SecureString -AsPlainText -Force -String "Password" # TODO: modify me
# Domain name in your cloud-based organization
$userDomainName = "domain.com" # TODO: modify me
# Password to the newly created mailbox
$userPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force # TODO: modify me
$credential = New-Object System.Management.Automation.PsCredential($userID,$password)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" `
-Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $session
New-MailUser -Name "%fullname%" -MicrosoftOnlineServicesID "%username%@$userDomainName" -Password $userPassword
Remove-PSSession $session
In the script, $userID and $password specify the credentials of a user who has sufficient permissions to create mailboxes in your Office 365 organization, $userDomainName specifies the name of the domain that is registered in your Office 365 organization and that will be used for the newly created mailboxes, and $userPassword specifies the default password that will be set initially for all mailboxes.
The following script creates Office 365 mailboxes for users in your Active Directory. The credentials are imported from the secure storage in the folder specifies by $credentialDirectoryPath:
$credentialDirectoryPath = "C:\ScriptDirectory\Credentials" # TODO: modify me
# Domain name in your cloud-based organization
$userDomainName = "domain.com" # TODO: modify me
# Password to the newly created mailbox
$userPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force # TODO: modify me
# Check credentials directory path
if(!(Test-Path -Path $credentialDirectoryPath))
{
$Context.LogMessage("The credentials folder was not found. Make sure that $credentialDirectoryPath exists.", "Error") # TODO: modify me
return
}
# Read credentials from the file
$file = Get-ChildItem -Path $credentialDirectoryPath
if(!$file)
{
$Context.LogMessage("The credentials file was not found.", "Error") # TODO: modify me
return
}
$userID = (Get-Content -Path $file.FullName)[0]
$passwordEncryptedString = (Get-Content -Path $file.FullName)[1]
$password = ConvertTo-SecureString -String $passwordEncryptedString
$credential = New-Object System.Management.Automation.PsCredential($userID,$password)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" `
-Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $session
New-MailUser -Name "%fullname%" -MicrosoftOnlineServicesID "%username%@$userDomainName" -Password $userPassword
Remove-PSSession $session
In the script, $credentialDirectoryPath specifies the path to the secure storage that stores the credentials of a user who has sufficient permissions to create mailboxes in your Office 365 organization, $userDomainName specifies the name of the domain that is registered in your Office 365 organization and that will be used for the newly created mailboxes, and $userPassword specifies the default password that will be set initially for all mailboxes.
To be able to use the script, first you need to create a secure storage with credentials of a user who has sufficient permissions to create mailboxes in your Office 365 organization. To do this:
-
Create a new text file. The text file should contain only two lines:
-
ID of the user who has sufficient permissions to create mailboxes in your Office 365 organization.
-
Password to the account of the user.
For example:
```powershell
user@domain.com
P@$$w0rd
```
-
Run the following script in PowerShell. The script will import the credentials specified in the text file to a secure storage. The secure storage will be located in the same folder as the text file, in the Credentials subfolder. The password will be encrypted using the standard Windows Encryption API, which means that only the user whose credentials were used to launch the script will be able to read the credentials in the secure storage. Since Adaxes uses the account of the default service administrator to perform all operations in Active Directory (including launching scripts), you will need to run the script with the credentials of Adaxes default service administrator to be able to use the credentials stored in the secure storage.
To import credentials from the text file created on the previous step, you need to launch the script with the -credentialFilePath full_file_path parameter, where full_file_path is the path to the text file that you've created on the previous step, for example:
.ImportCredentinal.ps1 -credentialFilePath C:\ScriptDirectory\credentials.txt
Optionally, you can also specify the -deleteOldCredentials parameter. If this parameter is specified, the script will purge the directory with credentials before creating any new files.
Here's the text of the script that imports credentials to a secure storage:
Param($credentialFilePath, [switch]$deleteOldCredentials) # Run the script with command line parameter -credentialFilePath (credentials file path) and optional parameter -deleteOldCredentials
$scriptDirectoryPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
# Create a directory for files with credentials
$credentialDirectoryPath = $scriptDirectoryPath + "\Credentials"
if ($deleteOldCredentials)
{
if ((Test-Path -Path $credentialDirectoryPath))
{
Get-Item -Path $credentialDirectoryPath | Remove-Item -Force -Recurse
}
}
if (!(Test-Path -Path $credentialDirectoryPath))
{
New-Item -ItemType directory -Path $credentialDirectoryPath | Out-Null
}
$credentialDirectory = Get-Item -Path $credentialDirectoryPath
$filePath = $credentialDirectory.FullName + "\" + "credentials.data"
if((Test-Path -Path $filePath))
{
Get-Item -Path $filePath | Remove-Item -Force -Recurse
}
$file = New-Item $filePath -Type file
Set-Content -Path $file.FullName -Value (Get-Content $credentialFilePath)[0]
ConvertTo-SecureString (Get-Content $credentialFilePath)[1] -AsPlainText -Force | ConvertFrom-SecureString | Add-Content $file
Write-Host "Import complete"
-
When credentials are imported into the secure storage, you no longer need the text file. You may safely delete it.