-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSet-WEMCitrixOptimizerConfiguration.ps1
162 lines (134 loc) · 6.23 KB
/
Set-WEMCitrixOptimizerConfiguration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<#
.Synopsis
Updates a Citrix Optimizer Configuration object in the WEM Database.
.Description
Updates a Citrix Optimizer Configuration object in the WEM Database.
.Link
https://msfreaks.wordpress.com
.Parameter IdSite
..
.Parameter IdTemplate
..
.Parameter State
..
.Parameter Groups
..
.Parameter Targets
..
.Parameter Connection
..
.Example
.Notes
Author: Arjan Mensch
#>
function Set-WEMCitrixOptimizerConfiguration {
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[int]$IdSite,
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)]
[int]$IdTemplate,
[Parameter(Mandatory=$False)][ValidateSet("Enabled","Disabled")]
[string]$State,
[Parameter(Mandatory=$False)]
[string[]]$Groups,
[Parameter(Mandatory=$False)]
[string[]]$Targets,
[Parameter(Mandatory=$True)]
[System.Data.SqlClient.SqlConnection]$Connection
)
process {
Write-Verbose "Working with database version $($script:databaseVersion)"
Write-Verbose "Function name '$($MyInvocation.MyCommand.Name)'"
# only continue if the WEM version supports it
if ($script:databaseSchema -lt 2003) {
Write-Error "WEM $($script:databaseSchema) does not support Citrix Optimizer Configurations"
Break
}
# only continue if a valid IdSite was passed
if (-not (Get-WEMConfiguration -Connection $Connection -IdSite $IdSite)) {
Write-Warning "No site found with IdSite $($IdSite)"
Break
}
# grab original object
$origObject = Get-WEMCitrixOptimizerConfiguration -Connection $Connection -IdSite $IdSite -IdTemplate $IdTemplate -Verbose
# only continue if the object was found
if (-not $origObject) {
Write-Warning "No Citrix Optimizer Configuration object found for Id $($IdTemplate)"
Break
}
# determine targets
$newtargets = 0
if ([bool]($MyInvocation.BoundParameters.Keys -match 'targets')) {
foreach($target in $Targets) {
if ($configurationSettings."$($script:databaseSchema)".VUEMCitrixOptimizerTargets.GetEnumerator() | Where-Object {$_.Value -eq $target}) {
$newtargets += [int]($configurationSettings."$($script:databaseSchema)".VUEMCitrixOptimizerTargets.GetEnumerator() | Where-Object {$_.Value -eq $target}).Name
} else {
Write-Host "Cannot apply Targets parameter.`n'$($target)' does not exist in WEM $($script:databaseSchema)" -ForegroundColor Red
Break
}
}
Write-Verbose "Determined Targets are valid"
}
# check State (only 1 Target OS template may be enabled at any time)
if ([bool]($MyInvocation.BoundParameters.Keys -match 'state') -and $State -eq "Enabled") {
$configurations = Get-WEMCitrixOptimizerConfiguration -Connection $Connection -IdSite $IdSite -State Enabled
# check Target OSs
foreach ($configuration in $configurations) {
$conftargets = 0
foreach ($target in $configuration.Targets) {
$conftargets += [int]($configurationSettings."$($script:databaseSchema)".VUEMCitrixOptimizerTargets.GetEnumerator() | Where-Object {$_.Value -eq $target}).Name
}
if($newtargets -band $conftargets) {
Write-Host "Cannot Enable this Citrix Optimizer Configuration.`nOnly one Target OS Template may be enabled at any time.`n'$($configuration.Name)' is already enabled for one or more of the requested target OSs." -ForegroundColor Red
Break
}
}
Write-Verbose "Determined State can indeed be Enabled"
}
$selectedGroups = @()
$unselectedGroups = @()
$availableGroups = $origObject.TemplateXml.root.group.displayname
# check Groups
if ([bool]($MyInvocation.BoundParameters.Keys -match 'groups')) {
if (@($Groups | Where-Object { $availableGroups -notcontains $_ } | Select-Object -first 1).Count) {
Write-Host "One or more of the requested groups are not available in the template." -ForegroundColor Red
Break
}
$selectedGroups = $Groups
$unselectedGroups = @($availableGroups | Where-Object { $_ -notin $selectedGroups })
}
# build the query to update the object
$SQLQuery = "UPDATE VUEMCitrixOptimizerConfigurations SET "
$updateFields = @()
$keys = $MyInvocation.BoundParameters.Keys | Where-Object { $_ -notmatch "connection" -and $_ -notmatch "idtemplate" -and $_ -notmatch "idsite" }
foreach ($key in $keys) {
switch ($key) {
"State" {
$updateFields += "State = $($tableVUEMState["$($State)"])"
continue
}
"Groups" {
$updateFields += "SelectedGroups = '$($selectedGroups -join ",")'"
$updateFields += "UnselectedGroups = '$($unselectedGroups -join ",")'"
continue
}
"Targets" {
$updateFields += "Targets = $($newtargets)"
continue
}
Default {}
}
}
# if anything needs to be updated, update the object
if($updateFields) {
$SQLQuery += "{0}, " -f ($updateFields -join ", ")
$SQLQuery += "RevisionId = $($origObject.Version + 1) WHERE IdTemplate = $($IdTemplate)"
$null = Invoke-SQL -Connection $Connection -Query $SQLQuery
# Updating the ChangeLog
New-ChangesLogEntry -Connection $Connection -IdSite $origObject.IdSite -IdElement $IdTemplate -ChangeType "Update" -ObjectName $origObject.Name -ObjectType "Citrix Optimizer\Configurations" -NewValue "N/A" -ChangeDescription $null -Reserved01 $null
} else {
Write-Warning "No parameters to update were provided"
}
}
}