TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
OneDrive Users Provisioning Using Power Shell script
Sushant Kesari
Jul 04
2016
Code
2.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Create folder in same path - 1. Script 2. Logs, and provide the User name details in CSV file. and check the path in script.
<#c
Provision OneDrive and Set Permissions
.DESCRIPTION
Provisions OneDrive
for
list fo users and add administrator account to provisioned sites
Update the variable section
Make sure you have latest version of SharePoint Client SDK: http:
//www.microsoft.com/en-in/download/details.aspx?id=42038
And Install latest version of Azure Module
for
Powershell and MSOL signin assistnat from here http:
//technet.microsoft.com/en-us/library/jj151815.aspx
.EXAMPLE
Set-OneDriveProvisonAndPermissions.ps1
.OUTPUTS
#>
#Input Variables Begin
#Credentials Store Location - Delete the xml if credentials change
$credFile=
"C:\ProvisionOneDrive-AssignAdmins\Script\OD4BCred.xml"
#Log File location - Need to create the directory
$Logfile =
"C:\ProvisionOneDrive-AssignAdmins\Logs\OneDriveProvisionReport.log"
$strOrgName =
"CompanyName"
$Domain =
"CompanyName.com"
#O365 Global admin
$O365AdminAccount =
"
[email protected]
"
;
#O365 Admin site URL
$webUrl =
"https://CompanyName-admin.sharepoint.com"
[
string
]$usersCSVFile =
"C:\ProvisionOneDrive-AssignAdmins\userstoprovision_Users.csv"
#List of OneDrive users (seperated by CSV)
$OneDriveUsers = $usersToProvision = import-csv $usersCSVFile | select -ExpandProperty UPN
# Input Variables End
function LogWrite
{
param([
string
]$logstring)
$dateTime =
get
-date -format G
Write-Host
" "
Write-Host $dateTime $logstring
Add-Content $Logfile -Value
" "
Add-Content $Logfile -Value ( $dateTime + $logstring)
}
function Get-MSOLCredentials() {
if
((Get-Module Microsoft.Online.SharePoint.PowerShell).Count -eq 0) {
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
}
If (Test-Path ($credFile))
{
$cred = Import-Clixml $credFile
}
Else
{
Get-Credential $O365AdminAccount | Export-Clixml $credFile
$cred = Import-Clixml $credFile
}
LogWrite (
"**-- Loaded MSOL credentials from $credFile --*"
)
}
function Create-OneDriveSites()
{
try
{
$Error.Clear()
[System.Reflection.Assembly]::LoadWithPartialName(
"Microsoft.SharePoint.Client"
)
[System.Reflection.Assembly]::LoadWithPartialName(
"Microsoft.SharePoint.Client.Runtime"
)
[System.Reflection.Assembly]::LoadWithPartialName(
"Microsoft.SharePoint.Client.UserProfiles"
)
#Must be SharePoint Administrator URL
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$web = $ctx.Web
$cred = Import-Clixml $credFile
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.Username,$cred.Password)
$ctx.Load($web)
$ctx.ExecuteQuery()
$loader =[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx)
#To Get Profile
$profile = $loader.GetUserProfile()
$ctx.Load($profile)
$ctx.ExecuteQuery()
#$profile
foreach
($user
in
$OneDriveUsers)
{
Write-Host
"Queue for OD4B proviosioning: $user"
}
#To enqueue Profile
$loader.CreatePersonalSiteEnqueueBulk($OneDriveUsers)
$loader.Context.ExecuteQuery()
LogWrite (
"**-- Site Provision complete for $OneDriveUsers --*"
)
}
catch
{
if
($Error.count -gt 0)
{
Start-Sleep -seconds 5
Create-OneDriveSites
}
}
}
function Assign-Administrator()
{
try
{
$Error.Clear()
foreach
($user
in
$OneDriveUsers){
$cred = Import-Clixml $credFile
Connect-MsolService -Credential $cred
Connect-SPOService -Url $webUrl -Credential $cred
get
-msoluser -userprincipalname $user
$struser = $user
$pos= $struser.IndexOf(
"@"
)
$len = $struser.Length -1
$DomainStartPos = $pos
$DomainEndPos = $len
#$Domain = $struser.SubString($DomainStartPos, $DomainEndPos)
$strUser = $struser.SubString(0, $pos)
$strUser = $struser -replace
"\."
,
"_"
$PersonalOrgURL =
"https://"
+ $strOrgName +
"-my.sharepoint.com/personal/"
$SiteUrl= $PersonalOrgURL + $struser
$Domain = $Domain -replace
"\."
,
"_"
$SiteUrl= $SiteUrl +
"_"
+ $Domain
Set-SPOUser -Site $SiteURL -LoginName $O365AdminAccount -IsSiteCollectionAdmin $
true
LogWrite (
"**-- Added $O365AdminAccount as Site Admin to $SiteUrl --*"
)
}
}
catch
{
if
($Error.count -gt 0)
{
Start-Sleep -seconds 5
Assign-Administrator
LogWrite ($Error.ToString())
}
}
}
try
{
Get-MSOLCredentials
Create-OneDriveSites
Assign-Administrator
}
catch
{
LogWrite ($_.Exception.Message)
LogWrite ($_.Exception.ItemName)
LogWrite ($_.CategoryInfo)
}
finally
{
# Dispose afterwards
}
One Drive
Power Shell script
users provisioning