2012-06-20 74 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() 
} 
相關問題