OneDrive Users Provisioning Using Power Shell script

Create folder in same path - 1. Script 2. Logs, and provide  the User name details in CSV file. and check the path in script.
 
  1. <#c  
  2.   Provision OneDrive and Set Permissions  
  3. .DESCRIPTION  
  4.   Provisions OneDrive for list fo users and add administrator account to provisioned sites  
  5.   Update the variable section  
  6.   Make sure you have latest version of SharePoint Client SDK: http://www.microsoft.com/en-in/download/details.aspx?id=42038  
  7.   And Install latest version of Azure Module for Powershell and MSOL signin assistnat from here http://technet.microsoft.com/en-us/library/jj151815.aspx  
  8.  .EXAMPLE  
  9.    Set-OneDriveProvisonAndPermissions.ps1   
  10. .OUTPUTS  
  11.     
  12.     #>  
  13.  
  14. #Input Variables Begin  
  15.     #Credentials Store Location - Delete the xml if credentials change  
  16.     $credFile="C:\ProvisionOneDrive-AssignAdmins\Script\OD4BCred.xml"  
  17.     #Log File location - Need to create the directory  
  18.     $Logfile = "C:\ProvisionOneDrive-AssignAdmins\Logs\OneDriveProvisionReport.log"  
  19.     $strOrgName = "CompanyName"   
  20.     $Domain = "CompanyName.com"     
  21.     #O365 Global admin  
  22.     $O365AdminAccount = "[email protected]";  
  23.     #O365 Admin site URL  
  24.     $webUrl = "https://CompanyName-admin.sharepoint.com"  
  25.     [string]$usersCSVFile = "C:\ProvisionOneDrive-AssignAdmins\userstoprovision_Users.csv"  
  26.     #List of OneDrive users (seperated by CSV)  
  27.     $OneDriveUsers = $usersToProvision = import-csv $usersCSVFile | select -ExpandProperty UPN  
  28.      
  29. # Input Variables End  
  30.   
  31.   
  32. function LogWrite  
  33. {  
  34.   param([string]$logstring)  
  35.   
  36.   $dateTime = get-date -format G  
  37.   Write-Host " "  
  38.   Write-Host $dateTime $logstring  
  39.   Add-Content $Logfile -Value " "  
  40.   Add-Content $Logfile -Value ( $dateTime  + $logstring)  
  41.   
  42. }  
  43.   
  44. function Get-MSOLCredentials() {  
  45.    
  46.     if ((Get-Module Microsoft.Online.SharePoint.PowerShell).Count -eq 0) {  
  47.         Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking  
  48.     }  
  49.       
  50.     If (Test-Path ($credFile))  
  51.     {  
  52.         $cred = Import-Clixml $credFile  
  53.     }  
  54.     Else  
  55.     {  
  56.         Get-Credential $O365AdminAccount | Export-Clixml $credFile  
  57.         $cred = Import-Clixml $credFile  
  58.     }  
  59.   
  60.       
  61.     LogWrite ("**-- Loaded MSOL credentials from $credFile  --*")  
  62. }  
  63.   
  64. function Create-OneDriveSites()  
  65. {  
  66. try{  
  67. $Error.Clear()  
  68.     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")  
  69.     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  
  70.     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")  
  71.  
  72.     #Must be SharePoint Administrator URL  
  73.       
  74.     $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)  
  75.     $web = $ctx.Web  
  76.     $cred = Import-Clixml $credFile   
  77.     $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.Username,$cred.Password)  
  78.       
  79.     $ctx.Load($web)  
  80.     $ctx.ExecuteQuery()  
  81.   
  82.     $loader =[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx)  
  83.  
  84.     #To Get Profile  
  85.     $profile = $loader.GetUserProfile()  
  86.     $ctx.Load($profile)  
  87.     $ctx.ExecuteQuery()  
  88.     #$profile   
  89.   
  90.     foreach($user in $OneDriveUsers)  
  91.     {  
  92.       Write-Host "Queue for OD4B proviosioning: $user"   
  93.     }  
  94.       
  95.     #To enqueue Profile  
  96.     $loader.CreatePersonalSiteEnqueueBulk($OneDriveUsers)   
  97.     $loader.Context.ExecuteQuery()  
  98.   
  99.     LogWrite ("**-- Site Provision complete for $OneDriveUsers --*")   
  100. }  
  101. catch  
  102. {  
  103. if($Error.count -gt 0)  
  104. {  
  105. Start-Sleep -seconds 5  
  106. Create-OneDriveSites  
  107. }  
  108. }  
  109.       
  110.       
  111. }  
  112.   
  113. function Assign-Administrator()  
  114. {  
  115.     try  
  116. {  
  117. $Error.Clear()  
  118. foreach($user in $OneDriveUsers){    
  119.  $cred = Import-Clixml $credFile  
  120.  Connect-MsolService -Credential $cred  
  121.  Connect-SPOService -Url $webUrl -Credential $cred  
  122.  get-msoluser -userprincipalname $user  
  123.     $struser = $user  
  124.     $pos= $struser.IndexOf("@")  
  125.     $len = $struser.Length -1  
  126.     $DomainStartPos = $pos  
  127.     $DomainEndPos = $len  
  128.     #$Domain = $struser.SubString($DomainStartPos, $DomainEndPos)       
  129.     $strUser = $struser.SubString(0, $pos)     
  130.     $strUser = $struser -replace "\.""_"  
  131.     $PersonalOrgURL = "https://" + $strOrgName + "-my.sharepoint.com/personal/"  
  132.     $SiteUrl= $PersonalOrgURL + $struser  
  133.     $Domain = $Domain -replace "\.","_"  
  134.     $SiteUrl= $SiteUrl + "_" + $Domain  
  135.       
  136.     Set-SPOUser -Site $SiteURL -LoginName $O365AdminAccount -IsSiteCollectionAdmin $true   
  137.         LogWrite ("**-- Added $O365AdminAccount as Site Admin to $SiteUrl  --*")   
  138. }  
  139. }  
  140. catch  
  141. {  
  142. if($Error.count -gt 0)  
  143. {  
  144. Start-Sleep -seconds 5  
  145. Assign-Administrator  
  146. LogWrite ($Error.ToString())  
  147. }  
  148. }  
  149.   
  150. }  
  151.   
  152.   
  153.   
  154. try  
  155. {  
  156.     Get-MSOLCredentials  
  157.     Create-OneDriveSites  
  158.     Assign-Administrator  
  159.       
  160. }  
  161. catch  
  162. {  
  163.   LogWrite ($_.Exception.Message)  
  164.   LogWrite ($_.Exception.ItemName)  
  165.   LogWrite ($_.CategoryInfo)  
  166. }  
  167. finally  
  168. {  
  169.   # Dispose afterwards  
  170. }