Created
February 29, 2016 23:44
-
-
Save AlexKasaku/eb329508f789f387f2e0 to your computer and use it in GitHub Desktop.
Use SPE Remoting to upload media items, maintaining folder structure
This file contains 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
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://sc81rev151003/ | |
$baseFolder = "C:\Temp\Images\ToUpload\" | |
$baseItemPath = "master:/sitecore/media library/Test/" | |
# Get folder structure, renaming to suit the Sitecore path | |
$foldersToCreate = Get-ChildItem -Path $baseFolder -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName -replace [regex]::escape($baseFolder), $baseItemPath -replace "\\", "/" } | |
$args = @{ | |
"paths" = $foldersToCreate | |
} | |
# Invoke a remote script to iterate over the paths and create items using the Folder template. | |
Invoke-RemoteScript -Session $session -ArgumentList $args -ScriptBlock { | |
$params.paths | %{ New-Item -Path $_ -ItemType "Common/Folder" } | |
} | |
# Now get all the files we want to upload. This is just going to assume that all files found on disk | |
# from the base folder are desirable | |
$files = Get-ChildItem -Path $baseFolder -Recurse | ?{ -not $_.PSIsContainer } | |
# Iterate over the files and use Send-MediaItem to upload | |
$files | %{ | |
$path = $_.FullName | |
$destPath = $path -replace [regex]::escape($baseFolder), $baseItemPath -replace "\\", "/" -replace "master:" , "" | |
Write-Host "Uploading $path -> $destPath" | |
Send-MediaItem -Session $session -Path $path -Destination $destPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment