Quantcast
Viewing all articles
Browse latest Browse all 10096

Create folders in ODFB with PowerShell for multiple users (.csv file)

Hello,

I'm trying to create 2 folders on each users' ODFB provisioned, and can't get the PowerShell script to work for multiple users at once.

I used the same script to create my 2 folders for ONE user (after modifying a bit), and that worked correctly.
Went to all possible websites on internet to look for a solution for the error I'm getting, still can't get it resolved :-(

Script:
======

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$spoUser = "<admin_acct>@<tenant_name>.onmicrosoft.com"
$spoPassword = Read-Host -Prompt "Please enter your Admin password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spoUser,$spoPassword)


$FilePath = "C:\users\<localAcct>\Desktop\ODFBUsers.csv"
$users = Import-Csv -Path $FilePath

ForEach ($user in $users) {
$spoOD4BUrl = ("https://<tenant_name>-my.sharepoint.com/personal/{0}_mydomain_co_uk" -f $user.user)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($spoOD4BUrl)
$ctx.RequestTimeout = 16384000
$ctx.Credentials = $creds
$ctx.ExecuteQuery()

$web = $ctx.Web
$ctx.Load($web)

$spoDocLibName = "Documents"
$spoList = $web.Lists.GetByTitle($spoDocLibName)
$ctx.Load($spoList.RootFolder)

$spoFolder = $spoList.RootFolder
$Folder1 = "FOLDER1"
$newFolder1 = $spoFolder.Folders.Add($Folder1)
$web.Context.Load($newFolder1)
$web.Context.ExecuteQuery()

$Folder2 = "FOLDER2"
$newFolder2 = $spoFolder.Folders.Add($Folder2)
$web.Context.Load($newFolder2)
$web.Context.ExecuteQuery()
}

When I run this script I get the following error:
=============================

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
At C:\Users\<localAcct>\Desktop\MyPSScriptName.ps1:32 char:2
+     $ctx.ExecuteQuery()
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
 

I have looked for this error everywhere, but cannot find anything to solve my problem (or that I could probably understand from a CSOM perspective :-) )

Thanks for your help.


FrenchSpeaker - MCTS


Viewing all articles
Browse latest Browse all 10096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>