【问题标题】:SharePoint OnLine - PowerShell PNP - List folders that have attributeSharePoint Online - PowerShell PNP - 列出具有属性的文件夹
【发布时间】:2021-12-13 01:57:14
【问题描述】:
$siteURL="https://URL"
$folder="/FOLDER"

Function GetFolders($folderUrl)  
{      
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
    # Loop through the folders  
    foreach($folder in $folderColl)  
    {                      
       write-host -ForegroundColor Green $folder.Name  
    }           
}  
  
# Connect to SharePoint Online site  
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
  
# Call the functions  
GetFolders($folder)

我正在尝试从下面的选项卡中列出包含字符串的文件夹。例如,字符串是“important_documents”

我已设法列出所有,但缺少完成脚本的 powershell 知识 :)

【问题讨论】:

    标签: powershell sharepoint-online


    【解决方案1】:

    我的文档库:

    请以管理员身份运行以下 PowerShell 脚本:

    #Variables
    $SiteURL = "https://xxxx.sharepoint.com/sites/sitename"
    $LibraryName = "{libraryname}"
    
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
       
        #Get the Library
        $Library = $Ctx.web.Lists.GetByTitle($LibraryName)
        $Folders = $Library.RootFolder.Folders
        $Ctx.Load($Folders)
        $Ctx.executeQuery()
     
        #Get the Folder by Name
        $Folder = $Folders | Where {$_.Name -like "*important_documents*"} 
        foreach($Fol in $Folder){
        #Get Some Folder Properties
            Write-host "Folder Name:"$Fol.Name
            Write-host -f Green "Total Number of Files in the Folder:"$Fol.ItemCount
            Write-host ""
            }   
    }
    Catch {
        write-host -f Red "Error Getting Folder!" $_.Exception.Message
    }
    

    【讨论】:

    • 没关系,我已经设法显示包含一些字符串的文件夹...但是当我更改“过滤器”ibb.co/wRCqKDY 时,我的脚本需要显示文档
    • 你的意思是要根据你指定的View(过滤器)显示对应的文档吗?
    • 没错。我想显示并稍后编辑具有 View(filter) important_documents 的文档的权限
    • 添加更多信息 我有 1000 多个文件夹。它们都有子文件夹。脚本需要检查所有文件夹/子文件夹,并且只显示文件夹/子文件夹名称中包含“important_documents”的文件夹/子文件夹。
    【解决方案2】:
    $siteURL="https://URL"
    $folder="/FOLDER"
    
    Function GetFolders($folderUrl)  
    {      
        $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder
        # Loop through the folders  
        foreach($folder in $folderColl)  
        {                      
            $newFolderURL= $folderUrl+"/"+$folder.Name
            GetFolders($newFolderURL)
            $a = $newFolderURL
            
            if($a -like "*important_documents*"){
            write-host -ForegroundColor Green $a
            Get-PnPList | Where {$_.Hidden -eq $false}
            }
        }
    }  
      
    # Connect to SharePoint Online site  
    Connect-PnPOnline –Url $siteURL –Credentials (Get-Credential)  
      
    # Call the functions  
    GetFolders($folder)  
    

    更新:我已经设法列出了所有包含重要文档的文件夹和子文件夹。在 mu 的情况下,它运行缓慢,因为我有很多子文件夹。

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2018-03-05
      • 1970-01-01
      • 2021-03-18
      • 2020-10-08
      • 2022-12-05
      相关资源
      最近更新 更多