Privacy Info

Friday, December 2, 2022

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 items to be removed were large in number. After some research we decided to create PowerShell to solve this problem. We came up with below script which we ran across folders recursively and remove the items from workflow. To remove items from workflow we need to make Workflow and Workflow State field as blank.

Script

$folderPath = "master:\sitecore\content\Home\TestFolder"

$items = Get-ChildItem -Path $folderPath -Recurse 

foreach($item in $items)
{
	$item.Editing.BeginEdit()
	$item.Fields["__Workflow"].Value = ""
	$item.Fields["__Workflow state"].Value = ""
	$item.Editing.EndEdit() | Out-Null
	Write-Host "Item $($item.name) removed from Workflow"	
}	

Hope you find this blog helpful 😀


Article Referred 

https://www.logicalfeed.com/posts/1194/sitecore-powershell-script-to-remove-update-workflows

Tuesday, October 18, 2022

PowerShell Script - Recycle Application Pool



We sometimes require to recycle the application pool of the websites, to clear cache content or to restart website. Below script comes in handy to do this task. We need to specify user credentials, server name and application pool name. PowerShell script will do the recycling task.

Script

$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"


You can modify script based on your requirement

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 - 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...