Skip to content

Instantly share code, notes, and snippets.

@AlexKasaku
Created February 29, 2016 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexKasaku/eb329508f789f387f2e0 to your computer and use it in GitHub Desktop.
Save AlexKasaku/eb329508f789f387f2e0 to your computer and use it in GitHub Desktop.
Use SPE Remoting to upload media items, maintaining folder structure
$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