Skip to content

Commit

Permalink
Added: Command to add a template to an adb item
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiospizzi committed Apr 9, 2020
1 parent d5f8e4d commit 0a9a670
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions Adb/Adb.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'Get-AdbItem'
'Set-AdbItemProperty'
'Remove-AdbItemProperty'
'Add-AdbItemTemplate'
# Item Validation
'Test-AdbItemValidation'
)
Expand Down
65 changes: 65 additions & 0 deletions Adb/Functions/Add-AdbItemTemplate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<#
.SYNOPSIS
Add the template name to a the adb item.
.DESCRIPTION
This command will query all existing templates on the adb item, merge
them with the spcified template name and then update the new template
list.
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
PS C:\> Add-AdmItemTemplate -Name 'myname' -Template 'mytpl'
Add the template mytpl to the item myname.
#>
function Add-AdbItemTemplate
{
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
# The adb session.
[Parameter(Mandatory = $false)]
[PSTypeName('Adb.Session')]
[System.Object]
$Session,

# The item name.
[Parameter(Mandatory = $true)]
[System.String]
$Name,

# The template to add to the item.
[Parameter(Mandatory = $true)]
[System.String]
$Template
)

$Session = Test-AdbSession -Session $Session

$item = Get-AdbItem -Session $Session -Name $Name

# Prepare the template list
$templates = [System.String[]] $item.templatesNames
$templates += $Template
$templates = $templates | Select-Object -Unique | Sort-Object

$item = [PSCustomObject] @{
name = $Name
templatesNames = $templates
}

$requestSplat = Get-AdbSessionRequestSplat -Session $Session -Method 'Put'
$requestSplat['Uri'] = '{0}/items/{1}' -f $Session.Uri, $Name
$requestSplat['Body'] = $item | ConvertTo-Json -Compress -Depth 99

if ($PSCmdlet.ShouldProcess($requestSplat.Uri, $requestSplat.Method.ToUpper()))
{
Write-Verbose ('{0} {1} {2}' -f $requestSplat.Method.ToUpper(), $requestSplat.Uri, $requestSplat.Body)
Invoke-RestMethod @requestSplat -Verbose:$false -ErrorAction 'Stop' | Out-Null
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is mainly based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

* Added: Command to add a template to an adb item

## 1.3.0 - 2020-04-08

* Added: Add command to get the current session
Expand Down

0 comments on commit 0a9a670

Please sign in to comment.