如何使用 PowerShell return 创建者和创建日期列只有一个用户和日期
How to return only one user and date for Created By and Created Date column using PowerShell
我有一个脚本可以将来自如下所示站点中的库和列表的所有项目和文件输出为 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
下面是脚本输出的示例:
上面的输出显示了一个名为 Lions.pdf 的文件,其中包含 3 个版本。此外,这会显示哪个用户在特定版本的文件中进行了更改。我的问题是,有没有办法 return Created By 列中的用户名和 Created Date 仅一次,而不是为文件的每个版本重复?如果是这样,谁能告诉我我是怎么度过的。
下面是所需的输出,它在“创建者”列中显示一次用户名,并且“创建日期”也只显示一次:
这可能有点脏,但这是我实现此目的的第一种方法。
在每次循环迭代中,您将 $authorName 和 $createdDate 的值与它们在上一次迭代中的值进行比较。如果它们相等,则删除这些值。
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
我有一个脚本可以将来自如下所示站点中的库和列表的所有项目和文件输出为 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
下面是脚本输出的示例:
上面的输出显示了一个名为 Lions.pdf 的文件,其中包含 3 个版本。此外,这会显示哪个用户在特定版本的文件中进行了更改。我的问题是,有没有办法 return Created By 列中的用户名和 Created Date 仅一次,而不是为文件的每个版本重复?如果是这样,谁能告诉我我是怎么度过的。
下面是所需的输出,它在“创建者”列中显示一次用户名,并且“创建日期”也只显示一次:
这可能有点脏,但这是我实现此目的的第一种方法。
在每次循环迭代中,您将 $authorName 和 $createdDate 的值与它们在上一次迭代中的值进行比较。如果它们相等,则删除这些值。
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