使用 powershell 将数据从 SharePoint 文档库提取到 CSV

使用 powershell 将数据从 SharePoint 文档库提取到 CSV

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

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.

希望你们能理解我的要求。我希望描述列数据只出现在一行中。

答案1

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

$web = get-spweb $siteUrl
$caseLib = $web.lists | where {$_.title -eq $listTitle}
$query=new-object Microsoft.SharePoint.SPQuery 
$query.ViewFields = "<FieldRef Name='LinkFilename'/><FieldRef Name='DocumentSetDescription'/>"
$query.RowLimit=500000

Write-Output "Header}Description" | Out-File "temp.csv" 

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)
        {
            ### Insert the line below to remove line breaks ###############
            $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" 
    }
} while ($query.ListItemCollectionPosition -ne $null)

import-csv temp.csv -delimiter "}" | export-csv -NoTypeInformation  -Path "result.csv"

Write-Host "Exiting"

Piero 分享的答案。

相关内容