$Username="Username" $Password="Password" function RecycleAppPool($Server,$AppPool) { Write-Host "ServerName:$Server ApplicationPool:$AppPool" # Credentials $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password # Checking App Pool by name $AppItem = (Invoke-Command $Server -ScriptBlock{param($AppPool) Get-WebAppPoolState | Where-Object {$_.ItemXPath -match "$using:AppPool"} } -ArgumentList $AppPool -Credential $Cred)[0] If($AppItem -ne $Null) { try { # Recycle App Pool $Recycle = Invoke-Command $Server -ScriptBlock{param($AppPool)Restart-WebAppPool -name "$using:AppPool"} -ArgumentList $AppPool -Credential $Cred Write-Host "Recycle Successful" } catch { Write-Host "Error: " -NoNewline -ForegroundColor red Write-Host $_.Exception.Message Contiune } } } RecycleAppPool "ServerName" "AppPoolName"
Privacy Info
Tuesday, October 18, 2022
PowerShell Script - Recycle Application Pool
Monday, October 17, 2022
Sitecore PowerShell - Merge Layouts
Below script will help you to merge content from layouts. PowerShell script will merge layout from Final to Shared. In the script we need to specify template id for which layouts needs to be merged.
Script
$query = "fast:/sitecore/content//*[@@templateid='{CC44DB9D-1111-4E8E-9D84-080CD8895C77}']" Get-Item -Path "master:" -Query $query | Merge-Layout
You can customize the script as per your requirement and use it.
Hope this information is useful to you.
Sunday, October 16, 2022
PowerShell Script - CPU and Storage Details
Manually checking Storage consumption and CPU usage multiple time is very repetitive and boring task. Also if you want to check same details on remote servers then it will again consume lot of time. PowerShell help us to automate this process. We need to specify credentials, name of the server in the script and it will provide storage consumption and CPU utilization details.
Script
PowerShell Script
$MyDir = Split-Path -Parent $MyInvocation.MyCommand.Path [xml]$ConfigFile = Get-Content "$MyDir\Settings.xml" function GetCPUStorageDetails($serverName) { $Username = $ConfigFile.Settings.CredentialsSettings.Username; $Password = ConvertTo-SecureString -String $ConfigFile.Settings.CredentialsSettings.Password -AsPlainText -Force $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password $cpuUsage = Get-WmiObject Win32_Processor -ComputerName $serverName -Credential $Cred Write-Host (“{0}%” -f $cpuUsage.LoadPercentage ) $storageDetails = Get-WmiObject -Class win32_logicaldisk -ComputerName $serverName -Credential $Cred foreach ($storageDetail in $storageDetails) { #Device ID | FreeSpace Write-Host -NoNewline (“{0} {1}GB ” -f $storageDetail.DeviceId,([math]::Round($storageDetail.FreeSpace/1GB,2)) ) } Write-Host "" } GetCPUStorageDetails("ServerName")
Settings.xml
<?xml version="1.0"?> <Settings> <CredentialsSettings> <Username>Username</Username> <Password>Password</Password> </CredentialsSettings> </Settings>
Hope you find this information useful.
Saturday, October 15, 2022
Sitecore PowerShell Script - Item Creation from API
This post describes how can you convert API response from URL to Sitecore item. We need to specify folder item path, Template ID and API URL. After running the script items will be created with API details. Also create template with id, userid, title and completed field and specify template ID for this new template in $templateID variable.
Script
$itemPath="master:\content\home\DemoTestFolder" $templateID="{46F57F90-B447-4E8A-8CA1-69B1AD08ACD2}" $ApiUrl = "https://jsonplaceholder.typicode.com/todos" $response = Invoke-RestMethod -Uri $ApiUrl -Method Get -ContentType "application/json"; foreach ($row in $response) { if (-not ([string]::IsNullOrEmpty($row.id))) { $itemName = $row.id #Create Item $newItem = New-Item -Path $itemPath -Name $itemName -ItemType $templateID; #Add values in fields $newItem.Editing.BeginEdit() $newItem["id"] = $row.id $newItem["userId"] = $row.userId $newItem["title"] = $row.title $newItem["completed"] = $row.completed $newItem.Editing.EndEdit() Write-Host "Item Created: " $itemName } }
API URL Reference: https://jsonplaceholder.typicode.com
Hope you find this information useful.
Sitecore PowerShell Script - Creating Multiple Language Versions

function LanguageVersionsOperations($LanguageItemInfo) { #Spliting the Input $SplitDetails=$LanguageItemInfo.Split(";") #Path of the item $itemPath=$SplitDetails[0] #Item Details from Get-Item method $itemGetInfo= Get-Item $itemPath #Deleting All versions except English foreach ($version in $itemGetInfo.Versions.GetVersions($true)) { if ($version.Language -ne "en") { Remove-ItemVersion $version Write-Host $version.DisplayName " - " $version.ID " - " $version.Language "- deleted" } } #Adding Versions for($i = 1; $i -lt $SplitDetails.Length; $i++) { $itemInfo = Get-Item $itemPath | Add-ItemLanguage -Language "en" -TargetLanguage $SplitDetails[$i] -IfExist OverwriteLatest Write-Host $itemInfo.DisplayName " - " $itemInfo.ID " - " $SplitDetails[$i] "- added" } } $Items ="master:\sitecore\content\Home\TestItem1;en-US;en-CA;fr-CA;zh-CN;en-AU", "master:\sitecore\content\Home\TestItem2;en-US;en-CA;fr-CA;en-IN;es-AR", "master:\sitecore\content\Home\TestItem3;en-US;en-CA;fr-CA;en-IN;es-AR", "master:\sitecore\content\Home\TestItem4;en-US;en-CA;fr-CA;zh-CN;en-AU" foreach ($Item in $Items) { Write-Host "Start Operation $((Get-Date).ToString())" LanguageVersionsOperations $Item Write-Host "End Operation $((Get-Date).ToString())" }
Thursday, October 13, 2022
Sitecore PowerShell Script - Item Creation from CSV file
Creation of multiple items and adding the content to it sometime becomes time consuming. Especially when you have large amount of data to be added in CMS from Excel (CSV) file. We also faced similar challenge where we need to create multiple items and added details to it from CSV file. It was a repetitive and time consuming task for us. For this obstacle we came up with Sitecore PowerShell script which will do this task for us. We just need to specify the location of path where we want to create items, Template ID and CSV file location. Sitecore PowerShell Script will run and create the items as per CSV file details.
$itemPath="master:\content\home\TestFolder" #Location where items are created $templateID="{0087B3DC-A60A-4C46-B510-2A3A8F7C4D36}" #Template from items are created $importCSVPath = "C:\DemoImportFile.csv" #CSV file path #Import data from CSV file $importRows = Import-CSV $importCSVPath New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { foreach ($row in $importRows) { $itemName = $row.Field1 #Create Item $newItem = New-Item -Path $itemPath -Name $itemName -ItemType $templateID; #Add values in fields $newItem.Editing.BeginEdit() $newItem["Field1"] = $row.Field1 $newItem["Field2"] = $row.Field2 $newItem["Field3"] = $row.Field3 $newItem["Field4"] = $row.Field4 $newItem.Editing.EndEdit() Write-Host "Item Created: " $itemName } }
Website Pages Checker - Console Application
It is difficult to check and test pages of website manually one by one if pages count is huge. There are some limitations with automation testing of websites. We cannot check whether UI is meet as per expectation or is there any content issue with website with automation testing. To solve this problem we came up with hybrid solution - Website Pages Checker. In this application we need to specify the websites URL's that we require to check and then run the application. The application will visit each page specified in the list, automatically scroll till end of the page and it will ask whether page is present as per requirement. This application saved our huge amount of time avoiding manually entering URL's in browser and specifying whether they are working or not in excel sheet. Please refer below steps to create this console application.
Step 1: Create Console Application in Visual Studio
Step 2: Install below Nugget Packages for the project.
- log4net
- Newtonsoft.Json
- Selenium.WebDriver
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- Log4net Logging Setup Start--> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections> <log4net> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="file1" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs\log_%date{yyyy.MM.dd_HH.mm.ss}.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <appendToFile value="true" /> <rollingStyle value="Once" /> <maxSizeRollBackups value="5" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="file2" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs\PagesDetails_%date{yyyy.MM.dd_HH.mm.ss}.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <appendToFile value="true" /> <rollingStyle value="Once" /> <maxSizeRollBackups value="5" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="console" /> </root> <logger name="File1"> <level value="All"/> <appender-ref ref="file1"/> </logger> <logger name="File2"> <level value="All"/> <appender-ref ref="file2"/> </logger> </log4net> <!-- Log4net Logging Setup End--> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <appSettings> <add key="UrlsFilePath" value="Urls.json"/> </appSettings> </configuration>
namespace WebsitePagesChecker { public static class Logger { public static readonly log4net.ILog Log = log4net.LogManager.GetLogger("File1"); public static readonly log4net.ILog SiteDetailsFile = log4net.LogManager.GetLogger("File2"); } }
using System; using System.Collections.Generic; namespace WebsitePagesChecker { internal class JsonUrls { public string url { get; set; } public static List<JsonUrls> GetSiteUrls() { List<JsonUrls> URLS = new List<JsonUrls>(); try { string UrlsFilePath = System.Configuration.ConfigurationManager.AppSettings["UrlsFilePath"].ToString(); //Getting URLS from Json File string json = System.IO.File.ReadAllText(UrlsFilePath); URLS = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonUrls>>(json); } catch (Exception ex) { Logger.Log.Error(String.Format("{0} Exception {1}", DateTime.Now, ex)); } return URLS; } } }
[ { "url": "https://www.google.com" }, { "url": "https://www.amazon.com" }, { "url": "https://www.flipkart.com" }, { "url": "https://www.youtube.com" }, { "url": "https://bookmyshow.com" } ]
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace WebsitePagesChecker { internal class Drivers { protected static IWebDriver SetupChromeDriver() { ChromeOptions options = new ChromeOptions(); options.AddArgument("--log-level=3");// Hides log errors/warnings IWebDriver driver = new ChromeDriver(); driver.Manage().Window.Maximize(); return driver; } protected static void CloseExitDrivers(IWebDriver driver) { driver.Close(); driver.Quit(); } } }
using OpenQA.Selenium; using System; using System.Collections.Generic; namespace WebsitePagesChecker { internal class TestCases:Drivers { public static void CheckWebsitePages() { try { IWebDriver driver = SetupChromeDriver(); List<JsonUrls> URLS = JsonUrls.GetSiteUrls(); Logger.SiteDetailsFile.Info(String.Format("Total URLS:{0}", URLS.Count)); foreach (var URL in URLS) { string script = "var i = 10; var int = setInterval(function() { window.scrollTo(0, i); i += 10; if (i >= 3000) clearInterval(int);}, 20);";//to increase speed increase values of i driver.Navigate().GoToUrl(URL.url); IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript(script); Console.Write("\nPage is Ok? Yes (y) or No (n):"); char userChoice = Console.ReadKey().KeyChar; Logger.SiteDetailsFile.Info(String.Format("{0} Page:{1} Result:{2}", DateTime.Now, URL.url, userChoice.ToString().ToUpper())); } CloseExitDrivers(driver); } catch (Exception ex) { Logger.Log.Error(String.Format("Exception: {0}",ex.ToString())); } } } }
using System; namespace WebsitePagesChecker { internal class Program { static void Main(string[] args) { StartApplication(); } private static void StartApplication() { Logger.Log.Info(String.Format("Application Start {0}", DateTime.Now)); TestCases.CheckWebsitePages(); Logger.Log.Info(String.Format("Application End {0}", DateTime.Now)); Console.ReadKey(); } } }
Tuesday, October 11, 2022
PowerShell Script - Website Certificate Information
It is difficult to manage the certificates for websites. When websites hosted on server are large in number task become more tedious and complicated. We have also received similar task for collecting details of certificates for each website on remote server. In our case websites hosted on server were large in number and manually collecting the details for the certificates was time consuming task. So we came up with below PowerShell script which will help us to provide the details.
Script
Import-Module -Name WebAdministration Get-ChildItem -Path IIS:SSLBindings | ForEach-Object -Process ` { if ($_.Sites) { $certificate = Get-ChildItem -Path CERT:LocalMachine/My | Where-Object -Property Thumbprint -EQ -Value $_.Thumbprint [PsCustomObject]@{ Sites = $_.Sites.Value CertificateFriendlyName = $certificate.FriendlyName CertificateIssuer = $certificate.Issuer ExpiryDate = [string]$certificate.NotAfter } } } | ConvertTo-Json | Out-File -Width 4096 C:\ServerCertDetails.json -Append
Above script will provide details of fields for each website hosted on server.
- Website Name
- Certificate Name
- Certificate Issuer
- Certificate Expiry Date
Monday, October 10, 2022
Failed to Start Sitecore Marketing Automation Engine Service
While installing Sitecore 9.3 we encountered below error for marketing automation service.
[Error] Failed to start the Marketing Automation Engine service. System.InvalidOperationException: This configuration has not been initialized. Please call the initialize method before using it. [Error] Error initializing XConnect client. System.AggregateException: One or more errors occurred. -> Sitecore.XConnect.XdbCollectionUnavailableException: An error occurred while sending the request. —> System.Net.Http.HttpRequestException: An error occurred while sending the request.
We tried to manually restart the service but it failed and displayed below error.
- Open PowerShell window in Administrator mode
- Run below two commands
Get-Childitem cert:\LocalMachine\root -Recurse | Where-Object {$.Issuer -ne $.Subject}
Get-Childitem cert:\LocalMachine\root -Recurse | Where-Object {$.Issuer -ne $.Subject} | Move-Item -Destination Cert:\LocalMachine\CA
- https://community.sitecore.com/community?id=community_question&sys_id=2295232d1b4770d0b8954371b24bcb69
- https://sitecore.stackexchange.com/questions/27214/sitecore-9-3-installation-failing-failed-to-start-the-marketing-automation-en
- https://himmatsinghdulawat.blogspot.com/2021/06/failed-to-start-service-sitecore-marketing-automation-engine.html
- https://markgibbons25.medium.com/troubleshooting-sitecore-marketing-automation-plan-enrollment-1b8461e802b0
- https://techieticker.blogspot.com/2020/04/failed-to-start-service-sitecore.html
Sunday, October 9, 2022
Website Monitor - Console Application
1. Create Console Application in Visual Studio
2. Install below two Nugget package for your project
log4net
Newtonsoft.Json
3. Add Reference for log4net in AssemblyInfo.cs file
4. Add below code in App.config file
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections> <!-- Log4net Logging Setup Start --> <log4net> <root> <level value="ALL" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="file" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs/WebsiteMonitor_%date{yyyy.MM.dd_HH.mm.ss}.log" /> <appendToFile value="true" /> <rollingStyle value="Once" /> <maxSizeRollBackups value="5" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> </log4net> <!-- Log4net Logging Setup End --> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <appSettings> <add key="UrlsFilePath" value="Urls.json"/> </appSettings> </configuration>
Note: You can change log4net configurations as per your requirement.
5. Create Logs folder in the application and set below actions.
namespace SitesMonitor { public static class Logger { public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); } }
[ { "url": "https://www.google.com" }, { "url": "https://www.amazon.com" }, { "url": "https://www.flipkart.com" }, { "url": "https://www.youtube.com" }, { "url": "https://bookmyshow.com" } ]
using System; using System.Collections.Generic; namespace SitesMonitor { public class JsonUrls { public string url { get; set; } public static List<JsonUrls> GetSiteUrls() { List<JsonUrls> URLS = new List<JsonUrls>(); try { string UrlsFilePath = System.Configuration.ConfigurationManager.AppSettings["UrlsFilePath"].ToString(); //Getting URLS from Json File string json = System.IO.File.ReadAllText(UrlsFilePath); URLS = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonUrls>>(json); } catch (Exception ex) { Logger.Log.Error(String.Format("{0} Exception {1}", DateTime.Now, ex)); } return URLS; } } }
using System; using System.Collections.Generic; using System.Net; namespace SitesMonitor { internal class Program { static void Main(string[] args) { StartApplication(); } private static void StartApplication() { try {
Logger.Log.Info(String.Format("Execution Started {0}", DateTime.Now));List<JsonUrls> URLS = JsonUrls.GetSiteUrls(); foreach (var URL in URLS) { string url = URL.url; WebRequest request = WebRequest.Create(url); try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response != null || response.StatusCode == HttpStatusCode.OK) { //Website is working fine Logger.Log.Info(String.Format("{0} URL:{1} Status Code:{2} Status Description:{3}", DateTime.Now, url, response.StatusCode, response.StatusDescription)); } else { //Error occured for website Logger.Log.Info(String.Format("{0} URL:{1} Status Code:{2} Status Description:{3}", DateTime.Now, url, response.StatusCode, response.StatusDescription)); } } catch (Exception ex) { Logger.Log.Error(String.Format("{0} URL:{1} Exception {2}", DateTime.Now, url, ex)); } } } catch (Exception ex) { Logger.Log.Error(String.Format("{0} Exception {1}", DateTime.Now, ex)); } finally { Logger.Log.Info(String.Format("Execution Completed {0}", DateTime.Now)); Console.ReadLine(); } } } }
Sitecore PowerShell Script - Remove item from Workflow
We came across scenario where we need to remove multiple items from workflow. If items would be less then there would not be any issue but i...

-
While installing Sitecore 9.3 we encountered below error for marketing automation service. [Error] Failed to start the Marketing Autom...
-
Creation of multiple items and adding the content to it sometime becomes time consuming. Especially when you have large amount of data to be...
-
It is difficult to manage the certificates for websites. When websites hosted on server are large in number task become more tedious and com...