Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm using a PowerShell script to update page titles and PublishingPageLayout's in a particular web that has 391 pages. However, I'm finding that the longer the script runs, the slower each pages is updated, so I'm wondering how I can speed things up.

Here's my script (cut down for readability)

function ProcessSubWebs($currentWeb)
{
    if([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($currentWeb))
    {            
        $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($currentWeb)
        $publishingPages = $publishingWeb.GetPublishingPages()
        foreach ($publishingPage in $publishingPages)
        {
            if($publishingPage.ListItem.File.CheckOutStatus -eq "None")
            {
                UpdatePage -page $publishingPage
            }
        }

        foreach($sub in $currentWeb.Webs)
        {
            if($sub.Webs.Count -gt 0)
            {
                ProcessSubWebs($sub)    
            }
        }
        Write-Host -ForegroundColor red "FINISHED"
    }
    else
    {
        Write-Host -Foregroundcolor Red "^ not a publishing site" 
    }
}

function UpdatePage($page)
{
    $page.CheckOut();
    Write-Host -Foregroundcolor green $page.Url;
    $NewPageTitle = $page.Title.Replace("Questionmark - ","");
    $page.Title = $NewPageTitle;

    $NewPageLayout = "/_catalogs/masterpage/QM_KB_NoCommentsRatings.aspx, QM KB Article No Comments or Ratings";
    $page.ListItem.Properties["PublishingPageLayout"] = $NewPageLayout;
    $page.ListItem.Update();
    $page.CheckIn("nothing");
    $page.ListItem.File.Approve("Updated PageLayout and Title");
}

ProcessSubWebs(Get-SPWeb -identity http://my.server.com/products/help/v3/kbase)

I'm wondering if there is a way to 'close' or 'dispose' the page object? I'd tried just using .Close() and .Dispose() but they return errors. Is there another technique I can use to close each page after it's Approved, to make the script run faster?

I'm about to run the script on over 500 pages in multiple webs/sites.. but even if I find out afterwards, I think this would be useful info to know.

share|improve this question
1  
There is no need to call Dispose method on Page because the page does not implement IDispose interface. –  Ashish Patel Feb 11 '12 at 16:49
    
OK, that make sense. So the question is, why does my script get slower and slower the longer it run, and the more pages it approves, and is there anyway to speed it up, or at least keep it running as quickly as it does when I first execute it? –  QMKevin Feb 11 '12 at 17:04
1  
One thing you can try is Get SPSite using Get-SPSite and then use SPSite.AllWebs which gives all Webs recursive, so no need to write a recursive function. –  Ashish Patel Feb 11 '12 at 17:19
    
I'll try that, but this code is meant to be for a specific site within the site collection, rather than all sites (or, to use PowerShell terms.. a single web and it's sub-webs rather than the entire site) –  QMKevin Feb 11 '12 at 17:29

1 Answer 1

up vote 3 down vote accepted

You're not disposing $sub on each iteration. Do this outside your if test in the foreach.

share|improve this answer
    
Thanks - that kept the script running reasonably smoothly :) –  QMKevin Feb 12 '12 at 1:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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