Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Thursday, 4 October 2018

Sitecore PowerShell Extensions - Custom function

Last time I had to prepare a resolution for making changes on the existing content after the deployment. We were adding new components to Standard Values of the page and they had to be configured properly on the content item by setting data source. Change like this couldn’t be synchronised by Unicorn as the page content item was one-time deployment only item (NewItemOnlyEvaluator) and setting data source straight on Standard Value is a really bad idea!

We had three options there:
  1. Set data source manually after synch
  2. Create a Sitecore content package with prepared changes
  3. Prepare Sitecore PowerShell script that will automate the changes we need
As doing things manually is always error-prone and package could overwrite some content changes, we decided to go for third option – PowerShell scripting!

First of all, I needed a way for setting data source on rendering that already exists on a page content item. I found there is not such specific function in SPE core so I wrote a custom one.





To create a custom SPE function we need to have new module created. The easiest it will be by using wizard:





We will need Shared Functions integration, so we can select it just while creating the module.



It can be done after as well just by creating scripts library called Functions. Here's link to documentation: https://doc.sitecorepowershell.com/modules/integration-points/functions

After this we will have our module folder with Functions folder inside. There we will create our custom shared function.


I have named it Set-RenderingDatasource.  Body of the function should be placed inside field described as Script body - Source code written in PowerShell. That's the function I've shown earlier. 

Next thing will be create new PowerShell Script Library item, just on the same level as Functions item. I named it as After Deploy Scripts and created one new script inside - Apply-Datasources:



This script will use our custom shared function to apply datasources to items. Here is the code:



First crucial thing is to import our shared function

Import-Function -Name Set-RenderingDatasource

Then I have added a flag for rolling back changes if needed. That's a subject of decision how we should do the rollback. For this example rollback will be simply setting empty datasources to that items - as they are empty at beginning.

Under I've added variables that will contain ID of the page where we want make our updates, references to rendering items we want to set datasource and ids of datasources. All those things should be known, as we are assuming they will be deployed by, e.g. unicorn (new renderings and datasources - one time deployment) or they are in the content already for some time - the page we want to update.

Then, based on the rollback flag, I'm using our imported function to set up datasources

Set-RenderingDatasource -Page $updatedPage -Rendering $modalRendering -DatasourceId $modalDatasourceId -Device Default -Publish

 Let's roll now! Let's edit our script in ISE!

And run it to see the result:


It's good to log the things script does and say something at the end of execution.

Done! I hope that was interesting lesson of using Sitecore PowerShell to apply changes after deployments. Enjoy!

Sunday, 10 June 2018

Sitecore 9 installation scripts

As Sitecore 9 installation is much different than in previous versions and we have now Sitecore Installation Framework, I would like to share with you my installation PowerShell scripts.

First of all, many thanks to George Chang for the Sitecore 9 install script which I based mine (https://blogs.perficientdigital.com/2017/10/23/how-to-install-sitecore-9-with-the-sitecore-install-framework/).

First thing you will need to do is have Solr installed. It is required from v.9.0 to have Solr running and on SSL. There is a great post by Jeremy Davis about installing Solr easily by his PS script - https://jermdavis.wordpress.com/2017/10/30/low-effort-solr-installs/.

After we have Solr installed and running on https we can start installing our Sitecore 9 instance.

We need to have Sitecore Installation Framework installed to our PowerShell. Here is the script that takes care of it:

 

And here is the Sitecore 9 installation script itself. There is a configuration object as first in the code, you can specify there Sitecore version to install, SQL Server credentials and Solr configuration.


All scripts are available on my GitHub: https://github.com/ReoKzK/Sitecore9-Installer


Friday, 13 January 2017

Razl scripting with PowerShell

Razl, tool from Hedgehog Development, that was designed to transfer items between Sitecore instances and compare them, has some nice scripting features.

I needed to use Razl scripting for Sitecore upgrade process. I created new fresh Sitecore instance (it was big jump, from 6.5 to 8.1) and started preparing what should be moved to new database. Old instance had a lot of datasource items and huge amount of media library items. Transfer all of this things was huge deal and was processing for over a dozen of hours. I decided to make some changes in my approach.

Preparation of Sitecore instances

I was using Razl 3.0.5. Unfortunately I had some problems with my solution that I couldn't connect with this tool to the Sitecore that had all of solution code deployed, so I've created a clean instance of version 6.5 and a clean version of 8.1. I've changed the connection strings of this two to the correct solution databases and I used this two clean instances just for transferring purposes - worked like a charm. You might not have exact problem, but this is how I went through that.

Preparing Razl script file

First you have to prepare the script file in the Razl. You can do it just like in general, by adding tasks to Task List:


Then, instead of running transfer, you can click 'Export Tasks' button. You will be prompted to save the script file. Let's take a look into the one:
<?xml version="1.0" encoding="utf-8"?>
<razl>
  <connection name="SC65VanillaWithSW.Master" readOnly="false" install="true">
    <url>http://sc650vanilla/</url>
    <accessGuid>047c5600-e111-449c-9e2b-179b06f17190</accessGuid>
    <database>master</database>
    <path>C:\inetpub\wwwroot\sc650Vanilla\Website</path>
  </connection>
  <connection name="SC81VanillaForSW.Master" readOnly="false" install="true">
    <url>http://sc81u2vanillaforconnection/</url>
    <accessGuid>8db29355-a8d4-4102-969a-537cc8c54f30</accessGuid>
    <database>master</database>
    <path>C:\inetpub\wwwroot\sc81u2vanillaforconnection\Website</path>
  </connection>
  <operation name="CopyAll" source="SC65VanillaWithSW.Master" target="SC81VanillaForSW.Master">
    <!--Copy with overwrite '/sitecore/templates/SW', all missing parents and all child items from 'SC65VanillaWithSW.Master' to 'SC81VanillaForSW.Master'-->
    <parameter name="itemId">b80ae69f-4212-43e6-af97-2f86ddcddae1</parameter>
    <parameter name="overwrite">True</parameter>
    <parameter name="lightningMode">False</parameter>
  </operation>
  <operation name="CopyAll" source="SC65VanillaWithSW.Master" target="SC81VanillaForSW.Master">
    <!--Copy with overwrite '/sitecore/templates/User Defined', all missing parents and all child items from 'SC65VanillaWithSW.Master' to 'SC81VanillaForSW.Master'-->
    <parameter name="itemId">b29ee504-861c-492f-95a3-0d890b6fca09</parameter>
    <parameter name="overwrite">True</parameter>
    <parameter name="lightningMode">False</parameter>
  </operation>
  <operation name="CopyAll" source="SC65VanillaWithSW.Master" target="SC81VanillaForSW.Master">
    <!--Copy with overwrite '/sitecore/layout/Layouts/SW', all missing parents and all child items from 'SC65VanillaWithSW.Master' to 'SC81VanillaForSW.Master'-->
    <parameter name="itemId">2141e479-3f72-447c-9afa-3b4db1ea283d</parameter>
    <parameter name="overwrite">True</parameter>
    <parameter name="lightningMode">False</parameter>
  </operation>
</razl>

It has a two connections for the instances which will be used in transfer. It comes from the Razl configuration, where firstly I created that connections. Under that elements there is a list of operations with specified source instance, target instance, ID of the item and configuration of the operation type - in my case it will be CopyAll with overwriting the target instance.

As far as I knew at that time, there was no way to edit exported script with Razl, so I was editing it manually.

Preparing PowerShell run script

At last I wanted to have a PowerShell script that will run my transfer script with just one click. I have prepared a PSs that run razl.exe with specified scripts. For core database this is quite simple - set your directory where Razl script are located in:


For master database I decided to split Razl script into smaller parts, so the I was able to comment out some of them to run just specific ones:


Also these scripts log full log into specific files. So everything that happens while transfer is saved for analysis. Here is some example part of the log:

    1 13:00:31,579 INFO  CopyAll Started Id: 563a2ddb-ad89-4182-9706-eb1512abe0b1, Path: /sitecore/system/Field types/List Types/Google Maps Multilist, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core, Overwrite: True
   1 13:00:31,579 INFO  CopyAll Processing /sitecore/system/Field types/List Types/Google Maps Multilist
   1 13:00:31,580 INFO  CopyItem Started Id: 563a2ddb-ad89-4182-9706-eb1512abe0b1, Path: /sitecore/system/Field types/List Types/Google Maps Multilist, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:31,847 INFO  CopyItem Finish Id: 563a2ddb-ad89-4182-9706-eb1512abe0b1, Path: /sitecore/system/Field types/List Types/Google Maps Multilist, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:31,928 INFO  CopyAll processing children of /sitecore/system/Field types/List Types/Google Maps Multilist
   1 13:00:31,928 INFO  CopyAll Processing /sitecore/system/Field types/List Types/Google Maps Multilist/Menu
   1 13:00:31,929 INFO  CopyItem Started Id: 60daf681-8069-46a2-aa39-5490741f6cec, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:32,196 INFO  CopyItem Finish Id: 60daf681-8069-46a2-aa39-5490741f6cec, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:32,252 INFO  CopyAll processing children of /sitecore/system/Field types/List Types/Google Maps Multilist/Menu
   1 13:00:32,252 INFO  CopyAll Processing /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Show Map
   1 13:00:32,252 INFO  CopyItem Started Id: ce073686-11cb-4921-b4db-61c98bc1f54a, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Show Map, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:32,519 INFO  CopyItem Finish Id: ce073686-11cb-4921-b4db-61c98bc1f54a, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Show Map, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:32,576 INFO  CopyAll processing children of /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Show Map
   1 13:00:32,576 INFO  CopyAll Processing /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Select All
   1 13:00:32,577 INFO  CopyItem Started Id: bbe0cd71-aac7-47bc-971a-49265078d17c, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Select All, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:33,153 INFO  CopyItem Finish Id: bbe0cd71-aac7-47bc-971a-49265078d17c, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Select All, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:33,209 INFO  CopyAll processing children of /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Select All
   1 13:00:33,209 INFO  CopyAll Processing /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Deselect All
   1 13:00:33,209 INFO  CopyItem Started Id: b54acbc5-0fc0-4d6d-a863-0f8657e50504, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Deselect All, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:38,480 INFO  CopyItem Finish Id: b54acbc5-0fc0-4d6d-a863-0f8657e50504, Path: /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Deselect All, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core
   1 13:00:38,537 INFO  CopyAll processing children of /sitecore/system/Field types/List Types/Google Maps Multilist/Menu/Deselect All
   1 13:00:38,593 INFO  CopyAll Finish Id: 563a2ddb-ad89-4182-9706-eb1512abe0b1, Path: /sitecore/system/Field types/List Types/Google Maps Multilist, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core, Overwrite: True
   1 13:00:38,705 INFO  CopyAll Started Id: 5cee31bf-120d-4936-b9c6-930cbf63cb57, Path: /sitecore/system/Field types/Simple Types/Google Maps Single Point, From: SC65VanillaWithSW.Core, To: SC81VanillaForSW.Core, Overwrite: True

More info about Razl scripting is in official Razl documentation: https://hedgehogdevelopment.github.io/razl/script.html.