-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-AzureSetting.ps1
122 lines (110 loc) · 4.84 KB
/
Add-AzureSetting.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
function Add-AzureSetting
{
<#
.Synopsis
Adds an Azure local storage resource to a service definition
.Description
Adds an Azure local storage resource to a service definition.
Azure local storage can create well-known directories on the host machine
.Link
New-AzureServiceDefinition
.Example
New-AzureServiceDefinition -ServiceName MyService |
Add-AzureSetting -Name MySetting -Value MyValue -AsString
#>
[CmdletBinding(DefaultParameterSetName='NameAndValue')]
[OutputType([xml],[string])]
param(
# The ServiceDefinition XML. This should be created with New-AzureServiceDefinition or retreived with Import-AzureServiceDefinition
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({
$isServiceDefinition = $_.NameTable.Get("http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition")
if (-not $IsServiceDefinition) {
throw "Input must be a ServiceDefinition XML"
}
return $true
})]
[Xml]
$ServiceDefinition,
# The name of the setting to configure
[Parameter(Mandatory=$true, ParameterSetName='NameAndValue')]
[string]
$Name,
# The value to us for the setting
[Parameter(Mandatory=$true, ParameterSetName='NameAndValue')]
[string]
$Value,
# A table of names and values for Azure settings
[Parameter(Mandatory=$true, ParameterSetName='SettingTable')]
[Hashtable]
$Setting,
# If set, will output results as string rather than XML
[switch]
$AsString
)
process {
if ($psCmdlet.ParameterSetName -eq 'NameAndValue') {
# Resolve the role if it set, create the role if it doesn't exist, and track it if they assume the last item.
$roles = @($ServiceDefinition.ServiceDefinition.WebRole),
@($ServiceDefinition.ServiceDefinition.WorkerRole) +
@($ServiceDefinition.ServiceDefinition.VirtualMachineRole)
$xmlNamespace = @{'ServiceDefinition'='http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'}
$selectXmlParams = @{
XPath = '//ServiceDefinition:WebRole|//ServiceDefinition:WorkerRole|//ServiceDefinition:VirtualMachineRole'
Namespace = $xmlNamespace
}
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
if (-not $roles) {
$ServiceDefinition = $ServiceDefinition |
Add-AzureRole -RoleName "WebRole1"
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
}
if ($roles.Count -gt 1) {
if ($ToRole) {
} else {
$role = $roles[-1]
}
} else {
if ($ToRole) {
if ($roles[0].Name -eq $ToRole) {
$role = $roles[0]
} else {
$role = $null
}
} else {
$role = $roles[0]
}
}
if (-not $role) { return }
if (-not $role.ConfigurationSettings) {
$role.InnerXml += "<ConfigurationSettings/>"
}
$ConfigurationSettingsNode =
$(Select-Xml -Xml $role -Namespace $xmlNamespace -XPath '//ServiceDefinition:ConfigurationSettings' |
Select-Object -ExpandProperty Node -First 1)
$ConfigurationSettingsNode.InnerXml += "<Setting name='$Name' value='$([Security.SecurityElement]::Escape($value))'/>"
} elseif ($psCmdlet.ParameterSetName -eq 'SettingTable') {
$null = $psboundParameters.Remove('asString')
$null = $psboundParameters.Remove('setting')
foreach ($kv in $setting.GetEnumerator()) {
$psboundParameters.Name = $kv.Key
$psboundParameters.Value = $kv.Value
$psboundParameters.ServiceDefinition = $ServiceDefinition
$ServiceDefinition = & $myInvocation.MyCommand @psBoundParameters
}
}
}
end {
if ($AsString) {
$strWrite = New-Object IO.StringWriter
$serviceDefinition.Save($strWrite)
return "$strWrite"
} else {
$serviceDefinition
}
}
}