首页 > 解决方案 > Powershell脚本保存在UTC时间

问题描述

我已经实现了用于将数据从共享点提取到 CSV 文件的 powershell 脚本。目前我的文件正在按照我的时区保存。但我想用UTC时间保存文件。表示在脚本下我想将时间格式转换为 UTC 时间。谁能更正我的脚本。在我正在使用的脚本行下面。

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = get-spweb http://testsharepoint/sharepoint/
$caseLib = $web.lists | where {$_.title -eq "Library"}

$query=new-object Microsoft.SharePoint.SPQuery 

$query.ViewFields = "<FieldRef Name='LinkFilename'/><FieldRef Name='DocumentSetDescription'/>"

$query.RowLimit=5000

$ListName1 = "Test_"
$ExportFolder1 = “C:\export\”
$ExportName1 = Get-Date -f “yyyyMMdd”
$ExportPath1 = $ExportFolder1 + $ListName1 + $ExportName1 + “.csv”

$ListName = "Data_"
$ExportFolder = “\\servername\Data\”
$ExportName = Get-Date -f “yyyyMMdd”
$ExportPath = $ExportFolder + $ListName + $ExportName + “.csv”

do
{
$caseLibItems=$caseLib.GetItems($query)
$query.ListItemCollectionPosition=$caseLibItems.ListItemCollectionPosition
$listItemsTotal = $caseLibItems.Count
$x = 0
for($x=0;$x -lt $listItemsTotal; $x++)
{
$Description = $caseLibItems[$x]["DocumentSetDescription"]
$str = ""
if('$Description; -ne $null)
$str = $caseLibItems[$x]["LinkFilename"].ToString() + '}' + $Description 
  }
else
 {
$str = $caseLibItems[$x]["LinkFilename"].ToString()
}
Write-Output $str| Out-File $ExportPath1 -Append
}

标签: powershellcsvsharepointtime

解决方案


很简单,因为 PowerShell 有一个.ToUniversalTime()方法。在不更改其他所有内容的情况下更改以下内容将是最简单的。

$ExportName = (Get-Date).ToUniversalTime().ToString("yyyyMMdd")
$ExportName1 = (Get-Date).ToUniversalTime().ToString("yyyyMMdd")

推荐阅读