Unexpected token '}' in expression or statement

JamaliSara 80 Reputation points
2023-06-27T11:00:15.0033333+00:00

I want to grant permission to document library using following script

But

Unexpected token '}' in expression or statement error have been occured no

#Config Variables
$CSVFile = "C:\Users\\Downloads\Test.csv"

#Get the data from CSV file
$CSVFile = Import-CSV $CSVFilePath
   
#Read CSV file and Grant Permission to the Folder
ForEach($Row in $CSVFile)
{
    $SiteURL=$Row.SiteURL
    #Connect to the site
    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
               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
}
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 37,156 Reputation points
    2023-06-27T13:36:24.0266667+00:00

    You just have one too many closing brackets. You may be getting misled by the indentation.

    #Config Variables
    $CSVFile = "C:\Users\\Downloads\Test.csv"
    
    #Get the data from CSV file
    $CSVFile = Import-CSV $CSVFilePath
       
    #Read CSV file and Grant Permission to the Folder
    ForEach($Row in $CSVFile)
    {
        $SiteURL=$Row.SiteURL
        #Connect to the site
        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
                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
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 48,026 Reputation points
    2023-06-27T15:12:32.0333333+00:00

    @MotoX80 is correct regarding the extra closing bracket. But even when that problem is resolved, you still have a "Catch" block in the last three lines that has no matching "Try":

    Catch {
        Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
    }
    

  3. Emily Du-MSFT 51,946 Reputation points Microsoft External Staff
    2023-06-28T02:02:28.7233333+00:00

    1.Create a csv file as following picture show.

    1

    2.Please run below PNP PowerShell.

    $SiteURL = "https://tenant.sharepoint.com/sites/emilytestcom"
    $CSVFile = "C:\pnp.csv"
    
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    $CSVData = Import-CSV $CSVFile
    
    ForEach($Row in $CSVData)
    {
         #Get the user
         $Users = $Row.user
    
         ForEach($User in $Users)
         {     
               Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
               Set-PnPListPermission -Identity $Row.library -AddRole $Row.permission -User $User
                
         }
    }   
    

    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.


  4. Emily Du-MSFT 51,946 Reputation points Microsoft External Staff
    2023-06-28T06:07:16.9333333+00:00

    1.Create a csv file as following picture show.

    1

    2.Please run below PNP PowerShell.

     $filePath = "C:\pnp.csv"
    
     $csv = Import-Csv $filePath
    
     ForEach($Row in $csv)
     {
         Connect-PnPOnline -Url $Row.SiteURL -Interactive
         Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
         Set-PnPListPermission -Identity $Row.library -AddRole $Row.permission -User $Row.user
    
     }   
    

    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.


  5. Emily Du-MSFT 51,946 Reputation points Microsoft External Staff
    2023-06-29T03:05:54.6266667+00:00

    According to the screenshot you provide, PowerShell contains some mistakes. Please following below steps.

    1.Create a csv file as following picture show.

    enter image description here

    2.Run following PowerShell by using site collection admin account.

     $filePath = "C:\pnp.csv"
    
     $csv = Import-Csv $filePath
    
     ForEach($Row in $csv)
     {
         Connect-PnPOnline -Url $Row.SiteURL -Interactive
         Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
         Set-PnPListPermission -Identity $Row.library -AddRole $Row.Permission -User $Row.user
    
     }
    

    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.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.