脚本输出显示修改日期列Powershell的时间不一致

时间:2015-11-12 20:52:16

标签: csv powershell datetime sharepoint-2010 last-modified

我有一个脚本,可以将以下所示网站中的所有项目和所有文件输出到CSV。此脚本还显示文件的当前版本和先前版本:

Add-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue

function Get-DocInventory([string]$siteUrl)
{
    $web = Get-SPWeb $siteUrl

    $excludeLists = @("Master Page Gallery",
                "Workflows",
                "Workflow History"
                 )

    foreach ($list in $web.Lists) {
        if($excludeLists -notcontains $list.Title)
        {
            foreach ($item in $list.Items) 
            {
                $firstPass = $true

                foreach($version in $item.Versions)
                {
                    $created = ""
                    $authorName = ""

                    if($firstPass)
                    {
                        $created = ($item["Created"] -as [datetime]).DateTime

                        $personField = $item.Fields.GetField("Author");
                        $authorObject = $personField.GetFieldValue($item["Author"]);
                        $authorName = $authorObject.LookupValue;                    

                        $firstPass = $false
                    }

                    $userField = $version.Fields.GetField("Editor");
                    $editorObject = $userField.GetFieldValue($version["Editor"]);
                    $editorName = $editorObject.LookupValue;

                    $localOffset = +5;
                    $modified = $version["Modified"] -as [datetime];

                    if($modified.IsDaylightSavingTime())
                    {
                        $localOffset += 1;
                    }

                    $modifiedLocal = $modified.addHours(-$localOffset);

                    $data = @{
                        "Version" = $version.VersionLabel
                        "List Name" = $list.Title
                        "Created By" = $authorName 
                        "Created Date" = $created
                        "Modified By" = $editorName
                        "Modified Date" = ($modifiedLocal -as[datetime]).DateTime
                        "Item Name" = $item.Name
                    }

                    New-Object PSObject -Property $data | Select "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date"
                }
            }
            $web.Dispose();
        }
    }
}

Get-DocInventory -siteUrl "http://contoso.com/sites/depts/HBG" | Export-Csv -NoTypeInformation -Path C:\AuditReport.csv

以下是脚本的示例输出:

enter image description here

此外,以下是excel格式的此文件的版本历史记录:

enter image description here

当我比较输出和版本历史时,我注意到版本1-3显示时间提前1小时。另一方面,文件的4-7版显示正确的日期/时间。

我当时认为可能是因为夏令时了,所以我评论了这一部分:

if($modified.IsDaylightSavingTime())
                        {
                            $localOffset += 1;
                        }

但我仍然得到相同的输出。某些文件会出现此问题,而其他文件则显示正确的日期/时间。我试图研究,但无法得出答案。有人可以帮我解决这个问题吗?

0 个答案:

没有答案