-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-AzureRole.ps1
108 lines (96 loc) · 3.32 KB
/
Add-AzureRole.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
function Add-AzureRole
{
<#
.Synopsis
Adds and azure role to a service definition
.Description
Adds an azure role to a service definition
.Example
New-AzureServiceDefinition -ServiceName AService |
Add-AzureRole -RoleName MyWebRole -VMSize Large -RoleType Web -AsString
.Link
New-AzureServiceDefinition
#>
[OutputType([xml],[string])]
param(
# The Service Definition
[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 role
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[string]
$RoleName,
# The VMSize
[ValidateSet('ExtraSmall','Small','Medium', 'Large', 'Extra-Large', 'XS', 'XL', 'S', 'M', 'L')]
[string]
$VMSize,
# If set, will disable native code execution on the role. This will prevent PHP or other CGI from working
[Switch]
$DisableNativeCodeExecution,
# If set, will output as a string
[switch]
$AsString,
# The type of the role.
[ValidateSet('Web','Worker','VirtualMachine', 'VM')]
[string]
$RoleType = 'Web'
)
process {
#region Correct Parameters
$enableNativeCodeExecution = (-not $DisableNativeCodeExecution).ToString().ToLower()
$vmSize = if ('XS' -eq $VmSize) {
"ExtraSmall"
} elseif ('XL' -eq $VmSize) {
"ExtraLarge"
} elseif ('M' -eq $VmSize) {
"Medium"
} elseif ('S' -eq $VmSize) {
"Small"
} elseif ('L' -eq $VmSize) {
"Large"
} elseif ($vmSize) {
$vmSize
} else {
$null
}
if ($vmSize) {
# Force every instance of a subword into camel case
foreach ($subWord in 'Extra','Small', 'Medium', 'Large') {
$vmSize= $vmSize -ireplace $subWord, $subWord
}
}
#endregion Correct Parameters
$roleElement = if ($roleType -eq 'Web') {
"WebRole"
} elseif ($roleType -eq 'Worker') {
"WorkerRole"
} elseif ('VirtualMachine', 'VM' -contains $roleType) {
"VirtualMachineRole"
}
if ($vmSize) {
@($serviceDefinition.ChildNodes)[-1].InnerXml += "<$roleElement name='$RoleName' vmsize='$VMSize' enableNativeCodeExecution='$enableNativeCodeExecution' />"
} else {
@($serviceDefinition.ChildNodes)[-1].InnerXml += "<$roleElement name='$RoleName' enableNativeCodeExecution='$enableNativeCodeExecution' />"
}
}
end {
if ($AsString) {
$strWrite = New-Object IO.StringWriter
$serviceDefinition.Save($strWrite)
return "$strWrite"
} else {
$serviceDefinition
}
}
}