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

时间:2018-09-27 10:42:10

标签: arrays string powershell csv sharepoint

我正在尝试使用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.

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

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

1 个答案:

答案 0 :(得分:0)

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

$web = get-spweb $siteUrl

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

$ query =新对象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)

import-csv temp.csv -delimiter“}” | export-csv -NoTypeInformation-路径“ result.csv”

写主机“退出”

Piero分享的答案。