-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-AzureStartupTask.ps1
159 lines (138 loc) · 5.75 KB
/
Add-AzureStartupTask.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
function Add-AzureStartupTask
{
<#
.Synopsis
Adds a startup task to Azure
.Description
Adds a startup task to an azure service configuration, and packs some extra information into the XML to allow
using ScriptBlock as startup tasks
.Example
New-AzureServiceDefinition -ServiceName "MyService" |
Add-AzureStartupTask -ScriptBlock { "Hello World" } -Elevated -asString
.Link
Out-AzureService
#>
[OutputType([xml],[string])]
[CmdletBinding(DefaultParameterSetName='CommandLine')]
param(
# The Service Definition XML
[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 role
[string]
$ToRole,
# The command line to run
[Parameter(Mandatory=$true,ParameterSetName='CommandLine')]
[string]
$CommandLine,
# The batch script to run
[Parameter(Mandatory=$true,ParameterSetName='BatchScript')]
[string]
$BatchScript,
# The ScriptBlock to run.
[Parameter(Mandatory=$true,ParameterSetName='ScriptBlock')]
[ScriptBlock]
$ScriptBlock,
# The parameter to be passed to the script block
[Parameter(ParameterSetName='ScriptBlock')]
[Hashtable]
$Parameter,
# The task type.
[ValidateSet('Simple', 'Background', 'Foreground')]
[string]
$TaskType = 'Simple',
# If set, the task will be run elevated
[switch]
$Elevated,
# If set, returns the service definition XML up to this point as a string
[switch]
$AsString
)
process {
$taskType = $taskType.ToLower()
# 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.Startup) {
$role.InnerXml += "<Startup/>"
}
$startupNode = Select-Xml -Xml $role -Namespace $xmlNamespace -XPath '//ServiceDefinition:Startup' |
Select-Object -ExpandProperty Node -First 1
$execContext= if ($elevated) { 'elevated' } else { 'limited' }
if ($psCmdlet.ParameterSetName -eq 'CommandLine') {
$startupNode.InnerXml += "<Task commandLine='$CommandLine' executionContext='$execContext' taskType='$taskType'/>"
} elseif ($psCmdlet.ParameterSetName -eq 'ScriptBlock') {
$parameterChunk = if ($parameter) {
$parameterChunk = "<Parameters>"
foreach ($kv in $parameter.GetEnumerator()) {
if ($kv.Value) {
$parameterChunk += "<Parameter name='$($kv.Key)' value='$([Security.SecurityElement]::Escape($kv.Value))' />"
} else {
$parameterChunk += "<Parameter name='$($kv.Key)' />"
}
}
$parameterChunk += "</Parameters>"
} else { ""}
$startupNode.InnerXml += "<Task commandLine='' executionContext='$execContext' taskType='$taskType'>
<ScriptBlock>
$([Security.SecurityElement]::Escape($ScriptBlock))
</ScriptBlock>
$parameterChunk
</Task>"
} elseif ($psCmdlet.ParameterSetName -eq 'BatchScript') {
$startupNode.InnerXml += "<Task commandLine='' executionContext='$execContext' taskType='$taskType'>
<Batch>
$([Security.SecurityElement]::Escape($BatchScript))
</Batch>
</Task>"
}
}
end {
if ($AsString) {
$strWrite = New-Object IO.StringWriter
$serviceDefinition.Save($strWrite)
return "$strWrite"
} else {
$serviceDefinition
}
}
}