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 |
[email protected] |
/personal/veera_kaveri_sample_com/Documents/New Joinees Tasks |
Read |
https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com |
[email protected] |
/personal/veera_kaveri_sample_com/Documents/BoxUpdate |
Contribute |
https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com |
[email protected] |
/personal/veera_kaveri_sample_com/Documents/Resumes |
Full Control |
https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com |
[email protected] |
/personal/veera_kaveri_sample_com/Documents/New Joinees Tasks/Test1.jpg |
Read |
https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com |
[email protected] |
/personal/veera_kaveri_sample_com/Documents/BoxUpdate/Test2.jpg |
Contribute |
https://sample-my.sharepoint.com/personal/veera_kaveri_sample_com |
[email protected] |
/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.
- #Variables
- $CSVPath = "D:\Bharat\ODFB\OnedriveUsers10.csv"
- $ListName = "Documents"
- $folderPermissionFlag = 'No'
- $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>"
- $result = @()
- $username = "[email protected]"
- $password = '#######'
- $cred = New - Object - TypeName System.Management.Automation.PSCredential - argumentlist $userName, $(convertto - securestring $Password - asplaintext - force)
- #Get data from CSV
- $CSVData = Import - Csv $CSVPath
- #Iterate through each row in CSV
- ForEach($Row in $CSVData) {
- Try {
- #Connect to SharePoint Online Site
- #Write - host "Connecting to Site: "
- $Row.SiteURL
- # $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>"
- If($Row.Folder.IndexOf('.') - gt 0) {
- try {
- $file = Get - PnPFile - Url $Row.Folder - AsListItem
- Set - PnPListItemPermission - List $ListName - Identity $file.ID - User $Row.UserEmail - AddRole $Row.Role - ErrorAction Stop
- $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>"
- } catch {
- $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>"
- }
- }
- else {
- try {
- Connect - PnPOnline - Url $Row.SiteURL - Credentials $cred
- Set - PnPFolderPermission - List $ListName - Identity $Row.Folder - User $Row.UserEmail - AddRole $Row.Role - ErrorAction Stop
- $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>"
- } catch {
- $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>"
- }
- }
- }
- Catch {
- # write - host - f Red "Error Adding User to Group:"
- $_.Exception.Message
- DisConnect - PnPOnline
- $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>"
- }
- }
- #Write - Host $HtmlReport
- $HtmlReport += "</table>"
- $HtmlReport | Out - File - FilePath D: \Bharat\ ODFB\ outputFile.html - Force
- Invoke - Expression D: \Bharat\ ODFB\ ODFBCollabOutputFile.html
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!!