Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.