list 使用 PowerShell 更新列表中的所有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9946801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update all items in a list using PowerShell
提问by Josh Henninger
I have a SharePoint 2010 list with around 500 items. I need to create a PowerShell script that will call ALL of these list items, and then update a specific column (we'll call it 'Number') for EACH item.
我有一个包含大约 500 个项目的 SharePoint 2010 列表。我需要创建一个 PowerShell 脚本来调用所有这些列表项,然后为每个项更新一个特定的列(我们将其称为“编号”)。
The column (Number) that needs to be updated for each item is a Number column. I simply need to insert a random number into each list item, ranging from 0-100. It doesn't matter if numbers are repeated, but they need to be chosen at random.
每个项目需要更新的列(编号)是一个编号列。我只需要在每个列表项中插入一个随机数,范围从 0 到 100。数字是否重复无关紧要,但需要随机选择。
I am very new to PowerShell and am still trying to understand the fundamentals. If someone could provide me with assistance around how to configure this cmdlet, that would be greatly appreciated!
我对 PowerShell 非常陌生,并且仍在尝试了解基本原理。如果有人可以为我提供有关如何配置此 cmdlet 的帮助,我将不胜感激!
Thanks so much!
非常感谢!
-Josh
-乔希
回答by Stefan
Assuming the list you want to update is located at http://YouServer/ListLocation/Lists/TheList:
假设您要更新的列表位于http://YouServer/ListLocation/Lists/TheList:
$web = Get-SPWeb http://YourServer/ListLocation
$list = $web.Lists["TheList"]
foreach ($item in $list.Items)
{
$item["Number"] = Get-Random -Min 0 -Max 100;
$item.Update();
}
You need to execute this code in the SharePoint 2010 Management Shellor add the SharePoint PowerShell snap-in manually:
您需要在SharePoint 2010 Management Shell 中执行此代码或手动添加 SharePoint PowerShell 管理单元:
Add-PSSnapin Microsoft.SharePoint.PowerShell
回答by Joey
You may try something like the following:
您可以尝试以下操作:
$list | ForEach-Object { $_.Number = Get-Random -Min 0 -Max 100 }