├── LICENSE ├── README.md └── PreSetupOdbSites.ps1 /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 OneNoteDev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## OneDrive for Business Setup Powershell Cmdlets ## 2 | 3 | Created by Microsoft Corporation, 2016. Provided as-is without warranty. Trademarks mentioned here are the property of their owners. 4 | 5 | Tenant Admin Powershell Cmdlets to trigger the creation of OneDrive for Business sites and grant tenant administrators administrative access to these sites. 6 | 7 | ### Prerequisites ### 8 | Before you begin running the Scripts, review the following information about prerequisites: 9 | 10 | - [Set up the SharePoint Online Management Shell environment](http://go.microsoft.com/fwlink/p/?LinkId=506693) 11 | - [Download and install the SharePoint Online Client Components SDK](http://go.microsoft.com/fwlink/p/?LinkId=506692) 12 | 13 | Also verify the following: 14 | 15 | - The tenant admin is a Global Administrator in Office 365 for enterprises. 16 | - The tenant admin is a member of the Administrators group on the server on which you are running the Windows PowerShell script. 17 | 18 | ### PreSetupOdbSites.ps1 ### 19 | The PowerShell script contains the following cmdlets that can be made available by loading the script 20 | 21 | . C:\powershell\PreSetupOdbSites.ps1 22 | 23 | The cmdlets are as follows: 24 | 25 | 1- Add-UsersForOdbSitesCreation cmdlet 26 | 27 | Add users for OneDrive for Business site creation. 28 | 29 | 2- Grant-TenantAdminPermissionsToOdbSites cmdlet 30 | 31 | Grants the tenant administrators administrative permissions to the OneDrive for Business sites. 32 | 33 | ### Add-UsersForOdbSitesCreation cmdlet #### 34 | Add-UsersForOdbSitesCreation cmdlet can be used to trigger the creation of users' OneDrive for Business sites. The users’ emails need to be separated by commas. You can pass in a maximum of 200 users. The creation can take up to one day to complete. 35 | 36 | ***Example:*** 37 | 38 | Add-UsersForOdbSitesCreation -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com" 39 | 40 | ***Output:*** 41 | 42 | Hash table with Status and the Failures of users' emails separated by commas 43 | 44 | Name Value 45 | ---- ----- 46 | Failures 47 | Status Add-UsersForOdbSitesCreation finished executing. Failures: [0] 48 | 49 | ### Grant-TenantAdminPermissionsToOdbSites #### 50 | 51 | Grant-TenantAdminPermissionsToOdbSites cmdlet grants the Tenant Admin administrative permissions to the users' OneDrive for Business sites. The users’ emails need to be separated by commas. You can pass in a maximum of 200 users. 52 | 53 | ***Example:*** 54 | 55 | Grant-TenantAdminPermissionsToOdbSites -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com" 56 | 57 | ***Output:*** 58 | 59 | Hash table with Status and the Failures of users' emails separated by commas 60 | 61 | Name Value 62 | ---- ----- 63 | Failures 64 | Status Granting Tenant Admin Permissions To Odb Sites finished executing. Failures: [0] 65 | 66 | ### Version info ### 67 | 68 | This is the initial public release for this code sample. 69 | -------------------------------------------------------------------------------- /PreSetupOdbSites.ps1: -------------------------------------------------------------------------------- 1 | function Add-UsersForOdbSitesCreation 2 | { 3 | <# 4 | .Synopsis 5 | Creation of OneDrive for Business sites for users 6 | 7 | .Description 8 | This cmdlet can be used to trigger the creation of Users OneDrive for Business sites. The users emails needs to be separated by commas. You can pass in a maximum of 200 users. 9 | 10 | .Example 11 | Add-UsersForOdbSitesCreation -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com" 12 | 13 | .Output 14 | 15 | Hash table with Status and the Failures of User emails separated by commas 16 | 17 | Name Value 18 | ---- ----- 19 | Failures 20 | Status Add-UsersForOdbSitesCreation finished executing. Failures: [0] 21 | #> 22 | 23 | [CmdletBinding()] 24 | param( 25 | 26 | [Parameter(Mandatory=$true)] 27 | [ValidateNotNull()] 28 | [string] $userName, 29 | 30 | [Parameter(Mandatory=$true)] 31 | [ValidateNotNull()] 32 | [string] $password, 33 | 34 | [Parameter(Mandatory=$true)] 35 | [ValidateNotNull()] 36 | [string] $adminSiteUrl, 37 | 38 | [Parameter( 39 | Mandatory=$true, 40 | ValueFromPipeline = $true, 41 | ValueFromPipelineByPropertyName = $true)] 42 | [ValidateNotNull()] 43 | [string] $userIds 44 | ) 45 | 46 | <#Enable if you want to import the dlls directly#> 47 | <# 48 | Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.UserProfiles.dll' 49 | Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.dll' 50 | #> 51 | 52 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") 53 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") 54 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles") 55 | 56 | $results = @{} 57 | 58 | if ($userName.Trim().Length -eq 0) 59 | { 60 | throw "userName parameter is empty." 61 | } 62 | 63 | if ($password.Trim().Length -eq 0) 64 | { 65 | throw "password parameter is empty." 66 | } 67 | 68 | if ($adminSiteUrl.Trim().Length -eq 0) 69 | { 70 | throw "$adminSiteUrl parameter is empty." 71 | } 72 | 73 | if ($userIds.Trim().Length -eq 0) 74 | { 75 | throw "userIds parameter is empty." 76 | } 77 | 78 | try 79 | { 80 | $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($adminSiteUrl) 81 | $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force 82 | $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$securePassword) 83 | 84 | 85 | $profileLoader = [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx) 86 | 87 | if ($profileLoader -eq $null) 88 | { 89 | throw "Unable to get ProfileLoader [$adminSiteUrl]" 90 | } 91 | 92 | $inputUserIds = @() 93 | 94 | foreach ($userId in $userIds.Split(",")) 95 | { 96 | $id = $userId.Trim() 97 | if($id.Length -gt 0 ) 98 | { 99 | $inputUserIds += $id 100 | } 101 | } 102 | 103 | if ($inputUserIds.Length > 200) 104 | { 105 | throw "Number of users [$($inputUserIds.Length)] is more than 200, the maximum allowed." 106 | } 107 | 108 | Write-Host "Calling CreatePersonalSiteEnqueueBulk with [$($inputUserIds.Length)] users" 109 | $failedUserIds = @( $profileLoader.CreatePersonalSiteEnqueueBulk($inputUserIds) ) 110 | 111 | $msg = "Add-UsersForOdbSitesCreation finished executing. Failures: [$($failedUserIds.Length)]" 112 | Write-Host $msg 113 | Write-Host "Failed users: $failedUserIds" 114 | $results["Status"] = $msg 115 | $results["Failures"] = $failedUserIds -Join "," 116 | 117 | return $results 118 | } 119 | catch 120 | { 121 | Write-Host $_.Exception.ToString() 122 | throw $_ 123 | } 124 | } 125 | 126 | 127 | function Grant-TenantAdminPermissionsToOdbSites 128 | { 129 | <# 130 | .Synopsis 131 | Grants the Tenant Admin permission to the OneDrive for Business sites of some users 132 | 133 | .Description 134 | Grants the TenantAdmin administrative permissions to the OneDrive for Business sites of some users. The users emails needs to be separated by commas. You can pass in a maximum of 200 users. 135 | 136 | .Example 137 | Grant-TenantAdminPermissionsToOdbSites -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com" 138 | 139 | .Output 140 | 141 | Hash table with Status and the Failures of User emails separated by commas 142 | 143 | Name Value 144 | ---- ----- 145 | Failures 146 | Status Granting Tenant Admin Permissions To Odb Sites finished executing. Failures: [0] 147 | #> 148 | 149 | [CmdletBinding()] 150 | param( 151 | 152 | [Parameter(Mandatory=$true)] 153 | [ValidateNotNull()] 154 | [string] $userName, 155 | 156 | [Parameter(Mandatory=$true)] 157 | [ValidateNotNull()] 158 | [string] $password, 159 | 160 | [Parameter(Mandatory=$true)] 161 | [ValidateNotNull()] 162 | [string] $adminSiteUrl, 163 | 164 | [Parameter( 165 | Mandatory=$true, 166 | ValueFromPipeline = $true, 167 | ValueFromPipelineByPropertyName = $true)] 168 | [ValidateNotNull()] 169 | [string] $userIds 170 | ) 171 | 172 | <#Enable if you want to import the dlls directly#> 173 | <# 174 | Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.UserProfiles.dll' 175 | Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.dll' 176 | #> 177 | 178 | 179 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") 180 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") 181 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles") 182 | 183 | 184 | $results = @{} 185 | 186 | if ($userName.Trim().Length -eq 0) 187 | { 188 | throw "userName parameter is empty." 189 | } 190 | 191 | if ($password.Trim().Length -eq 0) 192 | { 193 | throw "password parameter is empty." 194 | } 195 | 196 | if ($adminSiteUrl.Trim().Length -eq 0) 197 | { 198 | throw "adminSiteUrl parameter is empty." 199 | } 200 | 201 | if ($userIds.Trim().Length -eq 0) 202 | { 203 | throw "userIds parameter is empty." 204 | } 205 | 206 | try 207 | { 208 | $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $password -asplaintext -force) 209 | Connect-SPOService -Url $adminSiteUrl -Credential $cred 210 | 211 | $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($adminSiteUrl) 212 | $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force 213 | $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$securePassword) 214 | 215 | 216 | $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx) 217 | $ctx.ExecuteQuery(); 218 | 219 | if ($peopleManager -eq $null) 220 | { 221 | throw "Unable to get peopleManager [$adminSiteUrl]" 222 | } 223 | 224 | $inputUserIds = @() 225 | 226 | foreach ($userId in $userIds.Split(",")) 227 | { 228 | $id = $userId.Trim() 229 | if($id.Length -gt 0 ) 230 | { 231 | $inputUserIds += $id 232 | } 233 | } 234 | 235 | if ($inputUserIds.Length > 200) 236 | { 237 | throw "Number of users [$($inputUserIds.Length)] is more than 200, the maximum allowed." 238 | } 239 | 240 | $failedUserIds = @() 241 | 242 | Write-Host "Calling GetPropertiesFor with [$($inputUserIds.Length)] users" 243 | foreach ($inputUserId in $inputUserIds) 244 | { 245 | try 246 | { 247 | if ($inputUserIds -notcontains "i:0#.f|membership|") 248 | { 249 | $inputUserId = "i:0#.f|membership|" + $inputUserId 250 | } 251 | 252 | $personProperties = $peopleManager.GetPropertiesFor($inputUserId) 253 | $ctx.Load($personProperties); 254 | $ctx.ExecuteQuery(); 255 | 256 | if ($personProperties -eq $null) 257 | { 258 | throw "Unable to get the PersonProperties for user [$inputUserId]" 259 | } 260 | 261 | if ($personProperties.PersonalUrl -eq $null) 262 | { 263 | throw "Unable to get the Personal Site Url for user [$inputUserId]" 264 | } 265 | 266 | Set-SPOUser -Site $PersonProperties.PersonalUrl -LoginName $userName -IsSiteCollectionAdmin $true | Out-Null 267 | } 268 | catch 269 | { 270 | Write-Host $_.Exception.ToString() 271 | $failedUserIds += $id 272 | } 273 | } 274 | 275 | $msg = "Granting Tenant Admin Permissions To Odb Sites finished executing. Failures: [$($failedUserIds.Length)]" 276 | Write-Host $msg 277 | Write-Host "Failed users: $failedUserIds" 278 | $results["Status"] = $msg 279 | $results["Failures"] = $failedUserIds -Join "," 280 | 281 | return $results 282 | } 283 | catch 284 | { 285 | Write-Host $_.Exception.ToString() 286 | throw $_ 287 | } 288 | } --------------------------------------------------------------------------------