2

I've written a PowerShell script to import and read a csv file and then update a corresponding SharePoint 2010 Document Library item with the attributes found in the csv.

Everything works perfectly but I have the added complexity of needing to do this with over a million items. I've tested with 5,000 items and the entire process takes a few hours to complete. At this rate, it'll take over a month to do all 1 million+ items.

Is there a better way to write this script or am I stuck due to the insane number of items? My current script is below. Keep in mind that it is working... just slowly.

    $xlCSV=6
    $xls='z:\shared\Metadata.xlsx'
    $csv='z:\shared\Metadata.csv'
    $xl=New-Object -com 'Excel.Application'
    $wb=$xl.workbooks.open($xls)
    $wb.SaveAs($csv,$xlCSV)
    $xl.displayalerts=$False

    $SPAssignment = Start-SPAssignment
    $SPWeb = Get-SPWeb "http://client.contoso.com" -AssignmentCollection $spAssignment
    $SPList = $SPWeb.Lists["Shared Documents"]

    foreach($i in Import-CSV 'z:\shared\Metadata.csv')
    {
    $SPItem = $SPList.Items | Where { $_["Name"] -eq $i."Document Name" }
    $SPItem["Metadata1"] = $i."META 1"
    $SPItem["Metadata2"] = $i."META 2"
    $SPItem["Metadata3"] = $i."META 3"
    $SPItem["Metadata4"] = $i."META 4"
    $SPItem["Metadata5"] = $i."META 5"
    $SPItem["Metadata6"] = $i."META 6"
    $SPItem["Metadata7"] = $i."META 7"
    $SPItem["Metadata8"] = $i."META 8"
    $SPItem.Update()
    }

    Stop-SPAssignment $SPAssignment
    $xl.quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
    $xl = $null
    Start-Sleep -s 10
    Remove-Item "z:\shared\Metadata.csv" -force
4

1 回答 1

0

也许将其分解为n个 csv,并使用后台作业并行运行相同的操作,这里有一个答案。因此,将您的主力代码放入函数或脚本块中,并为每个后台作业传递一个指向 csv 文件的链接以进行上传。我目前无权访问共享点服务器来测试任何代码,但这可能是您想要的。

于 2013-09-25T20:15:11.960 回答