Modily site URL into csv for granting permission to document library

ネパリ サンデャ 380 Reputation points
2023-06-28T06:17:43.4166667+00:00

I wan to run following script by modifying SiteURL in csv same as the following csv

How can I modify site URL into csv so I could use script smoothly

User's image

#Config Variables
$SiteURL = "https://tsubaki518.sharepoint.com/sites/allcompany"
$CSVFile = "C:\Document.csv"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Get the CSV file
    $CSVData = Import-CSV $CSVFile
  
    #Read CSV file and grant folder permissions
    ForEach($Row in $CSVData)
    {
        Try {
            #Get the user
            $Users = $Row.user

            ForEach($User in $Users)
            {
                #Grant Permission to the Folder
                Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
                Set-PnPListPermission -Identity $Row.library -AddRole $Row.permission -User $User
            }
        }
        Catch {
            Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
        }
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,907 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,582 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,182 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 32,246 Reputation points Microsoft Vendor
    2023-06-29T02:23:05.82+00:00

    Hi @ネパリ サンデャ

    We can try to get siteurl from $Row. Please refer to the following script

    #Config Variables
    $CSVFile = "C:\Document.csv"
     
    Try {
        #Get the CSV file
        $CSVData = Import-CSV $CSVFile
      
        #Read CSV file and grant folder permissions
        ForEach($Row in $CSVData)
        {
            $SiteURL=$Row.SiteURL
            #Connect to PnP Online
            Connect-PnPOnline -Url $SiteURL -Interactive
            Try {
                #Get the user
                $Users = $Row.user
    
                ForEach($User in $Users)
                {
                    #Grant Permission to the Folder
                    Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
                    Set-PnPListPermission -Identity $Row.library -AddRole $Row.permission -User $User
                }
            }
            Catch {
                Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
            }
        }
    }
    Catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. MotoX80 32,326 Reputation points
    2023-06-28T13:06:40.5866667+00:00

    @ネパリ サンデャ

    @JamaliSara

    @jennyKim

    Are you guys all taking the same class? You are all asking a question about what looks like the same script.

    Maybe you want to get together and ask your teacher for help with Powershell basics. Or review each other's script to see what everyone else is doing.

    https://learn.microsoft.com/en-us/answers/questions/1319847/unexpected-token-))-in-expression-or-statement

    https://learn.microsoft.com/en-us/answers/questions/1320364/modily-site-url-into-csv-for-granting-permission-t

    https://learn.microsoft.com/en-us/answers/questions/1320418/script-not-able-to-run-in-pnp-powershell-in-ps1-fo

    0 comments No comments