2012-06-20 34 views
3

如何使用PowerShell修改SharePoint列表中项目的值?当我尝试以下方法:如何使用PowerShell修改SharePoint列表中项目的值

$splist.GetItems() | ForEach-Object{ 
#Write-Host $_["Item"] 

if($_["Item"] -eq $null){ 
    $SPFileCollection = $folder.Files 
    $upFile=Get-ChildItem D:\Tools\SP-scripts\MDNSO-template.aspx 
    $tmpValue = $_["Title"] 
    $filename = "EM_Doc_Library\$tmpValue" 
    $SPFileCollection.Add("EM_Doc_Library\$tmpValue",$upFile.OpenRead(),$false) 
    Write-Host "creating the file" 
    Write-Host $_["Title"] 
    $_["Item"] = "MDNSO-<$tmpValue>" 
} 
} 

它说,unable to index into an object of type SPlistItem

+0

哪一行引发错误? –

+0

fucntion添加(),它说异常调用添加3个参数 – LifeScript

+0

你能帮我吗?我可以找出几个小时! – LifeScript

回答

9

这里是什么,你可以尝试做一个总结:

$spWeb = Get-SPWeb -Identity http://yourdomain/sites/config 
$spList = $spWeb.Lists["AppSettings"] 
$spItem = $spList.GetItemById(10013) //or another way that you prefer. 
$spItem["Name"] = "MyName" 
$spItem.Update() 

欲了解更多信息,你应该看看这篇博客。它包含了如何添加,更新和删除列表项使用PowerShell的例子:http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html

+0

请提供链接包含的摘要。 – Dason

+0

提供了一个总结... – Pantani

+0

不要忘记结束它:'$ spItem.Update()' –

0

使用PowerShell我的脚本做的非常完美,从 social.msdn.microsoft.com/Forums

下面是示例代码,Tyepe“YES”进行一次提示。 和替换的仅有的两个变量:(urlToTheYourSite & YourListNameToDelete)

write-host "This will delete data, type YES to continue" 
$retval = read-host 
if ($retval -ne "YES") 
{ 
    write-host "exiting - you did not type yes" -foregroundcolor green 
    exit 
} 
write-host "continuing" 

$web = get-spweb https://urlToTheYourSite 
$list = $web.lists | where {$_.title -eq "YourListNameToDelete"} 
Write-host "List $($list.title) has $($list.items.count) entries" 
$items = $list.items 

foreach ($item in $items) 

{ 
    Write-host " Say Goodbye to $($item.id)" -foregroundcolor red 

    $list.getitembyid($Item.id).Delete() 
} 
相关问题