Showing posts with label PowershellFundamental. Show all posts
Showing posts with label PowershellFundamental. Show all posts

Wednesday 3 August 2022

How to remove a rendering from page list using Sitecore PowerShell script ?

 There might be scenario, where 

  • remove a rendering from list of pages/Tree
  • remove broken rendering from Tree

Above we can achieve using Sitecore PowerShell script

$pageTemplateId = "{4A990Q43-71FD-4DAC-8865-5ABAAA13R911}"
$startPath = "master:/sitecore/content/YourSiteNode/Home/"
$useFinalLayout = $True
$useSharedLayout = $True
$rootItem = Get-ChildItem -Path $startPath -recurse | Where-Object { $_."TemplateID" -eq $pageTemplateId };
if($rootItem -ne $null) {
foreach($item in $rootItem) {
$renderings =  Get-Rendering -Item $item -Placeholder "/header/primary-menu" -Device (Get-LayoutDevice "Default") -FinalLayout:$useFinalLayout
foreach($rendering in $renderings) {
    $renderingItem = Get-Item -Path $rendering.ItemID
if($renderingItem.Name -eq "YourRenderingName") {
    Remove-Rendering -Item $item -Instance $rendering -Device (Get-LayoutDevice "Default") -FinalLayout:$useFinalLayout
    Write-Host $item.Name
}
}
}
}

Wednesday 29 September 2021

How to get url web status in bulk ?

 In today's article, would like to present script. And the purpose of this script to Get url web status in bulk.




Recently I was chasing few issue that random pages throwing 404/500/400. So thought to prepare a script and get list of items with their current status also can be re-used after period of time.

Steps to Execute:

1. Please find script by click here

2. Please update list of urls in one file and placed at one place (Let's say D:/urls.txt)

3. Open PowerShell and execute script as .\urlhealths.ps1


Note: 

     Please do not forget to update path in script to get url list (line no. 42)

     Also please ensure urls.txt contains list of url (one url in one line)


Hope this will help.


Wednesday 5 May 2021

How to get license details for a Sitecore instance using Sitecore PowerShell script ?

Since I was trying to get license details for a Sitecore instance, so thought to share with all.


below are ways to get Sitecore license details using Sitecore PowerShell script as

$licensePath = [Sitecore.Configuration.Settings]::LicenseFile
[xml]$licenseDocument = Get-Content -Path $licensePath
$licenseDocument.signedlicense.Signature.Object.license |Format-Table -AutoSize



Tuesday 4 May 2021

How to read list data from file, using Sitecore PowerShell script

Challenge:

There were scenario, where I was trying to get list of strings from text file and tried as below

[string[]]$ItemIdList = Get-Content -Path 'D:\ItemIds.txt'

but getting issue as file not found.


Approach to resolve:


With below lines of script, trying to first upload at temp/upload path (it will create directories if not exist).

And then reading list of strings from text file.

$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + "\temp\upload"
$filePath = Receive-File -Path $tempFolder -overwrite'
[string[]]$ItemIdList = Get-Content -Path $filePath

Friday 23 April 2021

How to get list of items with linked items using Sitecore PowerShell script ?

I was trying to get list of items with directly linked items using PowerShell script.
Since from Sitecore content editor at a time can see linked items only for individual items, so thought to share with all.

Below is the script hope, this will help

    
$allImages = Get - ChildItem - Path 'master:/sitecore/media library/Images' - recurse
$results = @();
$allImages | ForEach - Object {
    $sproperties = @{
        SID = $_.ID
    }

    $referer = Get - ItemReferrer - ID  $sproperties.SID
    $properties = @{
        Name = $_.Name
       ID = $_.ID
       Path = $_.ItemPath
       LinkedItemIds = $referer.ID
    }
    $results += New - Object psobject - Property $properties
}

$results | Select - Object - Property Name, Path, LinkedItemIds

Wednesday 30 December 2020

Create Sitecore bulk items from list

There are scenarios where we have to create item list in Sitecore - For example: Countries, States, Products or any dropdown item list.


Prerequisites


Sitecore PowerShell Extension must be installed.

List of items, for reference sharing list click to download CountryList, USStates, IndiaStates or any custom file.

Download script CreateBulkItem.ps1


Demo Time


Create a Sitecore item at below path 

/sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Content Editor/Context Menu/Packaging

by duplicating existing script item and paste CreateBulkItem.ps1 as below (update list text path in script)


Create Sitecore bulk items from list


Execution Steps

Now right click on the item/folder, where wanted to create item list as per screenshot
Name and Value field's must have default value set as $name in template's standard value


sitecore-bulk-item-step

sitecore-bulk-item-inputs

sitecore-bulk-item-success






bulk-item-list



Note: We can do this activity for different field values using json item list file, we only have to update PowerShell script and Sitecore steps will remains same .


Save your time, Hope this help.


Friday 5 January 2018

How to Schedule SQL Database backup Task with Powershell?

We're done with  SQL DB Backup using Powershell
Now my goal is to make it automated and Scheduled, and we can achieve this with 'ScheduledTask' command of PowerShell.

schedule-database-backup-with-powershell

To create schedule task we basically require four main things:

Define an ScheduledTaskAction
Will create         ScheduledTaskTrigger
Create NewScheduledTask
Registered Register-ScheduledTask

So let's start practically,

First will write script to generate db_backup with powershell script (click here)

Once done with this now we're going to create scheduledTask by following steps:

1. will initialize variables

   $TaskName = "ScheduledDBBackupTask"
   $TaskCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
   $TaskScript = "D:\DB_Backup.ps1"
 
2. here we're defining an action

   $Action = New-ScheduledTaskAction -Execute $TaskCommand -Argument "-NONInteractive -NoLogo -NoProfile -File $TaskScript"

3. here setting up the trigger, (our task will run everyday at 05:35PM)

   $Trigger = New-ScheduledTaskTrigger -Daily -At '05:35PM'
 
4. here creating new scheduledtask

   $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)
 
5. task has been created, now it's time to register this newly created task:
         
   $Task | Register-ScheduledTask -TaskName $TaskName
 
    For source code of this script click here

once tasted on your local and if you not required in future then remove scheduledtask by

Unregister-ScheduledTask ScheduedDBBackupTask

To verfiy that your custom scheduled task has been removed:

Get-ScheduledTask

Happy Scripting

How to take backup of SQL Database with Powershell?

PowerShell, gives us power to do task very quickly just by writing few line of magical scripts.
Today was looking to create backup of SQL database with the help of Windows Powershell and believe me it's too easy and fast.
database-backup-powershell

So let's start,

$Server = '<Server_Name>' 
$Database = '<Database_Name>'
$FilePath = '<TargetPath/Backup_FileName.bak>' 
Backup-SqlDatabase -ServerInstance $Server -Database $Database -BackupFile $FilePath 


for source code please click here
For example:

$Server = 'SQLServer'
$Database = 'POCDatabase'
$FilePath = 'C:\POCDatabase.bak'
Backup-SqlDatabase -ServerInstance $Server -Database $Database -BackupFile $FilePath

That's it, now check the backup file at target path.

@Note Sometime Backup-SqlDatabase commands not exist, then in this case have to manually add module to the PowerShell related to SQL by

Import-Module SQLPS

after importing module again try the above scripts.