I MODIFIED THE CAML AND IT'S NOW WORKING AND DOES NOT LOOP THROUGH ALL THE ITEMS IN A LIST.
I am looking for list items where workflow status equal "Error Occurred". I have the following code and it's working fine. However, there are 124,000 items in my list and the code goes through each item in the list. Is there a way to limit this to items where workflow status (workflow name is "Archive Data") is "Error Occurred". Please suggest. I added following CAML and changed the code but it still returns all the records. I tested the CAML with u2u and it returns 144 records. I added write-host to get a count and commented out the foreach for test.
# Terminates all workflow in a give list
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb "http://inside.nov.com/sales/SouthEast"
$web.AllowUnsafeUpdates = $true
# $list name is list display name
$list = $web.Lists["New Orders"]
$CAML = '<Where>
<Eq>
<FieldRef Name="ArchiveData" />
<Value Type="WorkflowStatus">3</Value>
</Eq>
</Where>'
$query = new-object Microsoft.SharePoint.SPQuery
$query.Query = $CAML
$listitems = $list.GetItems($query)
Write-Host "Count: " $listitems.Count
#foreach ($item in $listItems)
#{
# foreach ($wf in $item.Workflows)
# {
# #Cancel Workflows
# [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)
# }
#}
$web.Dispose()