Csv 如何使用PowerShell仅返回Created By和Created date列的一个用户和日期
我有一个脚本,可以将网站中库和列表中的所有项目和文件输出到CSV,如下所示:Csv 如何使用PowerShell仅返回Created By和Created date列的一个用户和日期,csv,powershell,sharepoint-2010,Csv,Powershell,Sharepoint 2010,我有一个脚本,可以将网站中库和列表中的所有项目和文件输出到CSV,如下所示: Add-PSSnapin microsoft.sharepoint.powershell $excludeLists = @("Master Page Gallery", "Workflows", "Workflow History" ) function Get-Doc
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个版本。此外,这将显示在文件的特定版本中进行更改的用户。我的问题是,是否有一种方法可以在“创建人”列中只返回一次用户名和创建日期,而不是在文件的每个版本中重复?如果是这样的话,有人能告诉我我是如何度过这段艰难的时光的吗
以下是所需的输出,在“创建人”列中显示用户名一次,创建日期也只显示一次:
这可能有点脏,但这将是我实现这一目标的第一个方法 在每次循环迭代中,将$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
这正是我要找的!谢谢你的解释,非常感谢!