Third day of Christmas… Powershell Scripts!

Published by

on

On the third day of Christmas, my true blog gave to me:

Three powershell scripts,
Two Keystone merge tips,
…and a placeholder rule in the content tree.

Let’s unwrap those scripts!

Open a URL

The below script can be used as a TeamCity build definition step to open a URL that you can pass in as a parameter to the script file. Helpful if you want to trigger some post-deployment logic, such as publishing, or just do a wake-up of the application instance.

param(
 [parameter(Mandatory=$true)] [string]$hostname
)

$url = "http://$hostname";
Write-Host "Opening URL : $url";

try{
   $r = [System.Net.WebRequest]::Create($url); 
   $r.Timeout = 120000; 
   $resp = $r.GetResponse();
   $resp.Close();
}
catch 
{
   Write-Error $_
   ##teamcity[buildStatus status='FAILURE'
   [System.Environment]::Exit(1)
}

Clean up Include Files

The following script can be used to get rid of some Sitecore patch include files that were previously deployed and need to be deleted.

param(
 [parameter(Mandatory=$true)] [string]$webroot,
 [parameter(Mandatory=$true)] [string]$projectfolder
)

$path = $webroot + '\App_Config\Include\'
$projectfiles = $path + $projectfolder + '\*.*'

Write-Host "Removing $projectfiles";
if(Test-Path -Path $projectfiles){
   Remove-Item $projectfiles
   Write-Host ">> Items removed.";
}

Robocopy deploys

Sometimes you just want to robocopy a bunch of files!

param(
 [parameter(Mandatory=$true)] [string]$deployDir,
 [parameter(Mandatory=$true)] [string]$packageDir,
 [parameter(Mandatory=$true)] [string]$invalidDirs,
 [parameter(Mandatory=$true)] [string]$invalidFileMask
)

$excludeDirs = $invalidDirs -split ",";
$excludeFiles = $invalidFileMask -split ",";

Write-Host "Deploying package from [$packageDir] to: [$deployDir].";
Write-Host " >> Excluding Directories: $excludeDirs";
Write-Host " >> Excluding Files: $excludeFiles";

robocopy $packageDir $deployDir /E /xd $excludeDirs /XF $excludeFiles;
exit ($LastExitCode -band 24)

9 responses to “Third day of Christmas… Powershell Scripts!”

  1. Fourth Day of Christmas… Continuous Integration tools! | Agile and ALM Avatar

    […] CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  2. Fifth Day of Christmas… Five Golden Rules | Agile and ALM Avatar

    […] Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  3. Sixth day of Christmas… Keystone config tips! | Agile and ALM Avatar

    […] Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  4. Seventh day of Christmas… most-heard retrospective comments | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  5. Eighth day of Christmas… Scaling agile! | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  6. Ninth day of Christmas… Nine Giphy’s Dancing! | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  7. Tenth day of Christmas… Coding WTFs! | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  8. Eleventh Day of Christmas… Lip Syncing! | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

  9. Twelfth day of Christmas… Sitecore memes! | Agile and ALM Avatar

    […] most-heard retrospective comments, Six Keystone config tips, Five Golden Rules! Four CI tools, Three powershell scripts, Two Keystone merge tips, …and a placeholder rule in the content […]

Leave a reply to Ninth day of Christmas… Nine Giphy’s Dancing! | Agile and ALM Cancel reply