You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been trying to build a powershell runbook that captures the source document library's folder structure and cloning it to a target sites document library using PnP PowerShell. Below is the script;
`#Azure Cert
$password = Get-AutomationVariable -Name 'PnP-module-cert-pwd'
$secPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$cert = Get-AutomationCertificate -Name 'SharePoint PnP PowerShell Connection.pfx'
$pfxCert = $cert.Export("pfx", $password)
$certPath = "$env:TEMP\SharePointPnPConnection.pfx"
Set-Content -Value $pfxCert -Path $certPath -Force -AsByteStream
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have been trying to build a powershell runbook that captures the source document library's folder structure and cloning it to a target sites document library using PnP PowerShell. Below is the script;
`#Azure Cert
$password = Get-AutomationVariable -Name 'PnP-module-cert-pwd'
$secPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$cert = Get-AutomationCertificate -Name 'SharePoint PnP PowerShell Connection.pfx'
$pfxCert = $cert.Export("pfx", $password)
$certPath = "$env:TEMP\SharePointPnPConnection.pfx"
Set-Content -Value $pfxCert -Path $certPath -Force -AsByteStream
Import PnP PowerShell Module
Import-Module PnP.PowerShell
Variables
$sourceSiteUrl = "https://XXXXX.sharepoint.com/sites/DKDevelopmentTestSite"
$targetSiteUrl = "https://XXXXX.sharepoint.com/sites/DKDevelopmentTestSiteTARGET"
$sourceLibraryName = "Shared%20Documents/"
$targetLibraryName = "Shared%20Documents/"
Connect to the source site Credentail from Cert
Connect-PnPOnline -Url $sourceSiteUrl -ClientId 'b372a83b-XXXX-XXXX-92d4-573e73070227' -Tenant '4f5a8277-XXXX-XXXX-b65a-8836c5784d0c' -CertificatePath $certPath -CertificatePassword $secPassword
Write-Output "Connected to source site: $sourceSiteUrl/$sourceLibraryName"
Get all folders and subfolders in the source document library
$sourceFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceLibraryName -ItemType Folder
Write-Output "Source folders - $sourceForlders"
Connect to the target site
Connect-PnPOnline -Url $targetSiteUrl -ClientId 'b372a83b-3514-XXXX-XXXX-573e73070227' -Tenant '4f5a8277-XXXX-XXXX-b65a-8836c5784d0c' -CertificatePath $certPath -CertificatePassword $secPassword
Write-Output "Connected to target site: $targetSiteUrl/$targetLibraryName"
Loop through each folder and replicate the structure
Write-Output "Looping through each folder and replicate the structure"
foreach ($folder in $sourceFolders) {
# Skip the root folder
Write-Output "Skipping root folder"
if ($folder.ServerRelativeUrl -eq "/sites/DevelopmentTestSite/$sourceLibraryName") {
continue
}
$relativeFolderPath = $folder.ServerRelativeUrl.Replace("/sites/DevelopmentTestSite/$sourceLibraryName", "")
$parentPath = if ($relativeFolderPath -ne "") { "$targetLibraryName$relativeFolderPath" }
else { $targetLibraryName }
# Create folder in target library
Add-PnPFolder -Name $folder.Name -Folder $parentPath
Write-Host "$parentPath/$folder.Name created"
}
Disconnect-PnPOnline`
The connection reg seems to work fine, it doesn't seem to find any of the folders or add them to target library. yet no error is produced.
Anyone have any idea on where I may be going wrong? I can't for the life f me get this working.
I aprpeciate any help I can get.
Cheers! D
Beta Was this translation helpful? Give feedback.
All reactions