Batch Provision oneDrive Sites
Office 365 batch provision oneDrive for Business sites
This is one way to provision ALL users with a OneDrive for Business site. A few lessons I learned the hard way:
- pre-provisioning oneDrive for Business Sites must be done in batches of 200
- Request-SPOPersonalSite -NoWait , does absolutely nothing at all
- Even if you have the Global Administrator role, you still need to be licensed for Exchange Online in order to properly provision the oneDrive Sites
This script relies on the Azure AD Module, the Sharepoint Online Module and the Exchange Online Management Shell
$modulesToLoad = @("msonline", "Microsoft.Online.SharePoint.Powershell")
$modulesToLoad | foreach {
if (!(Get-Module | Where-Object {$_.Name -like "$_"})) {
Import-Module $_
}
}
function Check-OneDrive {
param (
[Parameter(Mandatory=$true)]
[String]$upn
)
$oneDriveSite = ($MySitePrefix+($upn -replace '[\.\@]','_').Insert(0,'/personal/'))
try {
Get-SPOSite $oneDriveSite
return $true
}
catch {
return $false
}
}
$AdminURI = "https://yoursite-admin.sharepoint.com"
$AdminAccount = "user.name@your-site.onmicrosoft.com"
# URL for your tenant's MySite (OneDrive) domain
$MySitePrefix = "https://yoursite-my.sharepoint.com"
#Ask for password if not already set
if (!(Get-Variable -Name pass -ErrorAction SilentlyContinue)) { #Check if Variable Exists
$pass = Read-Host -AsSecureString -Prompt " Enter Password for $AdminAccount"
$msolCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminAccount, $pass
#$spolCred = New-Object -TypeName Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount, $pass)
}
#Connect to the Sharepoint Online Service
Write-Host "Connecting to SharePoint Online Service ($AdminURI)" -ForegroundColor Green -BackgroundColor Black
Connect-SPOService -Url $AdminURI -Credential $msolCred
Write-Host "Connecting to Microsoft Online Service" -ForegroundColor Green -BackgroundColor Black
Connect-MsolService -Credential $msolCred
#Load Exchange Online Management Shell
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $msolCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
$users = Get-Mailbox -ResultSize Unlimited | Select-Object userPrincipalName
$totalUsers = $users.Count
Write-Host $totalUsers New Accounts to Provision -ForegroundColor Black -BackgroundColor Gray
#Pre-Provision the OneDrive Sites. Must be done in Batches of 200
#Split the pre-provison list to batches of 200
$counter = [Management.Automation.PSObject] @{Value=0}
$groupSize=200
$provisionGroup = $users.userPrincipalName | Group-Object -Property {[Math]::Floor($counter.Value++ / $groupSize)}
Write-Host "Processing USER List" -ForegroundColor Green -BackgroundColor Black
$itemCount=1
foreach ($group in ($provisionGroup.Name)) {
$batchCount=1
$batchList=@()
foreach ($item in $provisionGroup[$group].group) {
$itemCount++
Write-Host "$itemCount of $totalUsers" -ForegroundColor DarkYellow -BackgroundColor DarkRed
#Check if the account has already been setup with a OneDrive for business site
#Check-OneDrive $item
#Add the account to the batchlist
$batchList+=$item
$batchCount++
if ($batchCount -gt 200){
Write-Host "`tSending Batch Provision List (200 users) to o365" -ForegroundColor Magenta -BackgroundColor Black
#provision One drive for the entire Group
Write-Host "...Please Wait" -ForegroundColor Cyan -BackgroundColor Black
Sleep 60 #had issues when the pause was not here. I'm too lazy to figure out why, the pause works though
try {
Request-SPOPersonalSite -UserEmails $batchList -ErrorAction Stop
}
Catch {
#another dumb thing that works, but sometimes the first try fails. This just tries it again
Request-SPOPersonalSite -UserEmails $batchList -ErrorAction Stop
}
Write-Host "`n"
}
}
}
#Process Last batch
Write-Host "`tSending Batch Provision List (<200 users) to o365" -ForegroundColor Magenta -BackgroundColor Black
#provision One drive for the entire Group
Write-Host "...Please Wait" -ForegroundColor Cyan -BackgroundColor Black
Sleep 60
try {
Request-SPOPersonalSite -UserEmails $batchList -ErrorAction Stop
}
Catch {
Request-SPOPersonalSite -UserEmails $batchList -ErrorAction Stop
}
Write-Host "`t`tBatch Processing Complete" -ForegroundColor White -BackgroundColor Green
Write-Host "`t DONE!" -ForegroundColor Green -BackgroundColor Black
Disconnect-SPOService