ODFB Folder And File Collaboration With Other ODFB Users

Introduction

 
Hi guys, let's explore a process of ODFB specific folders/files collaboration/permission granting to another ODFB User Account within the same tenant, along with an HTML Output report with error message specifications. This is better than the process in my previous blog.
 
The possible permission types are:
  • Read
  • Contribute
  • Full Control
Prerequisites
  • All the Source and Target User Accounts must be ODFB provisioned along with a minimum Microsoft E3-E5 license.
  • Windows PowerShell ISE with all PnP packages, modules to be installed.
  • Service Account of the Tenant Account to run the PnP script seamlessly.
CSV report with all the columns:
 
UserEmail Folder Role SiteURL
Rahul.Roy@sample.com /personal/veera_kaveri_sample_com/Documents/New Joinees Tasks Read https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
Rahul.Roy@sample.com /personal/veera_kaveri_sample_com/Documents/BoxUpdate Contribute https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
Rahul.Numb@sample.com /personal/veera_kaveri_sample_com/Documents/Resumes Full Control https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
Rahul.Roy@sample.com /personal/veera_kaveri_sample_com/Documents/New Joinees Tasks/Test1.jpg Read https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
Rahul.Roy@sample.com /personal/veera_kaveri_sample_com/Documents/BoxUpdate/Test2.jpg Contribute https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
Rahul.numbi@sample.com /personal/veera_kaveri_sample_com/Documents/Resumes/Test3.jpg Full Control https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com
 
I have given the wrong spelling of User Accounts in the above yellow highlighted areas to also show you the error rows output on my HTML report in the below output screenshot.
 
Process
 
Use the below PnP script after configuring the below-highlighted areas and run it to have the above permission roles assigned to the above folder specific ODFB User Accounts.
  1. #Variables  
  2. $CSVPath = "D:\Bharat\ODFB\OnedriveUsers10.csv"  
  3. $ListName = "Documents"  
  4. $folderPermissionFlag = 'No'  
  5. $HtmlReport = "<style>TABLE { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse; } TH { border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: blue;}TD { border-width: 1px; padding: 3px; border-style: solid; border-color: black; } </style><table><tr><th>User Email</th><th>Folder</th><th>Role</th><th>Site URL</th><th>Type</th><th>Status</th><th>Error Mesage</td></th></tr>"  
  6. $result = @()  
  7. $username = "svcacct-test-Migration@xxx.onmicrosoft.com"  
  8. $password = '#######'  
  9. $cred = New - Object - TypeName System.Management.Automation.PSCredential - argumentlist $userName, $(convertto - securestring $Password - asplaintext - force)  
  10. #Get data from CSV  
  11. $CSVData = Import - Csv $CSVPath  
  12. #Iterate through each row in CSV  
  13. ForEach($Row in $CSVData) {  
  14.     Try {  
  15.         #Connect to SharePoint Online Site  
  16.         #Write - host "Connecting to Site: "  
  17.         $Row.SiteURL  
  18.         # $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td style='background-color:green'>" + "Success : File permissions given " + "</td><td></td></tr>"  
  19.         If($Row.Folder.IndexOf('.') - gt 0) {  
  20.             try {  
  21.                 $file = Get - PnPFile - Url $Row.Folder - AsListItem  
  22.                 Set - PnPListItemPermission - List $ListName - Identity $file.ID - User $Row.UserEmail - AddRole $Row.Role - ErrorAction Stop  
  23.                 $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td>File</td><td style='background-color:green'>" + "Success" + "</td><td></td></tr>"  
  24.             } catch {  
  25.                 $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td>File</td><td style='background-color:red'>" + "Failed </td><td>" + $_.Exception.Message + "</td></tr>"  
  26.             }  
  27.         }  
  28.         else {  
  29.             try {  
  30.                 Connect - PnPOnline - Url $Row.SiteURL - Credentials $cred  
  31.                 Set - PnPFolderPermission - List $ListName - Identity $Row.Folder - User $Row.UserEmail - AddRole $Row.Role - ErrorAction Stop  
  32.                 $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td>Folder</td><td style='background-color:green'>" + "Success" + "</td><td></td></tr>"  
  33.             } catch {  
  34.                 $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td>Folder</td><td style='background-color:red'>" + "Failed </td><td>" + $_.Exception.Message + "</td></tr>"  
  35.             }  
  36.         }  
  37.     }  
  38.     Catch {  
  39.         # write - host - f Red "Error Adding User to Group:"  
  40.         $_.Exception.Message  
  41.         DisConnect - PnPOnline  
  42.         $HtmlReport += "<tr><td>" + $Row.UserEmail + "</td><td>" + $Row.Folder + "</td><td>" + $Row.Role + "</td><td>" + $Row.SiteURL + "</td><td></td><td style='background-color:red'>" + "Failed </td><td>" + $_.Exception.Message + "</td></tr>"  
  43.     }  
  44. }  
  45. #Write - Host $HtmlReport  
  46. $HtmlReport += "</table>"  
  47. $HtmlReport | Out - File - FilePath D: \Bharat\ ODFB\ outputFile.html - Force  
  48. Invoke - Expression D: \Bharat\ ODFB\ ODFBCollabOutputFile.html  
Amazing ODFB Folders And Files Collaboration With Other ODFB Use
 
The above script is run automatically, as we are hard coding the Admin Login details as shown in the above PnP Script and all the above-mentioned specific folders from their respective ODFB account get permission shared with the Role type mentioned to the User Email mentioned in the CSV rows.
 
Note
You need to keep the HTML file with the same name ready in the above Output path as mentioned so that the output is generated as shown above and can easily be browsed on a browser and also used for further analysis.
 

Conclusion

 
The entire process above can be used for bulk user accounts permissions/collaborations to various ODFB Folders/Files along with the HTML Output report as shown above via PnP scripting by just passing the CSV report prepared in the above format. This also promotes seamless running without giving login details during the runtime.
 
Cheers!!