Convers a Sitecore Powershell Extension item to a file on disk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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