I had a script I was working on a while back that got put on the back burner, but now I'm trying to finish it up, and I thought I had reached that point. I was running this script on our development environment with the desired results. However, once I switched to our staging environment it blows up on the foreach that goes through all subwebs of the specified Web. What gives? I don't see why it would blow up, but I'm also new to PowerShell.. so maybe it's something obvious?

#Add SharePoint assemblies    

function add-ContentEditorWebPart($siteUrl, $webUrl)    
    #Set up default variables
    $siteUrl = $siteUrl.ToString()        
    $webUrl = $webUrl.ToString()        
    #$content is going to be the contents of the content editor webpart [google remarketing tag in this case]

    $content = '<!--scripts goes here -->'        
    $webpartzone = "bottomWebPart"        
    $index = 0        
    $comment = "Google Remarketing Script Added"    

    #Get site and web objects        
    $site = new-Object Microsoft.SharePoint.SPSite($siteUrl)        
    $web = $site.OpenWeb($webUrl)        
    foreach ($subweb in $web.Webs)        
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($subweb)) {        
            write-host "Reviewing pages in"$subweb.Title"site...."

            #Get the Publishing Web and pages within it        
            $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($subweb)        
            $publishingPages = $publishingWeb.GetPublishingPages()

            foreach ($publishingPage in $publishingPages)        
                $file = $subweb.GetFile($publishingPage.Uri.ToString())
                #Check to ensure the page isn't checked out

                if ($publishingPage.ListItem.File.CheckOutStatus -eq "None")
                    Write-Host "Checking out page: " $publishingPage.Title

                    ### add webpart to page here                        
                    $webpartmanager = $subweb.GetLimitedWebPartManager($publishingPage.Uri, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

                    $webpart = new-object  Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
                    $webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
                    $webpart.Title = 'Google Remarketing'

                    $docXml = New-Object System.Xml.XmlDocument
                    $contentXml = $docXml.CreateElement("div");

                    $webpart.Content = $contentXml;

                    $webpartmanager.AddWebPart($webpart, $webpartzone, $index); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place


                    $file.CheckIn($comment, [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
                    $file.Approve("Approved automatically by PowerShell script.")
                    write-host "Added Content Editor Webpart and checked in file: " $publishingPage.Title "`n"                                                                 
                    #Notify user that the page is checked out and cannot be modified    
                    write-host "Page"$publishingPage.Title"is currently checked out to"$publishingPage.ListItem.File.CheckedOutBy"and cannot be modified"    

            #Notify user that the site is not a publishing site    
            write-host $subweb.Title"is not a publishing site"    

    #Dispose of Site object

The error that I get is the following:

An exception was thrown when trying to enumerate the collection: "The method or operation is not implemented.". At D:\scripts\AddContentEditor.ps1:44 char:17 + foreach <<<< ($publishingPage in $publishingPages)

Note: I removed the 'content' as to not post possibly sensitive information. So the line number of the error isn't correct.. but you can find that line by finding ($publishingPage in $publishingPages).

2 Answers 2


Usually this kind of error is the result of your current user not having access to one of the sub webs. You should either elevate permissions or use the sub webs for the current user.

  • 1
    That very well could be, but I'm logged in as our 'service account' which is supposed to have the highest privelages. I'm not sure how to check though.
    – snowBlind
    Commented Apr 19, 2012 at 18:24
  • I just realized I forgot to post the error along with the code.
    – snowBlind
    Commented Apr 19, 2012 at 19:24

$publishingWeb.GetPublishingPages() may return $null so you should check for that before looping through the pages

  • I tried to do a write host on $publishingPages, which was the variable that I stored whatever $publishingWeb.GetPublishingPages() returned, but I get the following error: Write-Host : The method or operation is not implemented. At D:\scripts\AddContentEditor.ps1:45 char:14 + write-host <<<< $publishingPages
    – snowBlind
    Commented Apr 23, 2012 at 17:51

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.