In this post I would like to show some recent enhancements made in 3.2 that will make interacting with SPE even better!
The packaged Windows PowerShell module can be found on the marketplace listed as SPE Remoting. That is the preferred method for interacting with SPE outside of the Sitecore environment. If you wish to use the same code from within the browser, such as to interact with another instance of SPE, you'll find the commands under here:
1 | master:/system/Modules/PowerShell/Script Library/Platform/Functions/Remoting2 |
We've included all of the documentation below in our book.
When you execute the script or import the module you'll get the following commands:
- New-ScriptSession - This can be reused between calls to all the other commands.
- Invoke-RemoteScript - Best option for performing any remote script execution.
- Send-MediaItem - Remotely upload.
- Receive-MediaItem - Remotely download.
Create a new session object that will contain a reference to the SPE session id and the web service proxy. Next we invoke a scriptblock on the server within the session.
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
# The following example remotely executes a script in Sitecore using a reusable session. | |
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore | |
Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id admin } | |
Name Domain IsAdministrator IsAuthenticated | |
---- ------ --------------- --------------- | |
sitecore\admin sitecore True False |
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
# The following downloads an item from the media library in the master db and dynamically detects the file extension. | |
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore | |
Receive-MediaItem -Session $session -Path "/sitecore/media library/Images/Icons/accuracy" -Destination C:\Images\ -Force |
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
# The following downloads all the items from the media library in the specified path. | |
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore | |
Invoke-RemoteScript -Session $session -ScriptBlock { | |
Get-ChildItem -Path "master:/sitecore/media library/Images/" | Select-Object -Expand ItemPath | |
} | Receive-MediaItem -Session $session -Destination C:\Temp\Images\ |
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
# The following uploads a single image with a new name to the specified path in the media library in the master db. | |
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore | |
Send-MediaItem -Session $session -Path C:\Images\banner.jpg -Destination "/sitecore/media library/Images/banner.jpg" |
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
# The following uploads all of the images in a directory to the specified path in the media library in the master db. | |
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore | |
Get-ChildItem -Path C:\Images | Send-MediaItem -Session $session -Destination "/sitecore/media library/Images/" |
No comments:
Post a Comment