Skip to content

Instantly share code, notes, and snippets.

@RobsonAutomator
Created February 7, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobsonAutomator/e32dd78849ebbf9bd8bace778338e4af to your computer and use it in GitHub Desktop.
Save RobsonAutomator/e32dd78849ebbf9bd8bace778338e4af to your computer and use it in GitHub Desktop.
Convers a Sitecore Powershell Extension item to a file on disk.
function Convert-ScriptItemToFile {
<#
.SYNOPSIS
Convers a SPE item to a file on disk.
.NOTES
Autor: Robert Senktas (@RobsonAutomator)
#>
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[Sitecore.Data.Items.Item]
$Item,
# The root path for SPE module scripts. This will be replaced with $FileRoot parameter.
[string]
$ScriptRoot = "/sitecore/system/Modules/PowerShell/Script Library",
# Folder on disk where items will be saved as files
[string]
$FileRoot
)
process {
# build a filesystem path
$scriptPath = $Item.Paths.Path -replace $ScriptRoot,$FileRoot -replace "/","\"
$scriptFolder = Split-Path $scriptPath -Parent
$whatifMessage = "Save script to a file $scriptPath.ps1"
If ($PSCmdlet.ShouldProcess($Item.ID, $whatifMessage)) {
# create a folder structure
mkdir $scriptFolder -Force | Out-Null
# save script to file
$encoding = New-Object System.Text.UTF8Encoding
[System.IO.File]::WriteAllText("$scriptPath.ps1", $Item."Script", $encoding)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment