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

[玩转系统] SharePoint Online:使用 PowerShell 根据创建或修改日期筛选列表项

作者:精品下载站 日期:2024-12-14 21:15:39 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 根据创建或修改日期筛选列表项


要求:使用 PowerShell 根据 SharePoint Online 中创建或修改的日期值筛选和获取列表项。

[玩转系统] SharePoint Online:使用 PowerShell 根据创建或修改日期筛选列表项

如何使用 PowerShell 过滤并获取过去 30 天内创建的列表项?

下面是获取过去 30 天内在 SharePoint Online 列表中创建的列表项的 PowerShell 示例。


#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"
 
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Get the List
    $List = $Ctx.Web.lists.GetByTitle($ListName)

    #Define CAML Query to Filter
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXML = "<View>
                        <Query>
                            <Where>
                                <Gt>
                                    <FieldRef Name='Created' Type='DateTime'/>
                                    <Value Type='DateTime'>
                                        <Today OffsetDays='-30'/>
                                    </Value>
                                </Gt>
                            </Where>
                        </Query>
                    </View>"

    #Get List Items
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host -f Green "Number of List Items Found:"$ListItems.Count
    #Get Each Item's Created Date
    $ListItems | ForEach-Object { Write-host ("List Item:{0} was created on {1}" -f $_["FileLeafRef"],$_["Created"]) }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell 过滤过去 24 小时内修改的列表项:

同样,我们可以使用 PnP PowerShell 根据上次修改日期过滤列表项,如下所示:


#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Define Query to Filter
$Query= "<View>
            <Query>
                <Where>
                    <Gt>
                        <FieldRef Name='Modified' Type='DateTime'/>
                        <Value Type='DateTime' IncludeTimeValue='TRUE'>
                            <Today OffsetDays='-1'/>
                        </Value>
                    </Gt>
                </Where>
            </Query>
        </View>"

$ListItems = Get-PnPListItem -List $ListName -Query $Query
Write-host "Total Number of Items Found:"$ListItems.count

#Get Each Item's Modified Date
$ListItems | ForEach-Object { Write-host ("List Item:{0} was Modified on {1}" -f $_["FileLeafRef"],$_["Modified"]) }

总之,借助 PowerShell 可以轻松完成根据创建或修改日期检索 SharePoint Online 中的项目列表。通过执行本文中概述的步骤,您可以使用 PowerShell 根据项目的创建或修改日期快速高效地检索项目列表。

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

取消回复欢迎 发表评论:

关灯