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

[玩转系统] SharePoint Online:PowerShell 获取-设置日期字段值

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

SharePoint Online:PowerShell 获取-设置日期字段值


PowerShell 获取 SharePoint Online 中的日期列值:

日期时间字段通常在 SharePoint Online 列表和库中用于捕获和显示日期和时间。这些字段可以设置为仅显示日期或日期和时间。通过使用 SharePoint Online PowerShell,您可以轻松获取或设置列表或库中日期时间字段的值,从而帮助您自动执行任务并节省时间。

让我们了解如何使用 PowerShell 获取 SharePoint Online 中的日期和时间字段的值。这对于报告或其他自动化任务非常有用。我们还将共享 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"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

#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)

#Get All List Items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()     

#Loop through each List Item and get Date Field Value
ForEach($Item in $ListItems)
{ 
    #Get Date & Time of the Field - E.g. Wednesday, February 01, 2017 12:00:00 AM
    $Item[$FieldName]
    
    #Get Date Alone - 2/1/2017
    Get-Date($Item[$FieldName]) -format "d"

    #Get Date in specific format E.g. dd/MM/yyyy HH:mm:ss
    Get-Date ($Item[$FieldName]) -Format "dd-MMM-yyyy"
    
    #Long Date Format - E.g. Wednesday, February 01, 2017
    (Get-Date($Item[$FieldName])).ToLongDateString()

    #Get Time Alone - E.g. 8:00 PM
    (Get-Date($Item[$FieldName])).ToShortTimeString()

    #Get Long Time Format - E.g. 12:30:25 AM
    (Get-Date($Item[$FieldName])).ToLongTimeString()

    #To Universal Date time format: E.g. Tuesday, January 19, 2016 8:00:00 PM
    (Get-Date($Item[$FieldName])).ToUniversalTime()

    #Get Day-Month-Year/Hour-Minute-Second-millisecond from the Date
    (Get-Date($Item[$FieldName])).Year
}

SharePoint Online:PowerShell 设置日期字段

以下是用于更新 SharePoint Online 中的日期时间列的 PowerShell:


#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

#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 the CAML Query to filter list items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Title' /><Value Type='Text'>SharePoint 2016 Upgrade</Value>
            </Eq>
        </Where>
    </Query>
</View>"
 
#Get the List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
  
$ListItems | ForEach-Object { 
    #Set Start Date Field to 7 Days from Today's Date
    $StartDate = (Get-Date).AddDays(7)

    $_[$FieldName] = $StartDate 
    $_.Update()  
    $Ctx.ExecuteQuery()

    Write-host "Date Field Updated!"
}

如何将字符串转换为日期?
您可以使用:Get-Date “10/02/1984” 或 [DateTime]”10/02/1984”

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

取消回复欢迎 发表评论:

关灯