Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Automating Office 365 user licensing and mailbox moves to the cloud in hybrid environment

$
0
0

Hi,

I spent quite a bit of time trying to figure out how to automate the move of (student) accounts to the cloud.  Most of my struggle, in the end, was understanding how to automate a credential object.  Part of this stemmed from still being a novice with Powershell, but a part of it seemed to be because of a difficulty in finding the right info.  With that in mind I wanted to share a script I created.  The script generates a list of new accounts and then applies a location and license to the account and then finally moving the mailbox from our on-premise Exchange to Office 365 (as well as removes old move requests in Exchange).  Hope this is helpful to others.

###########################################################
# Set Variable values
###########################################################
$date =[DateTime]::Now.AddDays(-3) #How many days to look back for new accounts
$pass = ConvertTo-SecureString -AsPlainText -String "<your password>" -Force
$onPremcred = "domain\username"
$o365Cred = "username@domain.com"

###########################################################
# Import necessary powershell modules
###########################################################
import-module ActiveDirectory
Import-Module MSOnline
###########################################################
# Get list of staff
###########################################################

get-aduser -filter {whenCreated -ge $date -and sAMAccountName -like "s-*"} -SearchBase "LDAP path, e.g. ou=staff users,dc=somedomain,dc=com"  -searchscope subtree | select userPrincipalName | export-csv c:\users.csv -notypeinformation -force


###########################################################
# Create connections to Office 365 for each credential, both
# local and remote (O365)
###########################################################

$msol =New-Object System.Management.Automation.PSCredential -ArgumentList $onPremcred, $pass
$msol2 =New-Object System.Management.Automation.PSCredential -ArgumentList $o365Cred, $pass
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $msol2 -Authentication Basic -AllowRedirection
Import-PSSession $session -AllowClobber

connect-msolservice -Credential $msol2

###########################################################
# Remove move requests of completed mailboxes
###########################################################

Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest -Confirm:$False

###########################################################
# Set location of student accounts
###########################################################

Import-Csv c:\users.csv | foreach {set-MsolUser -UserPrincipalName $_.userPrincipalName -UsageLocation US -verbose}

###########################################################
# Apply license
###########################################################
Import-Csv  c:\users.csv | foreach {Set-MsolUserLicense -UserPrincipalName $_.userPrincipalName -AddLicenses <license name> -verbose}

###########################################################
# Move mailbox(es)
###########################################################
Import-Csv  c:\users.csv | foreach {New-MoveRequest -Identity $_.userPrincipalName -Remote -RemoteHostName <MX domain -TargetDeliveryDomain <o365domain>.mail.onmicrosoft.com -RemoteCredential $msol -BadItemLimit 10}

###########################################################
# Close sessions
###########################################################

get-pssession | remove-pssession


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>