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


0

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

希望你们了解我的要求。我想说明列数据只需要我一行。

Answers:


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"

皮耶罗分享的答案。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.