2

I'm trying to write a powershell script which will run all over my sharepoint 2010/2007 pages and will list their page layout and will save the results into a text file.

while running the following script which i worte, i get after acouple of minuets an error:

EnumeratePages : Exception has been thrown by the target of an invocation. At T:\listpages.ps1:75 char:15 + EnumeratePages <<<< ('http://preportal.idc.ac.il') + CategoryInfo : NotSpecified: (:) [EnumeratePages], TargetInvocationException + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,EnumeratePages

# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 

function EnumeratePages($Url) {
    $site = new-object Microsoft.SharePoint.SPSite $Url 

    foreach($web in $site.AllWebs) {
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList

            Write-Host "Processing Web:" $pWeb.Url "..." -ForegroundColor Magenta

            foreach ($item in $pages.Items) {
                $fileUrl = $pWeb.Url + $webUrl + "/" + $item.File.Url
                Write-Host "   " $fileUrl -ForegroundColor Green
                foreach ($fld in $item.Fields)
                    {
                    if($fld.Title -and $fld.InternalName -and $item[$fld.InternalName])
                    {
                     if($fld.InternalName -eq "PublishingPagelayout")
                     {
                      Write-Host "PublishingPagelayout: " + $item[$fld.InternalName].ToString()
                      Select "Page Url: ", $fileUrl, "PublishingPagelayout: ", $item[$fld.InternalName].ToString() | Format-List
                     }
                    }

                    #$spFile = $web.GetFile($fileUrl.ToString())                               
                    #if($spFile.Properties.Count -gt 0)
                    #{

                    #}
                }                
            }
        }
        else {
            Write-Host "   Not a publishing web:" $web.Url". Looking for Site Pages library." -ForegroundColor Magenta
            $pages = $null
            $pages = $web.Lists["Site Pages"]

            if ($pages) {
                Write-Host "   " $pages.Title "found." -ForegroundColor Green
                foreach ($item in $pages.Items) {
                    $fileUrl = $pWeb.Url + $webUrl + "/" + $item.File.Url                               
                    Write-Host "   " $fileUrl -ForegroundColor Green    
                    foreach ($fld in $item.Fields)
                    {
                        if($fld.Title -and $fld.InternalName -and $item[$fld.InternalName])
                        {
                         if($fld.InternalName -eq "PublishingPagelayout")
                         {
                          Write-Host "PublishingPagelayout: " + $item[$fld.InternalName].ToString()
                          Select "Page Url: ", $fileUrl, "PublishingPagelayout: ", $item[$fld.InternalName].ToString() | Format-List
                         }
                        }
                    }
                    #$spFile = $web.GetFile($fileUrl)                   
                    #if($spFile.Properties.Count -gt 0)
                    #{

                    #}
            }
            else {
                Write-Host "    Site Pages library not found." -ForegroundColor Red
            }
        }

        Write-Host "... completed processing" $web "..." -ForegroundColor Magenta
    }
}
}


$row = EnumeratePages('http://server-name')
$row > t:\SitePagesPropertiesReport2.txt

Please advise.

Hagai
  • 21
  • 1
  • 1
  • 3
  • 1
    Have you tried debugging with the ISE or PowerGUI? – Andy Arismendi Feb 26 '12 at 09:10
  • try debugging it. But you said "after a couple of minutes" - minutes of running the script? Timeouts? – Dennis G Feb 26 '12 at 15:45
  • after minutes running the script. not timeout – Hagai Feb 27 '12 at 06:24
  • @חגאי I think that you have some page that there is the problem. To find a problematic page url add try-catch like here: [powershell 2.0 try catch how to access the exception](http://stackoverflow.com/questions/2182666/powershell-2-0-try-catch-how-to-access-the-exception) – Zoli Mar 02 '12 at 13:33

1 Answers1

2

This will do it for publishing pages:

filter Get-PublishingPages { 
    $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($_)
    $query = new-object Microsoft.SharePoint.SPQuery 
    $query.ViewAttributes = "Scope='Recursive'"
    $pubweb.GetPublishingPages($query)    
} 

get-spweb $url | Get-PublishingPages | select Uri, Title, @{Name='PageLayout';Expression={$_.Layout.ServerRelativeUrl}}
Winston Fassett
  • 3,500
  • 3
  • 36
  • 29