Public/New-CitrixTemplateRegistry.ps1

function New-CitrixTemplateRegistry {
    <#
    .SYNOPSIS
    Creates a new Windows Registry definition in the Citrix Optimizer template.

    .DESCRIPTION
    This function will create a Registry Definition in the Citrix Optimizer template.
    
    .PARAMETER Path
    Specifies the Path to the template file

    .PARAMETER GroupName
    Specifies the Group in the template file to add the Registry Entry to

    .PARAMETER EntryName
    The Display Name for the Registry Entry in Citrix Optimizer

    .PARAMETER EntryDescription
    The Display Description for the Registry Entry in Citrix Optimizer

    .PARAMETER ItemName
    The Registry Item Name to add

    .PARAMETER ItemPath
    The Registry Item Path to add

    .PARAMETER ItemValue
    The Registry Item Value to add

    .PARAMETER ItemType
    The Registry Item Type to add ("Dword","Binary","ExpandString","MultiString","String","Qword")

    .INPUTS
    This function will take inputs via pipeline as string

    .OUTPUTS
    Returns $true or $false depending on the Scheduled Task creation state

    .EXAMPLE
    PS> New-CitrixTemplateRegistry -Path 'template.xml' -GroupName 'Group1' -EntryName 'Add Edge Update Registry Entry' -EntryDescription 'Disable Edge Updates via HKLM' -ItemName 'UpdatesEnabled' -ItemPath 'HKLM\Software\Microsoft\Edge' -ItemValue '0' -ItemType 'Dword'
    Adds an entry to disable the Edge Updates in the template file.
    .EXAMPLE
    PS> New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'Group1' -EntryName 'Add Edge Update Registry Entry' -EntryDescription 'Disable Edge Updates via HKLM' -ItemName 'UpdatesEnabled' -ItemPath 'HKLM\Software\Microsoft\Edge' -ItemValue '0' -ItemType 'Dword'
    Adds an entry to disable the Edge Updates in the template file based on the return value in $Template.Path
    .EXAMPLE
    PS> New-CitrixTemplateRegistry -Path $Template.Path -GroupName $Group.Name -EntryName 'Add Edge Update Registry Entry' -EntryDescription 'Disable Edge Updates via HKLM' -ItemName 'UpdatesEnabled' -ItemPath 'HKLM\Software\Microsoft\Edge' -ItemValue '0' -ItemType 'Dword'
    Adds an entry to disable the Edge Updates in the template file based on the return value in $Template.Path and $Group.Name
    .EXAMPLE
    PS> New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'System Optimizations' -EntryName 'Remove Edge Item 1' -EntryDescription 'Remove Edge Item 1' -ItemName 'Item1' -ItemPath 'HKLM\Software\Microsoft\Edge' -DeleteValue
    Deletes a registry value from the master image
    .EXAMPLE
    PS> New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'System Optimizations' -EntryName 'Remove Edge Key 1' -EntryDescription 'Remove Edge Key 1' -ItemPath 'HKLM\Software\Microsoft\Edge1' -DeleteKey
    Deletes a registry key from the master image

    .LINK
    https://github.com/dbretty/Citrix.Optimizer.Template/blob/main/Help/New-CitrixTemplateRegistry.MD
#>


[CmdletBinding()]

Param (
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$true
    )]
    [System.String]$Path,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$true
    )]
    [System.String]$GroupName,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$true
    )]
    [System.String]$EntryName,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$true
    )]
    [System.String]$EntryDescription,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$false
    )]
    [System.String]$ItemName,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$true
    )]
    [System.String]$ItemPath,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$false
    )]
    [System.String]$ItemValue,
    [Parameter(
        ValuefromPipelineByPropertyName = $true,mandatory=$false
    )]
    [ValidateSet("Dword","Binary","ExpandString","MultiString","String","Qword")]
    [System.String]$ItemType,
    [switch]$DeleteValue,
    [switch]$DeleteKey
)

begin {

        # Set strict mode and initial return value
        Set-StrictMode -Version Latest

        # Set up PSCustom Object for return
        $Return = New-Object -TypeName psobject 
        $Return | Add-Member -MemberType NoteProperty -Name "Complete" -Value $false

} # begin

process {

    if(Get-Template -Path $Path){

        write-verbose "Citrix Optimizer Template $($Path) found"
        write-verbose "Load Citrix Optimizer Template"

        # Load Template and check for existing Group"
        [XML]$xmlfile = Get-Content $Path

        if(Get-TemplateGroup -Path $Path -GroupName $GroupName){

            write-verbose "Group $($GroupName) found"

            if(!(Get-TemplateEntry -Path $Path -EntryName $EntryName)){

                write-verbose "Registry Entry $($EntryName) not found, adding"

                $Group = $xmlfile.root.group | where-object {$_.id -eq $($GroupName)}

                write-verbose "Create Entry element"
                $Entry = $XMLFile.CreateElement("entry")

                    write-verbose "Create Name element"
                    $Name = $XMLFile.CreateElement("name")
                    $Name.InnerText = $EntryName
                    $Entry.AppendChild($Name)

                    $Description = $xmlfile.CreateElement("description")
                    $Description.InnerText = $EntryDescription
                    $Entry.AppendChild($Description)

                    $Execute = $xmlfile.CreateElement("execute")
                    $Execute.InnerText = "1"
                    $Entry.AppendChild($Execute)

                    write-verbose "Create Action element"
                    $Action = $XMLFile.CreateElement("action")

                        $Plugin = $XMLFile.CreateElement("plugin")
                        $Plugin.InnerText = "Registry"
                        $Action.AppendChild($Plugin)

                        $Params = $XMLFile.CreateElement("params")

                            if(!($DeleteKey)){
                                $ParamName = $XMLFile.CreateElement("name")
                                $ParamName.InnerText = $ItemName
                                $Params.AppendChild($ParamName)
                            }

                            $ParamPath = $XMLFile.CreateElement("path")
                            $ParamPath.InnerText = $ItemPath
                            $Params.AppendChild($ParamPath)

                            if($DeleteValue){
                                $ParamValue = $XMLFile.CreateElement("value")
                                $ParamValue.InnerText = "CTXOE_DeleteValue"
                                $Params.AppendChild($ParamValue)
                            } else {
                                if($DeleteKey){
                                    $ParamValue = $XMLFile.CreateElement("value")
                                    $ParamValue.InnerText = "CTXOE_DeleteKey"
                                    $Params.AppendChild($ParamValue)
                                } else {
                                    $ParamValue = $XMLFile.CreateElement("value")
                                    $ParamValue.InnerText = $ItemValue
                                    $Params.AppendChild($ParamValue)
                                }
                            }

                            if((!($DeleteValue)) -and (!($DeleteKey))){
                                $ParamValueType = $XMLFile.CreateElement("valuetype")
                                $ParamValueType.InnerText = $ItemType
                                $Params.AppendChild($ParamValueType)
                            }

                        $Action.AppendChild($Params)

                    $Entry.AppendChild($Action)

                $Group.AppendChild($Entry)

                $XMLFile.Save($Path)
                write-verbose "Registry Entry $($EntryName) added"
                $Return.Complete = $true

            } else {

                write-verbose "Entry $($EntryName) already found - quitting"
                write-error "Entry $($EntryName) already found - quitting"

            }

        } else {

            write-verbose "Group $($GroupName) not found - quitting"
            write-error "Group $($GroupName) not found - quitting"

        }
    } else {

        write-verbose "Template $($Path) not found - quitting"
        write-error "Template $($Path) not found - quitting"

    }

} # process

end {
    
    return $Return

} # end

}