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\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 เพื่อตั้งค่าวันที่
นี่คือ PowerShell เพื่ออัปเดตคอลัมน์วันที่และเวลาใน SharePoint Online:
#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”