This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPreSetupOdbSites.ps1
More file actions
288 lines (224 loc) · 11.2 KB
/
Copy pathPreSetupOdbSites.ps1
File metadata and controls
288 lines (224 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
function Add-UsersForOdbSitesCreation
{
<#
.Synopsis
Creation of OneDrive for Business sites for users
.Description
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.
.Example
Add-UsersForOdbSitesCreation -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com"
.Output
Hash table with Status and the Failures of User emails separated by commas
Name Value
---- -----
Failures
Status Add-UsersForOdbSitesCreation finished executing. Failures: [0]
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $userName,
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $password,
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $adminSiteUrl,
[Parameter(
Mandatory=$true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNull()]
[string] $userIds
)
<#Enable if you want to import the dlls directly#>
<#
Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.UserProfiles.dll'
Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.dll'
#>
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
$results = @{}
if ($userName.Trim().Length -eq 0)
{
throw "userName parameter is empty."
}
if ($password.Trim().Length -eq 0)
{
throw "password parameter is empty."
}
if ($adminSiteUrl.Trim().Length -eq 0)
{
throw "$adminSiteUrl parameter is empty."
}
if ($userIds.Trim().Length -eq 0)
{
throw "userIds parameter is empty."
}
try
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($adminSiteUrl)
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$securePassword)
$profileLoader = [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx)
if ($profileLoader -eq $null)
{
throw "Unable to get ProfileLoader [$adminSiteUrl]"
}
$inputUserIds = @()
foreach ($userId in $userIds.Split(","))
{
$id = $userId.Trim()
if($id.Length -gt 0 )
{
$inputUserIds += $id
}
}
if ($inputUserIds.Length > 200)
{
throw "Number of users [$($inputUserIds.Length)] is more than 200, the maximum allowed."
}
Write-Host "Calling CreatePersonalSiteEnqueueBulk with [$($inputUserIds.Length)] users"
$failedUserIds = @( $profileLoader.CreatePersonalSiteEnqueueBulk($inputUserIds) )
$msg = "Add-UsersForOdbSitesCreation finished executing. Failures: [$($failedUserIds.Length)]"
Write-Host $msg
Write-Host "Failed users: $failedUserIds"
$results["Status"] = $msg
$results["Failures"] = $failedUserIds -Join ","
return $results
}
catch
{
Write-Host $_.Exception.ToString()
throw $_
}
}
function Grant-TenantAdminPermissionsToOdbSites
{
<#
.Synopsis
Grants the Tenant Admin permission to the OneDrive for Business sites of some users
.Description
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.
.Example
Grant-TenantAdminPermissionsToOdbSites -userName "admin@contoso.onmicrosoft.com" -password "December2015" -adminSiteUrl "https://contoso-admin.sharepoint.com" -userIds "user1@contoso.onmicrosoft.com,user2@contoso.onmicrosoft.com"
.Output
Hash table with Status and the Failures of User emails separated by commas
Name Value
---- -----
Failures
Status Granting Tenant Admin Permissions To Odb Sites finished executing. Failures: [0]
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $userName,
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $password,
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $adminSiteUrl,
[Parameter(
Mandatory=$true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNull()]
[string] $userIds
)
<#Enable if you want to import the dlls directly#>
<#
Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.UserProfiles.dll'
Import-Module 'C:\mydlls\Microsoft.SharePoint.Client.dll'
#>
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
$results = @{}
if ($userName.Trim().Length -eq 0)
{
throw "userName parameter is empty."
}
if ($password.Trim().Length -eq 0)
{
throw "password parameter is empty."
}
if ($adminSiteUrl.Trim().Length -eq 0)
{
throw "adminSiteUrl parameter is empty."
}
if ($userIds.Trim().Length -eq 0)
{
throw "userIds parameter is empty."
}
try
{
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $password -asplaintext -force)
Connect-SPOService -Url $adminSiteUrl -Credential $cred
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($adminSiteUrl)
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$securePassword)
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
$ctx.ExecuteQuery();
if ($peopleManager -eq $null)
{
throw "Unable to get peopleManager [$adminSiteUrl]"
}
$inputUserIds = @()
foreach ($userId in $userIds.Split(","))
{
$id = $userId.Trim()
if($id.Length -gt 0 )
{
$inputUserIds += $id
}
}
if ($inputUserIds.Length > 200)
{
throw "Number of users [$($inputUserIds.Length)] is more than 200, the maximum allowed."
}
$failedUserIds = @()
Write-Host "Calling GetPropertiesFor with [$($inputUserIds.Length)] users"
foreach ($inputUserId in $inputUserIds)
{
try
{
if ($inputUserIds -notcontains "i:0#.f|membership|")
{
$inputUserId = "i:0#.f|membership|" + $inputUserId
}
$personProperties = $peopleManager.GetPropertiesFor($inputUserId)
$ctx.Load($personProperties);
$ctx.ExecuteQuery();
if ($personProperties -eq $null)
{
throw "Unable to get the PersonProperties for user [$inputUserId]"
}
if ($personProperties.PersonalUrl -eq $null)
{
throw "Unable to get the Personal Site Url for user [$inputUserId]"
}
Set-SPOUser -Site $PersonProperties.PersonalUrl -LoginName $userName -IsSiteCollectionAdmin $true | Out-Null
}
catch
{
Write-Host $_.Exception.ToString()
$failedUserIds += $id
}
}
$msg = "Granting Tenant Admin Permissions To Odb Sites finished executing. Failures: [$($failedUserIds.Length)]"
Write-Host $msg
Write-Host "Failed users: $failedUserIds"
$results["Status"] = $msg
$results["Failures"] = $failedUserIds -Join ","
return $results
}
catch
{
Write-Host $_.Exception.ToString()
throw $_
}
}