Get-PnPFile : The object specified does not belong to a list. At line:4 char:9 + $File = Get-PnPFile -Url $RelativePath -AsListItem }

Sai Panat 0 Reputation points
2023-11-21T09:01:12.8+00:00

I wanted to detch files as object and cretaed relative path and use , but is not working in my work computer but works in personal computer in test enviornment

$root="https://mysite.sharepoint.com"

foreach($row in $hashTable){

$Path=$row.path

$RelativePath= $path.Substring($root.length)

$File = Get-PnPFile -Url $RelativePath -AsListItem }

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ling Zhou_MSFT 23,620 Reputation points Microsoft External Staff
    2023-11-22T02:54:35.87+00:00

    Hi @Sai Panat,

    Thank you for posting in this community.

    The object specified does not belong to a list means that SharePoint attempt to get an Item or a file (Specified by $RelativePath) that does not belong to the SPWeb.

    I suggest you compare the $RelativePath on your work computer with the one in your test environment.

    You can use the Write-Host $RelativePath command to output your URL on the command line.

    You can share your full PowerShell commands with me if your URLs match exactly but the problem persists. This will help me to better test your issue and most likely find out why the problem is occurring. Please be careful not to give out your private information.


    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.


  2. Sai Panat 0 Reputation points
    2023-11-27T08:24:25.1333333+00:00

    foreach($row in $hashTable)

    {

     

    $root="https://site.sharepoint.com"

     

    $SitePath =”http://sitepath”

     

    $arr = @()

    $Path=$row.Path

     

     

    $Checksite=$Path.Substring(0,$SitePath.length)

    If ($Checksite -eq $SitePath)

    {

    $RelativePath= $path.Substring($root.length)       #Trim to get relative path

     

    Try

    {

     

    $File = Get-PnPFile -Url $RelativePath -AsListItem  #Get File as an Object

    $RolePermissions = Get-PnPProperty -ClientObject $File -Property RoleAssignments #Get all users for the file

    $File.BreakRoleInheritance($True, $False) #Break Inheritance

    $val=0

    while($val -ne $RolePermissions.count) #Delete each user access

    {

    $RolePermissions[0].DeleteObject()

    $val=$val+1

    }

     

    $CSVFileFound = "C:\Desktop\New folder\Filefound.csv"

    $Path | Out-File $CSVFileFound  -Append

     

    }

     

    Catch{

     

    $arr += [pscustomobject]@{Error=
    

     

     

    }

    }

    Else{

    $CSVNOTINDOCONE = "C:\User\Filenotindocone.csv"

    $Path | Out-File $CSVNOTINDOCONE  -Append

     

    }


Your answer

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