如何使用PowerShell仅返回一个用户和创建日期和创建日期列的日期

时间:2015-11-11 21:22:35

标签: csv powershell sharepoint-2010

我有一个脚本,可以将以下所示的网站中的库和列表中的所有项目和文件输出到CSV:

Add-PSSnapin microsoft.sharepoint.powershell
$excludeLists = @("Master Page Gallery",
                "Workflows",
                "Workflow History"
                                          )

function Get-DocInventory([string]$siteUrl) {
$web = Get-SPWeb "http://contoso.com/sites/depts/HBG"
foreach ($list in $web.Lists) {
if($excludeLists -notcontains $list.Title){

foreach ($item in $list.Items) {
foreach($version in $item.Versions){

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

$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" = ($item["Created"] -as [datetime]).DateTime
                        "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  | Export-Csv -NoTypeInformation -Path C:\AuditReport.csv

以下是脚本输出内容的示例:

enter image description here

上面的输出显示了一个名为Lions.pdf的文件,其中包含3个版本。此外,它还显示哪个用户在特定版本的文件中进行了更改。我的问题是,有没有办法在创建者列和创建日期中返回用户的名称,而不是为文件的每个版本重复一次?如果是这样,有人可以告诉我,我一直很难过。

以下是在“创建者”列中显示用户名称的所需输出,并且“创建日期”仅显示一次:

enter image description here

1 个答案:

答案 0 :(得分:1)

This may be a bit dirty, but that would be my first approach to accomplish this.

At each loop iteration, you compare the values of $authorName and $createdDate with their values during the previous iteration. If they're equal, you erase the values.

Add-PSSnapin Microsoft.Sharepoint.Powershell
$excludeLists = @("Master Page Gallery",
                  "Workflows",
                  "Workflow History")

function Get-DocInventory([string]$siteUrl) {
    $web = Get-SPWeb "http://contoso.com/sites/depts/HBG"

    foreach ($list in $web.Lists) {

        if($excludeLists -notcontains $list.Title) {

            foreach ($item in $list.Items) {

                $tempCreatedBy = ""
                $tempCreatedDate = ""

                foreach($version in $item.Versions) {

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

                    $authorName = $authorObject.LookupValue
                    if($authorName -eq $tempCreatedBy) { $authorName = "" } else { $tempCreatedBy = $authorName }

                    $createdDate = ($item["Created"] -as [datetime]).DateTime
                    if($createdDate -eq $tempCreatedDate) { $createdDate = "" } else { $tempCreatedDate = $createdDate }

                    $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" = $createdDate
                        "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  | Export-Csv -NoTypeInformation -Path C:\AuditReport.csv