当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用关键字查询 PowerShell 查找所有文档

作者:精品下载站 日期:2024-12-14 14:54:40 浏览:13 分类:玩电脑

SharePoint Online:使用关键字查询 PowerShell 查找所有文档


要求:获取SharePoint Online站点中的所有文档并将其导出为CSV。

PowerShell 在 SharePoint Online 中使用关键字查询查找所有文档

SharePoint Online 提供关键字查询语言 (KQL),这是一种用于构建搜索查询的强大而灵活的语法。以下是在 SharePoint Online 中使用 KQL 的示例: 用户想要获取近年来未更新的所有文档的列表。因此,PowerShell 脚本获取 SharePoint Online 网站集中按“上次修改日期”排序的所有文档。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Search.dll"

#Config Variables
$SiteURL="https://Crescent.sharepoint.com/"
$SearchQuery= "path:https://Crescent.sharepoint.com AND IsDocument:true AND (NOT FileType:aspx)"
$CSVFile = "C:\Temp\SearchResults.csv"

Try {
    $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
    
    #Define Keyword
    $KeywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($Ctx)
    $KeywordQuery.QueryText = $SearchQuery
    $KeywordQuery.RowLimit  = 500
    $keywordQuery.SelectProperties.Add("CreatedBy")
    $keywordQuery.SelectProperties.Add("LastModifiedTime")
    $keywordQuery.SortList.Add("LastModifiedTime","Asc")

    #Execute Search        
    $SearchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($Ctx)
    $SearchResults = $SearchExecutor.ExecuteQuery($KeywordQuery)
    $Ctx.ExecuteQuery()

    Write-host "Search Results Found:"$SearchResults.Value[0].ResultRows.Count

    #Get Search Results
    If($SearchResults)
    {
        $Results = @()
        foreach($Result in $SearchResults.Value[0].ResultRows)
        {
            $Results += New-Object PSObject -Property @{
                        'Document Name' =  $Result["Title"]
                        'URL' = $Result["Path"]
                        'Created By' = $Result["CreatedBy"]  
                        'Last Modified' = $Result["LastModifiedTime"]               
                        }
        }
        $Results
        #Export search results to CSV
        $Results | Export-Csv $CSVFile -NoTypeInformation
        Write-Host -f Green "Search Results Exported to CSV File!"
    }
}
Catch {
    write-host -f Red "Error Getting Search Results!" $_.Exception.Message
} 

结果 :

[玩转系统] SharePoint Online:使用关键字查询 PowerShell 查找所有文档

通过在 SharePoint Online 中使用 KQL,您可以创建复杂且有针对性的搜索查询,以快速轻松地查找所需的信息。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯