Skip to content

Instantly share code, notes, and snippets.

@gerane
Created December 18, 2016 06:53
Show Gist options
  • Save gerane/d4f9b623cca9787c4b58b0621f343a07 to your computer and use it in GitHub Desktop.
Save gerane/d4f9b623cca9787c4b58b0621f343a07 to your computer and use it in GitHub Desktop.
function Find-PlasterTemplate {
[CmdletBinding(DefaultParameterSetName='Default')]
[OutputType("PSCustomObject[]")]
param(
[Parameter(HelpMessage="Specifies the Name of the Plaster template to find",
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[string[]]
$Name,
[Parameter(HelpMessage="Specifies the minimum version of a single Template Version. The MaximumVersion and the RequiredVersion parameters are mutually exclusive; you cannot use both parameters in the same command.",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='MinimumVersion')]
[ValidateNotNull()]
[Version]
$MinimumVersion,
[Parameter(HelpMessage="Specifies the maximum version of a single Template Version. The MaximumVersion and the RequiredVersion parameters are mutually exclusive; you cannot use both parameters in the same command.",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='MaximumVersion')]
[ValidateNotNull()]
[Version]
$MaximumVersion,
[Parameter(HelpMessage="Specifies the exact version number of the Template to install",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='RequiredVersion')]
[ValidateNotNull()]
[Version]
$RequiredVersion,
[Parameter(HelpMessage="Specifies to include all versions of a template in the results. You cannot use the AllVersions parameter with the MinimumVersion, MaximumVersion, or RequiredVersion parameters.",
ParameterSetName='RequiredVersion')]
[switch]
$AllVersions,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Uri]
$Proxy,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[PSCredential]
$ProxyCredential,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[PSCredential]
$Credential
)
Process
{
Try {
$Module = Find-Module @PSBoundParameters -Tag PlasterTemplate -ErrorAction SilentlyContinue
if (!$Module) {
Throw "No match was found for the specified search criteria and template name '${Name}'. Templates must have the tag 'PlasterTemplate'"
}
$Module
}
Catch {
Throw
}
}
}
function Install-PlasterTemplate {
[CmdletBinding(DefaultParameterSetName='Default',
SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true,
HelpMessage="Specifies the Name of the Plaster template to be installed",
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[Parameter(HelpMessage="Specifies the minimum version of a single Template Version.",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='MinimumVersion')]
[ValidateNotNull()]
[Version]
$MinimumVersion,
[Parameter(HelpMessage="Specifies the maximum version of a single Template Version.",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='MaximumVersion')]
[ValidateNotNull()]
[Version]
$MaximumVersion,
[Parameter(HelpMessage="Specifies the exact version number of the Template to install",
ValueFromPipelineByPropertyName=$true,
ParameterSetName='RequiredVersion')]
[ValidateNotNull()]
[Version]
$RequiredVersion,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Uri]
$Proxy,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[PSCredential]
$ProxyCredential,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[PSCredential]
$Credential,
[Parameter(HelpMessage="Forces the installation of the Template")]
[switch]
$Force
)
Begin
{
$YesToAll = $false
$NoToAll = $false
}
Process
{
$PSBoundParameters['Repository'] = 'PSGallery'
$PSBoundParameters['Tag'] = 'PlasterTemplate'
$FindParams = $PSBoundParameters
$Null = $FindParams.Remove('Force')
$Null = $FindParams.Remove('Confirm')
$Null = $FindParams.Remove('Whatif')
Try {
$TemplatesPath = "$Env:APPDATA\Plaster\Templates"
$Module = PowerShellGet\Find-Module @PSBoundParameters
if (! (Test-Path -Path $TemplatesPath)) {
$Null = New-Item -Path $TemplatesPath -ItemType Directory
}
if (!$Module) {
Throw "No match was found for the specified search criteria and template name '${Name}'. Templates must have the tag 'PlasterTemplate'"
}
else {
$Destination = "$TemplatesPath\$($Module.Name)\$($Module.Version)"
}
if ($psCmdlet.ShouldProcess($Name, "Install Plaster template")) {
if (!(Test-Path -Path $Destination)) {
$InstallTemplate = $true
}
elseif (!($YesToAll -OR $NoToAll -OR $Force)) {
$Message = "$Name already Exists"
$Message2 = "Do you want to install anyway?"
$InstallTemplate = $psCmdlet.ShouldContinue($Message2, $Message, [ref]$YesToAll, [ref]$NoToAll)
}
if ($InstallTemplate -or $YesToAll -or $Force) {
$PSBoundParameters["Force"] = $true
PowerShellGet\Save-Module -LiteralPath $TemplatesPath @PSBoundParameters
}
}
}
Catch {
Throw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment