|
| 1 | +function New-CitrixTemplateScript { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Creates a new PowerShell Script definition in the Citrix Optimizer template. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function will create PowerShell Script Definition in the Citrix Optimizer template to disable. |
| 8 | + |
| 9 | + .PARAMETER Path |
| 10 | + Specifies the Path to the template file |
| 11 | +
|
| 12 | + .PARAMETER GroupName |
| 13 | + The existing Group to add the Service to |
| 14 | +
|
| 15 | + .PARAMETER EntryName |
| 16 | + The Display Name in Citrix Optimizer |
| 17 | +
|
| 18 | + .PARAMETER EntryDescription |
| 19 | + The Description in Citrix Optimizer |
| 20 | +
|
| 21 | + .PARAMETER ScriptFile |
| 22 | + The Service Name to Disable |
| 23 | +
|
| 24 | + .INPUTS |
| 25 | + This function will take inputs via pipeline as string |
| 26 | +
|
| 27 | + .OUTPUTS |
| 28 | + Returns $true or $false depending on the Script creation state |
| 29 | +
|
| 30 | + .EXAMPLE |
| 31 | + PS> New-CitrixTemplateScript -Path 'template.xml' -GroupName 'Group 1' -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1' |
| 32 | + Adds a script entry to disable the Windows Update service in the template file. |
| 33 | + .EXAMPLE |
| 34 | + PS> New-CitrixTemplateScript -Path $Template.Path -GroupName 'Group 1' -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1' |
| 35 | + Adds a script entry to disable the Windows Update service in the template file using the $Template.Path return value. |
| 36 | + .EXAMPLE |
| 37 | + PS> New-CitrixTemplateScript -Path $Template.Path -GroupName $Group.Name -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1' |
| 38 | + Adds a script entry to disable the Windows Update service in the template file using the $Template.Path return value and the $Group.Name return value. |
| 39 | +
|
| 40 | + .LINK |
| 41 | + https://github.com/dbretty/Citrix.Optimizer.Template/blob/main/Help/New-CitrixTemplateScript.MD |
| 42 | +#> |
| 43 | + |
| 44 | +[CmdletBinding()] |
| 45 | + |
| 46 | +Param ( |
| 47 | + [Parameter( |
| 48 | + ValuefromPipelineByPropertyName = $true,mandatory=$true |
| 49 | + )] |
| 50 | + [System.String]$Path, |
| 51 | + [Parameter( |
| 52 | + ValuefromPipelineByPropertyName = $true,mandatory=$true |
| 53 | + )] |
| 54 | + [System.String]$GroupName, |
| 55 | + [Parameter( |
| 56 | + ValuefromPipelineByPropertyName = $true,mandatory=$true |
| 57 | + )] |
| 58 | + [System.String]$EntryName, |
| 59 | + [Parameter( |
| 60 | + ValuefromPipelineByPropertyName = $true,mandatory=$true |
| 61 | + )] |
| 62 | + [System.String]$EntryDescription, |
| 63 | + [Parameter( |
| 64 | + ValuefromPipelineByPropertyName = $true,mandatory=$true |
| 65 | + )] |
| 66 | + [System.String]$ScriptFile |
| 67 | +) |
| 68 | + |
| 69 | +begin { |
| 70 | + |
| 71 | + # Set strict mode and initial return value |
| 72 | + Set-StrictMode -Version Latest |
| 73 | + |
| 74 | + # Set up PSCustom Object for return |
| 75 | + $Return = New-Object -TypeName psobject |
| 76 | + $Return | Add-Member -MemberType NoteProperty -Name "Complete" -Value $false |
| 77 | + |
| 78 | +} # begin |
| 79 | + |
| 80 | +process { |
| 81 | + |
| 82 | + # Check if the Script File Passed in is valid |
| 83 | + if(Test-Path -Path $ScriptFile){ |
| 84 | + |
| 85 | + # Check if the template already exists |
| 86 | + if(Get-Template -Path $Path){ |
| 87 | + |
| 88 | + write-verbose "Citrix Optimizer Template $($Path) found" |
| 89 | + write-verbose "Load Citrix Optimizer Template" |
| 90 | + |
| 91 | + # Load Template and check for existing Group and Service" |
| 92 | + [XML]$xmlfile = Get-Content $Path |
| 93 | + |
| 94 | + # Check that the Group exists |
| 95 | + if(Get-TemplateGroup -Path $Path -GroupName $GroupName){ |
| 96 | + |
| 97 | + write-verbose "Group $($GroupName) found" |
| 98 | + |
| 99 | + # Check the Entry is not already present |
| 100 | + if(!(Get-TemplateEntry -Path $Path -EntryName $EntryName)){ |
| 101 | + |
| 102 | + write-verbose "Script $($EntryName) not found, adding" |
| 103 | + |
| 104 | + # Get the Group XML details into a variable |
| 105 | + $Group = $xmlfile.root.group | where-object {$_.id -eq $($GroupName)} |
| 106 | + |
| 107 | + # Create the Entry element |
| 108 | + write-verbose "Create Entry element" |
| 109 | + $Entry = $XMLFile.CreateElement("entry") |
| 110 | + |
| 111 | + # Create the Entry header element |
| 112 | + write-verbose "Create Name, Description and Execute element" |
| 113 | + $Name = $XMLFile.CreateElement("name") |
| 114 | + $Name.InnerText = $EntryName |
| 115 | + $Entry.AppendChild($Name) |
| 116 | + |
| 117 | + $Description = $xmlfile.CreateElement("description") |
| 118 | + $Description.InnerText = $EntryDescription |
| 119 | + $Entry.AppendChild($Description) |
| 120 | + |
| 121 | + $Execute = $xmlfile.CreateElement("execute") |
| 122 | + $Execute.InnerText = "1" |
| 123 | + $Entry.AppendChild($Execute) |
| 124 | + |
| 125 | + # Create the action element |
| 126 | + write-verbose "Create Action element" |
| 127 | + $Action = $XMLFile.CreateElement("action") |
| 128 | + |
| 129 | + # Create the plugin element |
| 130 | + write-verbose "Create Plugin element" |
| 131 | + $Plugin = $XMLFile.CreateElement("plugin") |
| 132 | + $Plugin.InnerText = "PowerShell" |
| 133 | + $Action.AppendChild($Plugin) |
| 134 | + |
| 135 | + # Create the executeparams element |
| 136 | + write-verbose "Create ExecuteParams element" |
| 137 | + $Params = $XMLFile.CreateElement("executeparams") |
| 138 | + |
| 139 | + # Format the PowerShell to Citrix Optimizer Standard |
| 140 | + $ScriptData = Get-Content $ScriptFile |
| 141 | + $FinalScript = Set-PowerShellFormat -ScriptData $ScriptData |
| 142 | + |
| 143 | + # Write the PowerShell Formatted Script |
| 144 | + $ParamValue = $XMLFile.CreateElement("value") |
| 145 | + $ParamValue.InnerText = $FinalScript |
| 146 | + $ParamValue.AppendChild($XMLFile.CreateCDataSection($FinalScript)) |
| 147 | + $Params.AppendChild($ParamValue) |
| 148 | + |
| 149 | + $Action.AppendChild($Params) |
| 150 | + |
| 151 | + $Entry.AppendChild($Action) |
| 152 | + |
| 153 | + $Group.AppendChild($Entry) |
| 154 | + |
| 155 | + # Close and save the XML file |
| 156 | + $XMLFile.Save($Path) |
| 157 | + write-verbose "Script $($EntryName) added" |
| 158 | + $Return.Complete = $true |
| 159 | + |
| 160 | + } else { |
| 161 | + |
| 162 | + write-verbose "Entry $($EntryName) already found - quitting" |
| 163 | + write-error "Entry $($EntryName) already found - quitting" |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + } else { |
| 168 | + |
| 169 | + # Group was not found in template |
| 170 | + write-verbose "Group $($GroupName) not found - quitting" |
| 171 | + write-error "Group $($GroupName) not found - quitting" |
| 172 | + |
| 173 | + } |
| 174 | + } else { |
| 175 | + |
| 176 | + # Template file not found |
| 177 | + write-verbose "Template $($Path) not found - quitting" |
| 178 | + write-error "Template $($Path) not found - quitting" |
| 179 | + |
| 180 | + } |
| 181 | + } else { |
| 182 | + |
| 183 | + # Script file not found |
| 184 | + write-verbose "Script $($ScriptFile) not found - quitting" |
| 185 | + write-error "Script $($ScriptFile) not found - quitting" |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | +} # process |
| 190 | + |
| 191 | +end { |
| 192 | + |
| 193 | + # Pass back return object |
| 194 | + return $Return |
| 195 | + |
| 196 | +} # end |
| 197 | + |
| 198 | +} |
0 commit comments