首页 > 解决方案 > 使用 powershell 从 SharePoint 文档库中提取数据到 CSV

问题描述

我正在尝试使用 powershell 将 SharePoint 文档库的数据提取到 CSV 文件。我在 CSV 文件上得到正确的数据。但是一列,即“描述”上有更多的文本数据。所以当运行脚本时,数据进入另一行(它不是一行)。供参考在下面写了脚本,我的输出文件在下面。

Powershell 脚本

$web = get-spweb "Site Url"
$caseLib = $web.lists | where {$_.title -eq "Document Library"}
$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewFields = ""
$query.RowLimit=500000
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 "Path"
        import-csv Data.csv -delimiter "}" -Header "Number", "Description" | export-csv -NoTypeInformation -Path "C:\csvfile1.csv"
    }
}
while ($query.ListItemCollectionPosition -ne $null)
Write-Host "Exiting"

输出文件供参考

Name Description

ABCD-123 This file imported data of system.

XYZA-231 Data migrated to next session

file need to upload on another server.

System update required.

CDFC-231 New file need to create on system

XYZA-984 system creating problem.

Source code error. update new file

HGFC-453 Maintenance updated file.

我想要的输出如下

Name Description

ABCD-123 This file imported data of system.

XYZA-231 Data migrated to next session.file need to upload on another server. System update required.

CDFC-231 New file need to create on system

XYZA-984 system creating problem. Source code error. update new file.

HGFC-453 Maintenance updated file.

希望大家能理解我的要求。我希望仅在一行中需要我的描述列数据。

任何人都可以在这个脚本上帮助我或纠正我。

标签: arraysstringpowershellcsvsharepoint

解决方案


在使用 $Description 之前用空格替换换行符。

$web = get-spweb $siteUrl

$caseLib = $web.lists | 其中 {$_.title -eq $listTitle}

$query=new-object Microsoft.SharePoint.SPQuery

$query.ViewFields = ""

$query.RowLimit=500000

写输出“标题}描述”| 输出文件“temp.csv”

{

$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)
    {
        $Description = $Description -replace "`n"," " -replace "`r"," "
        $str = $caseLibItems[$x]["LinkFilename"].ToString() + '}' + $Description
    }
    else
    {
        $str = $caseLibItems[$x]["LinkFilename"].ToString()
    }
    Write-Output $str | Out-File -Append "temp.csv" 
}

} 而 ($query.ListItemCollectionPosition -ne $null)

导入-csv temp.csv -delimiter "}" | 导出-csv -NoTypeInformation -路径“result.csv”

写主机“退出”

皮耶罗分享的答案。


推荐阅读