Created
May 22, 2019 12:22
-
-
Save AlexSen/07230face665aebdf5bfec492478ab84 to your computer and use it in GitHub Desktop.
PnP SharePoint - copy Modern site page to another site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Param( | |
| [Parameter(ParameterSetName = "Inputparameter",Position=1,Mandatory=$True)] | |
| [String]$sharepointUrl, | |
| [Parameter(ParameterSetName = "Inputparameter",Position=2,Mandatory=$True)] | |
| [String]$targeturl, | |
| [Parameter(ParameterSetName = "Inputparameter",Position=3,Mandatory=$True)] | |
| [String]$Sourceurl, | |
| [Parameter(ParameterSetName = "Inputparameter",Position=4,Mandatory=$True)] | |
| [String]$SourceDocumentLib, | |
| [Parameter(ParameterSetName = "Inputparameter",Position=4,Mandatory=$True)] | |
| [String]$TargetDocumentLib | |
| ) | |
| function Copy-sharepoint | |
| { | |
| Param( | |
| [Parameter(Mandatory=$True)] | |
| [String]$sharepointUrl, | |
| [Parameter(Mandatory=$True)] | |
| [String]$targeturl, | |
| [Parameter(Mandatory=$True)] | |
| [String]$Sourceurl, | |
| [Parameter(Mandatory=$True)] | |
| [String]$SourceDocumentLib, | |
| [Parameter(Mandatory=$True)] | |
| [String]$TargetDocumentLib, | |
| [Parameter(Mandatory=$True)] | |
| [String]$UserName, | |
| [Parameter(Mandatory=$True)] | |
| [String]$Password, | |
| [Parameter(Mandatory=$True)] | |
| [System.Management.Automation.PSCredential]$cred | |
| ) | |
| #create secure password | |
| $sPassword = $Password | ConvertTo-SecureString -AsPlainText -Force | |
| $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | |
| $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | |
| Connect-PnPOnline -Url $Sourceurl -Credentials $cred | |
| $Files= Find-PnPFile -List $SourceDocumentLib -Match * | |
| Disconnect-PnPOnline | |
| $Loginsource =$false | |
| $webClient = New-Object System.Net.WebClient | |
| $webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $sPassword) | |
| $webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f") | |
| $webclient.Proxy = $null | |
| foreach($file in $Files) | |
| { | |
| If($file.Name -eq "ACE-Tech-Talks.aspx") | |
| { | |
| $pageUrl = $sharepointUrl+$file.ServerRelativeUrl | |
| $UniqueFileName = $file.Name | |
| $ByteArray=$webClient.DownloadData($pageUrl) | |
| $tfolderWithname= ($pageUrl -split $Sourceurl)[1] | |
| $tfolderwiths =($tfolderWithname -split $UniqueFileName)[0] | |
| $tfolderrmstr =$tfolderwiths.TrimStart("/") | |
| $tfolder =$tfolderrmstr.TrimEnd("/") | |
| $fstream = [System.IO.MemoryStream]($ByteArray) | |
| If($tfolder -eq $null) | |
| { | |
| If($Loginsource -eq $false ) | |
| { | |
| Connect-PnPOnline -Url $targeturl -Credentials $cred | |
| $Loginsource =$true | |
| Add-PnPFile -FileName $UniqueFileName -Folder $TargetDocumentLib -Stream $fstream | |
| } | |
| else | |
| { | |
| Add-PnPFile -FileName $UniqueFileName -Folder $TargetDocumentLib -Stream $fstream | |
| } | |
| } | |
| else | |
| { | |
| If($Loginsource -eq $false ) | |
| { | |
| Connect-PnPOnline -Url $targeturl -Credentials $cred | |
| $Loginsource =$true | |
| Add-PnPFile -FileName $UniqueFileName -Folder $tfolder -Stream $fstream | |
| } | |
| else | |
| { | |
| Add-PnPFile -FileName $UniqueFileName -Folder $tfolder -Stream $fstream | |
| } | |
| } | |
| } | |
| } | |
| } | |
| $cred=Get-Credential | |
| $UName=$cred.UserName.ToString() | |
| $Pass =$cred.GetNetworkCredential().Password | |
| Copy-sharepoint -UserName $UName -Password $Pass -cred $cred -sharepointUrl $sharepointUrl -targeturl $targeturl -Sourceurl $Sourceurl -SourceDocumentLib $SourceDocumentLib -TargetDocumentLib $TargetDocumentLib | |
| write-host "Copying of file complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment