Find Default Page And Approve It Using Powershell In SPO

Basically in SPO, default.aspx are discontinued however while migration default pages also get migrated. Here this piece of code finding default pages as it may set up Homepage of sites.

Here, we are finding those default pages across site and subsites and approving it along with the particular user who created or modified.
  1. #Passing inputs through CSV file  
  2. # Pass Site URL and Library Name as Inputs.  
  3. Param(  
  4.     [Parameter(Mandatory = $true)]  
  5.     [string] $Csv)  
  6. # Pass your DLL 's   
  7. Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  8. Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  9. Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"  
  10. Import - Module Microsoft.Online.SharePoint.PowerShell  
  11. # Not required  
  12. for on premise site - Start  
  13. $userId = "[email protected]"  
  14. $pwd = Read - Host - Prompt "Enter password" - AsSecureString  
  15. $creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
  16. $root = Import - Csv $Csv  
  17. foreach($record in $root) {  
  18.     $TargetSite = $record.SharePointUrl  
  19.     $LibraryName = $record.LibraryName  
  20.     $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($TargetSite)  
  21.     # Not required  
  22.     for on premise site - Start  
  23.     $ctx.credentials = $creds  
  24.     # Iterating across subsites and finding  
  25.     default pages.  
  26.     $web = $ctx.Web  
  27.     $ctx.Load($web.Webs)  
  28.     $ctx.ExecuteQuery()  
  29.     if ($web.Webs.Count - gt 0) {  
  30.         foreach($AllWeb in $web.Webs) {  
  31.             try {#  
  32.                 Get all files from the Library  
  33.                 $List = $AllWeb.Lists.GetByTitle($LibraryName)  
  34.                 $q = New - Object Microsoft.SharePoint.Client.CamlQuery  
  35.                 $q.ViewXml = "<View Scope='RecursiveAll' />"  
  36.                 $Items = $List.GetItems($q)  
  37.                 $ctx.Load($Items)  
  38.                 $ctx.ExecuteQuery()  
  39.                 $FileName = "default.aspx"  
  40.                 Foreach($Item in $Items) {  
  41.                     $File = $Item["FileRef"].Substring($Item["FileRef"].LastIndexOf("/") + 1)  
  42.                     if ($File - eq $FileName) {  
  43.                         if ($Item["_ModerationStatus"] - eq "2" - and($Item["Editor"].LookupValue - eq("xxxUserName") - or $Item["Editor"].LookupValue - eq("xxxUserName"))) {  
  44.                             $Item["_ModerationStatus"] = "0"  
  45.                             $Item.Update()  
  46.                             $ctx.ExecuteQuery()  
  47.                             write - host "done"  
  48.                         }  
  49.                     }  
  50.                 }  
  51.             } catch {  
  52.                 $_ | Out - File errors.txt - Append  
  53.             }  
  54.         }  
  55.     }  
  56.     if ($web.Webs.Count - eq 0) {  
  57.         try {  
  58.             #Get all files from the Library  
  59.             $List = $AllWeb.Lists.GetByTitle($LibraryName)  
  60.             $q = New - Object Microsoft.SharePoint.Client.CamlQuery  
  61.             $q.ViewXml = "<View Scope='RecursiveAll' />"  
  62.             $Items = $List.GetItems($q)  
  63.             $ctx.Load($Items)  
  64.             $ctx.ExecuteQuery()  
  65.             $FileName = "default.aspx"  
  66.             Foreach($Item in $Items) {  
  67.                 $File = $Item["FileRef"].Substring($Item["FileRef"].LastIndexOf("/") + 1)  
  68.                 if ($File - eq $FileName) {  
  69.                     if ($Item["_ModerationStatus"] - eq "2" - and($Item["Editor"].LookupValue - eq("xxxUserName") - or $Item["Editor"].LookupValue - eq("xxxUserName"))) {  
  70.                         $Item["_ModerationStatus"] = "0"  
  71.                         $Item.Update()  
  72.                         $ctx.ExecuteQuery()  
  73.                         write - host "done"  
  74.                     }  
  75.                 }  
  76.             }  
  77.         } catch {  
  78.             $_ | Out - File errors.txt - Append  
  79.         }  
  80.     }  
  81. }  
Moderation Status is a 4-byte integer indicating the moderation approval status of a list item.

Configurations can require moderation approval to publish a list item or allow automatic approval. A published list item MUST have a Moderation Status of 0. The following are all possible valid values for Moderation Status.

ValueDescription
0The list item is approved.
1The list item has been denied approval.
2The list item is pending approval.
3The list item is in the draft or checked out state.
4The list item is scheduled for automatic approval at a future date.

RecursiveAll gets all files and all folders under the specified location. 

Hope this may help you! Happy Coding.