Sitecore create packages using PowerShell

The Sitecore PowerShell Extensions is a very powerful module what allows you to automate a lot of the trivial Sitecore tasks.

The Export-Package command will create a Sitecore package. This script will read a list of Sitecore item paths from a list, and add these to a package, then the package is downloaded.

The script was given to me by my colleague Adam Honoré, who should be credited.

$ItemsToBeDeployed = @(
@{ Recursive = $TRUE; Source = "master:/content/.../.../..." },
@{ Recursive = $FALSE; Source = "core:/content/.../.../..." },
...
...
);

$ErrorActionPreference = "Stop"

$Package = New-Package -Name "My Package";
$Package.Sources.Clear();
$Package.Metadata.Author = "PowerShell";
$Package.Metadata.Publisher = "Pentia";
$Package.Metadata.Version = Get-Date -Format FileDateTimeUniversal;
$Package.Metadata.Readme = 'This will install a Sitecore Package generated using PowerShell'

ForEach ($Item in $ItemsToBeDeployed)
{
  if ($Item.Recursive)
  {
    $Source = Get-Item $Item.Source | New-ItemSource -Name "N/A" -InstallMode Overwrite
    $Package.Sources.Add($Source);
  }
  else
  {
    $Source = Get-Item $Item.Source | New-ExplicitItemSource -Name "N/A" -InstallMode Overwrite
    $Package.Sources.Add($Source);
  }
}

# Save and Download Package
Export-Package -Project $package -Path "$( $package.Name ) - $( $package.Metadata.Version ).zip" -Zip
Download-File "$SitecorePackageFolder\$( $package.Name ) - $( $package.Metadata.Version ).zip"

HOW IT WORKS:

The $ItemsToBeDeployed variable contains a list of all the root items to be included in the package. Use the “Recursive” property to determine whether to include only the root item, or to include all children as well. Prefix the item path with the name of the database to get the item from.

The $Package variable is the package itself. You can set the various package properties here. The script will iterate through the $ItemsToBeDeployed variable, adding each item on the list as package sources.

Finally, the Export-Package powershell command is called to create the package, and the Download-File method is called to get the package from Sitecore.

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 8, Sitecore 9 and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.