arrays - 使用 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.
希望大家能理解我的要求。我希望仅在一行中需要我的描述列数据。
任何人都可以在这个脚本上帮助我或纠正我。
解决方案
在使用 $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”
写主机“退出”
皮耶罗分享的答案。
推荐阅读
- java - 尝试在回收站视图中实现搜索
- python - InvalidArgumentError: indices[120,0] = 3080 is not in [0, 32) [[{{node embedding_6/embedding_lookup}}]]
- wordpress - wp_mail 如何每小时发送 50 封电子邮件?
- python - Psycopg2 查询从 args 中删除 \n
- laravel-5 - 找不到对象 laravel 5.2 在 php 7.2 windows 10 上运行
- sql-server - PowerShell: REGEX 替换 SQL 语句并转义单引号
- sabre - 如何在 Sabre GetReservationRS 中识别 PQR(兑换票价)
- python - 如何将海关类型数组传递给 PostgreSQL 函数
- visual-studio-code - 在 VS Code 编辑器窗口中的行号和代码之间添加左边的空格/边距/填充
- c++ - 为什么循环索引会超出循环范围