首页 > 解决方案 > 使用 CSV 中的 PowerShell 更新 Sharepoint 在线列表

问题描述

我是一个powershell初学者,我遇到了一个问题。我需要更新在线共享点上的列表,其中数据来自通过 powershell 的 CSV 文件。如果该项目存在于列表中,则脚本只会在有任何差异时更新。如果该项目不存在,脚本将添加该项目我找到了一个基本上可以满足我需要的脚本,但是对于sharepoint服务器。有人可以帮我在线转换为sharepoint吗?谢谢你

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Read the CSV file
$CSVData = Import-CSV -path "C:\UserData.csv"

#Get the Web
$web = Get-SPWeb -identity "http://portal.crescent.com"

#Get the Target List
$List = $web.Lists["ExpertProfiles"]

#Iterate through each Row in the CSV
foreach ($Row in $CSVData)
{
    #Filter using CAML Query
    $CAMLQuery="<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$($Row.Name)</Value></Eq></Where>"
    $SPQuery=New-Object Microsoft.SharePoint.SPQuery
    $SPQuery.ViewAttributes = "Scope='Recursive'"  #Get all items including Items in Sub-Folders!
    $SPQuery.Query=$CAMLQuery
    $SPQuery.RowLimit = 1

    #Get the List item based on Filter
    $Item=$List.GetItems($SPQuery)[0]

    If($Item -ne $null)
    {
        #Update List Item
        $Item["Department"] = $Row.Department
        #$item.Update()
        Write-Host "Updated:"$row.Name -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.Name -f Red
    }
}

标签: powershellcsvsharepoint-online

解决方案


推荐阅读