0

I want to delete items from a document library using powershell 2.0, but not all of them, only ones that pass some of my if statements (and no I can't use CAML for all my conditions). So I have to loop through the items and then delete it. Except it takes so long to delete all the items. So I want to use background jobs. I have this code:

$spListItem = .....; # I get the item here
$task = {
    PARAM($spListItem);
    $spListItem.Delete();
}
$job1 = Start-Job -ScriptBlock $task -ArgumentList $spListItem;

but this code is not working. Does anyone know what is wrong?

ERROR:

Method invocation failed because [Deserialized.Microsoft.SharePoint.SPListItem] doesn't contain a method named 'Delete'.
    + CategoryInfo          : InvalidOperation: (Delete:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

1 Answer 1

0

Here I've found An SPListItem is not serializable

I've tried to create a workaround - it could work but it loses an advantage of "parallel" jobs because start time and memory costs are greater than "serial" running

$cmd = {
  param($url, $listName, $id) 
  Add-PSSnapin Microsoft.SharePoint.PowerShell
  $web = get-spweb -identity "$url"
  $list = $web.Lists["$listName"]   
  $list.GetItemById($id).Delete(); 
  write-host $id}

foreach  ($item in $items)
{           
  start-job -scriptblock $cmd -argumentList $url, $listName, $item.id       
}

If you'll find better solution please share it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.