More in keeping with PowerShell vernacular is to maintain the pipeline by using Start-SPAssignment
and Stop-SPAssignment
, eg
function foobar {
$assig = Start-SPAssignment
$x = Get-SPWeb -Identity "http://mylocalsite/Sites/test1/test2" -AssignmentCollection $assign |
ForEach-Object {$_.Lists | Where {$_.Title -EQ "someLibrary"} |
Select ID }
Stop-SPAssignment $assign
}
This is only an illustration not a verified code sample as I don't have a SharePoint server handy at the moment to test it, but it gives you the idea. If I get a chance I'll test it and edit my answer accordingly.
A further refinement is to collapse the ForEach into the pipeline
function foobar {
$assig = Start-SPAssignment
$x = Get-SPWeb -Identity "http://mylocalsite/Sites/test1/test2" -AssignmentCollection $assign |
Select-Object -ExpandProperty Lists |
Where {$_.Title -EQ "someLibrary"} |
Select ID
Stop-SPAssignment $assign
}
Once again only an illustration not a verified code sample.
The following blog post gives a good tutorial on the assignment object, PowerShell, SharePoint and Memory Leaks (Start-SPAssignment).