First of all, you don’t actually want Dispose to be called here anyway - when you call Dispose on an SPSite instance, all websites returned through OpenWeb are also deleted because they are “owned” by these SPSite!
One of the models used by the SharePoint 2010 cmdlets is a kind of “deferred deletion”, which means that the instances of SPWeb are not located until the pipeline in which they are involved is complete. This works as follows:
function Get-SPWeb { param([uri]$Url) begin {
Now here is how it works in practice (this is one line):
ps> get-spweb "http://localhost/sites/test" | foreach-object { $_.Title = "New Name"; $_.update() }
The first part will receive a single instance of SPWeb and pass it the ForEach-Object parts. Only when foreach completes (and finishes changing the name of the website) will the corresponding End block be called in get-spweb , which hosts the website and network. The important thing is that the entire pipeline is one block of code that is executed in a single call.
This will not work interactively:
ps> $w = get-spweb "http://localhost/sites/test"
So in this last example you will have to use a different get-spweb implementation (one that omits the final block or suppresses it using the switch parameter), and then you have to manage the site yourself.
Another important detail: interactive work in powershell with sharepoint objects will lead to a memory leak. By default, powershell runs in MTA (multi-threaded apartment) and will use the thread pool to execute its commands. Each line entered will use a different thread. Each time you access a COM object with a different thread, you will leak some memory from the unmanaged heap, since a new heap is allocated for the context switch (without the old one being freed.) This can be fixed by running powershell.exe with -STA switch. This ensures that all commands and pipelines are executed on the same thread, avoiding memory leaks. However, just closing the powershell console will return all the memory again, but long scripts may starve on your memory servers if you are not careful about flattening SharePoint (anything else you don't like to get hunger from a working set.) Here why the single-line approach works so well in the first example: the object is distributed and placed in one pipeline, and by extension - the same stream. No leakage.
x0n
source share