ØThe Sitecore PowerShell Extensions (SPE) is a module that provides a command-line interface and scripting environment to work with the Sitecore platform.
ØUsing the PowerShell extension we can perform various operations of Sitecore items, as well as allow access to Sitecore API. It looks and works in the same way as the Windows PowerShell utility. It also excels at reporting and task automation.
ØI’m going to use the SPE in Sitecore 10.1 and it’s out of the functionality here.
Where should we find SPE?
ØThis tool provides a simple command-line interface in the same way as our windows Powershell. you can enter a single command at a time and see the output.
Powershell ISE
ØThe Sitecore Powershell ISE is a tool that can be used to create, edit, test and execute PowerShell scripts. It offers a very advanced and flexible scripting environment.
ØISE adds a GUI and more functions. Following are some of the functions that it provides over the PowerShell console.
1. Saving PowerShell scripts and the ability to open them later
2. Executing scripts with one click
3. Switching the current database
4. Switching the current user and language
5. Debugging
6. Ability to execute selected part of the script
The interface of PowerShell ISE
There are several things that can be done using the SPE module, which includes
1. Getting Sitecore Item
2. Getting child item
3. Get item by path
4. Get items from all languages and versions
5. Making bulk updates
6. Publishing Sitecore items
7. Deleting items based on specific conditions
Use cases for using SPE
Get all the items under the Sitecore node
Script to Change template of selected item
$props = @{
InfoTitle = “Change template of items”
PageSize = 10000
}
$sourcePath = "/sitecore/content/Home/Homepage/Articles”
$items= Get-ChildItem -Path $sourcePath -ReCurse
Function Change-Template
{
Foreach($item in $items)
{
If($item.TemplateId -eq “{843D7183-80B4-4F0B-BA97-6C79BFCE0C5E}”)
{
$item.Editing.BeginEdit();
$item.TemplateId = “{B32C0EA5-EA98-4C07-874A-566C6462647D}”
$item.Editing.EndEdit();
Write-Host “Item id: ” $item.ID “ -Template id: “ $item.TemplateId
}
}
}
$items = Change-Template
$items | Show-ListView @props -Property ItemPath,ID
Close-Window
Why use session state elevation
ØThe concept actually sounds like more than it is, “SPE elevated rights” means that SPE has put a named token in your session state that has an expiration time.
ØWe are not elevating anything related to any other aspect of your Sitecore environment - you are “SPE elevated” which means we allow you to do execute the script and save it within your Sitecore environment.
Why session elevation?
ØShould a malicious actor be able to get a script onto your page. They would be able to execute a script as you would. This is why we’ve introduced the concept of session elevation.
How does the PowerShell script execute within my system?
Your PowerShell scripts can be introduced into the system in a number of ways. the most prominent ones are:
ISE :
Writing scripts in this Sitecore app is probably the most popular way you will provide the code for your authors.ISE allows you to write SPE modules and Apps utilizing its full power of creating user interfaces, reports, and dialogs.
Console
This app is a decent entry point when you need to quickly provide a one-liner to test something or just change a field in a large number of items.
Saved Scripts
SPE allows you to save scripts as Sitecore items that can be utilized by a number of “Integration points”.By saving your script in a proper location, you can expose your scripts in various places in the Sitecore user interface, such as the content editor context menu, ribbon, or even use them in workflow actions.
Remoting
This allows you to execute a script by sending it to a web service.