Skip to content

Commit 26cf028

Browse files
authored
Merge pull request #3 from dbretty/Add-Scripting-Module
Added New-CitrixTemplateScript function
2 parents 1c2881f + 8edbe2a commit 26cf028

6 files changed

Lines changed: 378 additions & 2 deletions

File tree

CitrixOptimizerAutomation/CitrixOptimizerAutomation.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'CitrixOptimizerAutomation.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '2307.003'
15+
ModuleVersion = '2307.004'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -69,7 +69,7 @@ PowerShellVersion = '5.1'
6969
# NestedModules = @()
7070

7171
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72-
FunctionsToExport = @('New-CitrixTemplate', 'New-CitrixTemplateGroup', 'New-CitrixTemplateService', 'New-CitrixTemplateTask', 'New-CitrixTemplateRegistry')
72+
FunctionsToExport = @('New-CitrixTemplate', 'New-CitrixTemplateGroup', 'New-CitrixTemplateService', 'New-CitrixTemplateTask', 'New-CitrixTemplateRegistry', 'New-CitrixTemplateScript')
7373

7474
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7575
CmdletsToExport = @()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Set-PowerShellFormat {
2+
<#
3+
.SYNOPSIS
4+
Sets the PowerShell Script to Citrix Optimizer Standards.
5+
6+
.DESCRIPTION
7+
This function will take in a ps1 Script File and Format it to work with Citrix Optimizer
8+
9+
.PARAMETER ScriptData
10+
Specifies the PScript Contents
11+
12+
.INPUTS
13+
This function will take inputs via pipeline as string
14+
15+
.OUTPUTS
16+
Returns the fotmatted PowerShell Script
17+
18+
.EXAMPLE
19+
PS> Set-PowerShellFormat -ScriptData $Script -EntryName 'Disable Windows Updates'
20+
Formats the script in $Script for the Citrix Optimizer.
21+
#>
22+
23+
[CmdletBinding()]
24+
25+
Param (
26+
[Parameter(
27+
ValuefromPipelineByPropertyName = $true,mandatory=$true
28+
)]
29+
$ScriptData
30+
)
31+
32+
begin {
33+
34+
Set-StrictMode -Version Latest
35+
36+
} # begin
37+
38+
process {
39+
40+
# Build the formatted script
41+
$FormattedScript = ""
42+
$FormattedScript = $FormattedScript + "`ttry {`n"
43+
44+
# Add the script contents
45+
foreach($Line in $ScriptData){
46+
$FormattedScript = $FormattedScript + "`t`t$($Line)`n"
47+
}
48+
49+
# Add the true return value
50+
$FormattedScript = $FormattedScript + "`t`t" + '$Global' + ":CTXOE_Details = " + "Complete;`n"
51+
$FormattedScript = $FormattedScript + "`t`t" + '$Global' + ":CTXOE_Result = " + '$True;' + "`n"
52+
$FormattedScript = $FormattedScript + "`t} catch {`n"
53+
54+
# Add the false return value
55+
$FormattedScript = $FormattedScript + "`t`t" + '$Global' + ":CTXOE_Details = " + "Errored;`n"
56+
$FormattedScript = $FormattedScript + "`t`t" + '$Global' + ":CTXOE_Result = " + '$False;' + "`n"
57+
$FormattedScript = $FormattedScript + "`t}`n"
58+
59+
} # process
60+
61+
end {
62+
63+
# Pass back return object
64+
return $FormattedScript
65+
66+
} # end
67+
68+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}

Examples/TestScript.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ $Key = New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'System Optimi
4242
$Key = New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'System Optimizations' -EntryName 'Remove Edge Key 2' -EntryDescription 'Remove Edge Key 2' -ItemPath 'HKLM\Software\Microsoft\Edge2' -DeleteKey -Verbose
4343
$Key = New-CitrixTemplateRegistry -Path $Template.Path -GroupName 'System Optimizations' -EntryName 'Remove Edge Key 3' -EntryDescription 'Remove Edge Key 3' -ItemPath 'HKLM\Software\Microsoft\Edge3' -DeleteKey -Verbose
4444

45+
# Add Template Script Files
46+
$Script = New-CitrixTemplateScript -Path $Template.Path -GroupName 'System Optimizations' -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1'

Help/New-CitrixTemplateScript.MD

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# New-CitrixTemplateScript
2+
3+
Creates a new PowerShell Script definition in the Citrix Optimizer template.
4+
5+
## Syntax
6+
7+
```PowerShell
8+
New-CitrixTemplateScript
9+
[-Path] <String[]>
10+
[-GroupName] <String[]>
11+
[-EntryName] <String[]>
12+
[-EntryDescription] <String[]>
13+
[-ScriptFile] <String[]>
14+
[<CommonParameters>]
15+
```
16+
## Description
17+
18+
This function will create PowerShell Script Definition in the Citrix Optimizer template to disable.
19+
20+
## Examples
21+
22+
### Example 1
23+
24+
```PowerShell
25+
New-CitrixTemplateScript -Path 'template.xml' -GroupName 'Group 1' -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1'
26+
```
27+
28+
Adds a script entry to disable the Windows Update service in the template file.
29+
30+
### Example 2
31+
32+
```PowerShell
33+
New-CitrixTemplateScript -Path $Template.Path -GroupName 'Group 1' -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1'
34+
```
35+
36+
Adds a script entry to disable the Windows Update service in the template file using the $Template.Path return value.
37+
38+
### Example 3
39+
40+
```PowerShell
41+
New-CitrixTemplateScript -Path $Template.Path -GroupName $Group.Name -EntryName 'Stop Windows Update Services' -EntryDescription 'Stops and disables the Windows Update Service' -ScriptFile './disablewu.ps1'
42+
```
43+
44+
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.
45+
46+
## Parameters
47+
48+
### -Path
49+
50+
Specifies the Path and Name for the Citrix Optimizer Template
51+
52+
| Description | Option |
53+
|:---|:---|
54+
| Type | String |
55+
| Mandatory | True |
56+
| Default Value: | None |
57+
| Accept pipeline input: | True |
58+
| Accept wildcard characters: | False |
59+
60+
### -GroupName
61+
62+
Specifies the Group to add the Service definition to
63+
64+
| Description | Option |
65+
|:---|:---|
66+
| Type | String |
67+
| Mandatory | True |
68+
| Default Value: | None |
69+
| Accept pipeline input: | True |
70+
| Accept wildcard characters: | False |
71+
72+
### -EntryName
73+
74+
Specifies the Display Name to add to the new template
75+
76+
| Description | Option |
77+
|:---|:---|
78+
| Type | String |
79+
| Mandatory | True |
80+
| Default Value: | None |
81+
| Accept pipeline input: | True |
82+
| Accept wildcard characters: | False |
83+
84+
### -EntryDescription
85+
86+
Specifies the Description to add to the new template
87+
88+
| Description | Option |
89+
|:---|:---|
90+
| Type | String |
91+
| Mandatory | True |
92+
| Default Value: | None |
93+
| Accept pipeline input: | True |
94+
| Accept wildcard characters: | False |
95+
96+
### -ScriptFile
97+
98+
Specifies the Script File to add to the template
99+
100+
| Description | Option |
101+
|:---|:---|
102+
| Type | String |
103+
| Mandatory | True |
104+
| Default Value: | None |
105+
| Accept pipeline input: | True |
106+
| Accept wildcard characters: | False |

0 commit comments

Comments
 (0)