As you probably know for copying publishing pages with all web parts in Sharepoint we may use Sharepoint Designer. It is convenient way when you need to copy one page, but what if there are a lot of pages and copying them manually will take a lot of time? In this case we may use PowerShell. The tricky moment here is that when you copy page in Sharepoint Designer it uses author.dll RPC call (you may check it Fiddler). So we will do the same: construct http request with necessary parameters and authenticate ourselves using Sharepoint Online cookies. In our example we will copy standard search results page results.aspx on search sub to the same Pages doclib with different name:
Here we used additional helper function Load-CSOMProperties from Gary Lapointe which allows to load object with specified properties in PowerShell (in C# we may use ClientContext.Load() for that with properties defined via lambda expressions). I won’t copy it here, you may just copy it from github to your ps1 file as is.
Lets check what script does. At first we specify username and password and initialize ClientContext (lines 137-153). The we pass context and site objects to Add-New-Search-Results-Page, also pass sub site url “en/search”, source page “results.aspx” and target page “fooresults.aspx” (line 155). Inside function we initialize sub site and get Pages doclib (lines 63-91). After that we iterate through all pages and find specified source page and at the same time check that target page doesn’t exist yet (lines 93-132). Having source page url and target page urls in form “Pages/results.aspx” and “Pages/fooresults.aspx” and additional parameter called service name which is needed for RPC post value in form “/search” we make call to Copy-Page function which makes actual copy (lines 130-134). In Copy-Page we construct http request object (lines 21-33). In order to authenticate http request we set HttpWebRequest.CookieContainer property using another helper function Get-Authentication-Cookie (lines 31-32). Then we construct RPC call post parameter (lines 35-40) and perform http request (42-55). In result we will have new fooresults.aspx page with the same web parts as original result.aspx page. Hope this information will help you in your work.