-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudStackDeployVMWithReports.ps1
200 lines (190 loc) · 9.33 KB
/
CloudStackDeployVMWithReports.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<#
.SYNOPSIS
A CloudStack/CloudPlatform Virtual Machine Desployment Scriptlet
.DESCRIPTION
Use this script to deploy a virtual machine in a CloudStack Cloud.
.PARAMETER templateid
The template ID for the VM.
.PARAMETER zoneid
The zone for the VM
.PARAMETER serviceofferingid
The Service offering ID for the VM
.PARAMETER securitygroupids
The Security Group IDs for the VM
.PARAMETER networkids
The Security Group IDs for the VM
#>
# Writen by Jeff Moody (fifthecho@gmail.com)
#
# 2013/7/19 v1.0 created
Param(
[Parameter(Mandatory=$true)]
[String]
$templateid
,
[Parameter(Mandatory=$true)]
[String]
$zoneid
,
[Parameter(Mandatory=$true)]
[String]
$serviceofferingid
,
[Parameter(Mandatory=$false)]
[Array]
$securitygroupids
,
[Parameter(Mandatory=$false)]
[Array]
$networkids
)
Import-Module CloudStackReportsClient
$parameters = Import-CloudstackReportsConfig
$EMails = $parameters[3]
$mailbody = ""
$hostname = $env:COMPUTERNAME
if ($parameters -ne 1) {
$cloud = New-CloudStackReports -apiEndpoint $parameters[0] -apiPublicKey $parameters[1] -apiSecretKey $parameters[2]
$date = Get-Date
$startTime = $date.ToShortTimeString()
if($securitygroupids) {
$sids = $securitygroupids | Sort-Object
$sids = $sids -join "%2c"
$job = Get-CloudStackReports -cloudStack $cloud -command deployVirtualMachine -options serviceofferingid=$serviceofferingid,zoneid=$zoneid,securitygroupids=$sids,templateid=$templateid
}
elseif($networkids) {
$nids = $networkids | Sort-Object
$nids = $nids -join "%2c"
$job = Get-CloudStackReports -cloudStack $cloud -command deployVirtualMachine -options serviceofferingid=$serviceofferingid,zoneid=$zoneid,networkids=$nids,templateid=$templateid
}
else {
Write-Error "No network or security groups specified."
}
if($job){
$jobid = $job.deployvirtualmachineresponse.jobid
$mailbody += "Started VM Deployment job <i>$jobid</i> at $startTime for template <i>$templateid</i>.<br />`n"
do {
Write-Host -NoNewline "."
$jobStatus = Get-CloudStackReports -cloudStack $cloud -command queryAsyncJobResult -options jobid=$jobid
Start-Sleep -Seconds 2
}
while ($jobStatus.queryasyncjobresultresponse.jobstatus -eq 0)
Write-Host " "
$statusCode = $jobStatus.queryasyncjobresultresponse.jobresultcode
if ($statusCode -ne 0) {
Write-Error $jobStatus.queryasyncjobresultresponse.errortext
}
else {
$vm = $jobStatus.queryasyncjobresultresponse.jobresult.virtualmachine
$vmid = $vm.id
$ip = $vm.nic.ipaddress
$password = $vm.password
$date = Get-Date
$endTime = $date.ToShortTimeString()
$mailbody += "`nCompleted at $endTime.<br /><br />"
$mailbody += "`nVM $vmid deployed. VM IP Address is <b>$ip</b> and Administrator password is <i>$password</i> <br />"
Write-Debug "Mail body: `n$mailbody"
Send-MailMessage -From "cloud@$hostname" -To $EMails -Body $mailbody -Subject "CloudStack VM Deployment" -BodyAsHtml -SmtpServer localhost
}
}
}
else {
Write-Error "Please configure the $env:userprofile\cloud-settings.txt file"
}
# SIG # Begin signature block
# MIIRpQYJKoZIhvcNAQcCoIIRljCCEZICAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjc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