Powershell Script to Check all Sharepoint Page's Webpart

  1. #Add SharePoint cmdlets reference  
  2. Add - PSSnapin microsoft.sharepoint.powershell - ErrorAction SilentlyContinue  
  3.   
  4. function enumerateWebParts($Url)  
  5. {  
  6.     $site = new - object Microsoft.SharePoint.SPSite $Url  
  7.   
  8.     foreach($web in $site.AllWebs)  
  9.     {  
  10.         if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))   
  11.         {  
  12.             $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)  
  13.             $pages = $pWeb.PagesList  
  14.   
  15.             Write - Host "Processing Web:"  
  16.             $pWeb.Url "http://bmfthnitd125:2222/" - ForegroundColor Magenta  
  17.   
  18.             foreach($item in $pages.Items)  
  19.             {  
  20.                 $fileUrl = $webUrl + "/" + $item.File.Url  
  21.                 Write - Host " "  
  22.                 $fileUrl - ForegroundColor Green  
  23.                 $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);  
  24.                 $wps = $manager.webparts  
  25.                 $wps | select - object @   
  26.                 {  
  27.                     Expression =  
  28.                     {  
  29.                         $pWeb.Url  
  30.                     };  
  31.                     Label = "Web URL"  
  32.                 }, @ {  
  33.                     Expression =  
  34.                     {  
  35.                         $fileUrl  
  36.                     };  
  37.                     Label = "Page URL"  
  38.                 }, DisplayTitle, IsVisible, @  
  39.                   {  
  40.                     Expression =   
  41.                     {  
  42.                         $_.GetType().ToString()  
  43.                     };  
  44.                     Label = "Type"  
  45.                 }  
  46.   
  47.             }  
  48.         } else  
  49.           
  50.         {  
  51.             Write - Host " Not a publishing web:"  
  52.             $web.Url ". Looking for Site Pages library." - ForegroundColor Magenta  
  53.             $pages = $null  
  54.             $pages = $web.Lists["Pages"]  
  55.   
  56.             if ($pages)  
  57.             {  
  58.                 Write - Host " "  
  59.                 $pages.Title "found." - ForegroundColor Green  
  60.   
  61.                 foreach($item in $pages.Items)  
  62.                 {  
  63.                     $fileUrl = $webUrl + "/" + $item.File.Url  
  64.                     $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);  
  65.                     $wps = $manager.webparts  
  66.                     $wps | select - object @   
  67.                     {  
  68.                         Expression =  
  69.                         {  
  70.                             $pWeb.Url  
  71.                         };  
  72.                         Label = "Web URL"  
  73.                     }, @ {  
  74.                         Expression =  
  75.                         {  
  76.                             $fileUrl  
  77.                         };  
  78.                         Label = "Page URL"  
  79.                     }, DisplayTitle, IsVisible, @   
  80.                      {  
  81.                         Expression =  
  82.                         {  
  83.                             $_.GetType().ToString()  
  84.                         };  
  85.                         Label = "Type"  
  86.                     }  
  87.   
  88.                 }  
  89.                 Write - Host "Press any key to continue..."  
  90.                 $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null  
  91.             } else {  
  92.                 Write - Host " Site Pages library not found." - ForegroundColor Red  
  93.             }  
  94.         }  
  95.         Write - Host "Press any key to continue..."  
  96.         $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null  
  97.         Write - Host "... completed processing"  
  98.         $web "..." - ForegroundColor Magenta  
  99.         Write - Host "Press any key to continue..."  
  100.         $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null  
  101.     }  
  102. }  
  103.   
  104. $row = enumerateWebParts('http://bmfthnitd125:2222/')  
  105. $row | Out - GridView  
  106.   
  107. Write - Host "Press any key to continue..."  
  108. $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null