-
-
Notifications
You must be signed in to change notification settings - Fork 272
/
Invoke-AzureRmVmScript.ps1
332 lines (287 loc) · 13.4 KB
/
Invoke-AzureRmVmScript.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# WARNING: THIS HAS MINIMAL TESTING AND SHOULD BE CONSIDERED EXPERIMENTAL
# Use at your own risk, and read through the code before using it
function Invoke-AzureRmVmScript {
<#
.SYNOPSIS
Invoke an ad hoc PowerShell script on an AzureRM VM
.DESCRIPTION
Invoke an ad hoc PowerShell script on an AzureRM VM
Prerequisites:
* You have the AzureRM module
* You're authenticated and have appropriate privileges
* You're running PowerShell 3 or later (tested on 5, YMMV)
.PARAMETER ResourceGroupName
Resource group for the specified VMs
.PARAMETER VMName
One or more VM names to run against
.PARAMETER StorageAccountName
Storage account to store the script we invoke
.PARAMETER StorageAccountKey
Optional storage account key to generate StorageContext
If not specified, we look one up via Get-AzureRmStorageAccountKey
Note that this is a string. Beware, given the sensitivity of this key
.PARAMETER StorageContext
Optional Azure storage context to use. We build one if not provided
.PARAMETER StorageContainer
Optional StorageContainer to use. Defaults to 'scripts'
.PARAMETER Filename
Optional Filename to use. Defaults to CustomScriptExtension.ps1
.PARAMETER ExtensionName
Optional arbitrary name for the extension we add. Defaults to CustomScriptExtension
.PARAMETER ForceExtension
If specified and a CustomScriptExtension already exists on a VM, we will remove it
.PARAMETER ForceBlob
If specified and a blob exists with the same Filename and StorageContainer used here, we overwrite it
.PARAMETER Force
If specified, we trigger both ForceExtension and ForceBlob
.PARAMETER ScriptBlock
Scriptblock to invoke. It appears we can collect output from StdOut and StdErr. Keep in mind these will be in string form.
.EXAMPLE
$params = @{
ResourceGroupName = 'My-Resource-Group'
VMName = 'VM-22'
StorageAccountName = 'storageaccountname'
}
Invoke-AzureRmVmScript @params -ScriptBlock {
"Hello world! Running on $(hostname)"
Write-Error "This is an error"
Write-Warning "This is a warning"
Write-Verbose "This is verbose!"
}
# ResourceGroupName : My-Resource-Group
# VMName : VM-22
# Substatuses : {Microsoft.Azure.Management.Compute.Models.InstanceViewStatus,
# Microsoft.Azure.Management.Compute.Models.InstanceViewStatus}
# StdOut_succeeded : Hello world! Running on VM-22\nWARNING: This is a warning
# StdErr_succeeded : C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.
# 8\Downloads\0\Cus\ntomScriptExtension.ps1 : This is an
# error\n + CategoryInfo : NotSpecified: (:)
# [Write-Error], WriteErrorExcep \n tion\n +
# FullyQualifiedErrorId :
# Microsoft.PowerShell.Commands.WriteErrorExceptio \n
# n,CustomScriptExtension.ps1\n
# This example runs a simple hello world script on VM-22
# The force parameter removed an existing CustomScriptExtension,
# and overwrote a matching container/file in my azure storage account
.FUNCTIONALITY
Azure
#>
[cmdletbinding()]
param(
# todo: add various parameter niceties
[Parameter(Mandatory = $True,
Position = 0,
ValueFromPipelineByPropertyName = $True)]
[string[]]$ResourceGroupName,
[Parameter(Mandatory = $True,
Position = 1,
ValueFromPipelineByPropertyName = $True)]
[string[]]$VMName,
[Parameter(Mandatory = $True,
Position = 2)]
[scriptblock]$ScriptBlock, #todo: add file support.
[Parameter(Mandatory = $True,
Position = 3)]
[string]$StorageAccountName,
[string]$StorageAccountKey, #Maybe don't use string...
$StorageContext,
[string]$StorageContainer = 'scripts',
[string]$Filename, # Auto defined if not specified...
[string]$ExtensionName, # Auto defined if not specified
[switch]$ForceExtension,
[switch]$ForceBlob,
[switch]$Force
)
begin
{
if($Force)
{
$ForceExtension = $True
$ForceBlob = $True
}
}
process
{
Foreach($ResourceGroup in $ResourceGroupName)
{
Foreach($VM in $VMName)
{
if(-not $Filename)
{
$GUID = [GUID]::NewGuid().Guid -replace "-", "_"
$FileName = "$GUID.ps1"
}
if(-not $ExtensionName)
{
$ExtensionName = $Filename -replace '.ps1', ''
}
$CommonParams = @{
ResourceGroupName = $ResourceGroup
VMName = $VM
}
Write-Verbose "Working with ResourceGroup $ResourceGroup, VM $VM"
# Why would Get-AzureRMVmCustomScriptExtension support listing extensions regardless of name? /grumble
Try
{
$AzureRmVM = Get-AzureRmVM @CommonParams -ErrorAction Stop
$AzureRmVMExtended = Get-AzureRmVM @CommonParams -Status -ErrorAction Stop
}
Catch
{
Write-Error $_
Write-Error "Failed to retrieve existing extension data for $VM"
continue
}
# Handle existing extensions
Write-Verbose "Checking for existing extensions on VM '$VM' in resource group '$ResourceGroup'"
$Extensions = $null
$Extensions = @( $AzureRmVMExtended.Extensions | Where {$_.Type -like 'Microsoft.Compute.CustomScriptExtension'} )
if($Extensions.count -gt 0)
{
Write-Verbose "Found extensions on $VM`:`n$($Extensions | Format-List | Out-String)"
if(-not $ForceExtension)
{
Write-Warning "Found CustomScriptExtension '$($Extensions.Name)' on VM '$VM' in Resource Group '$ResourceGroup'.`n Use -ForceExtension or -Force to remove this"
continue
}
Try
{
# Theoretically can only be one, so... no looping, just remove.
$Output = Remove-AzureRmVMCustomScriptExtension @CommonParams -Name $Extensions.Name -Force -ErrorAction Stop
if($Output.StatusCode -notlike 'OK')
{
Throw "Remove-AzureRmVMCustomScriptExtension output seems off:`n$($Output | Format-List | Out-String)"
}
}
Catch
{
Write-Error $_
Write-Error "Failed to remove existing extension $($Extensions.Name) for VM '$VM' in ResourceGroup '$ResourceGroup'"
continue
}
}
# Upload the script
Write-Verbose "Uploading script to storage account $StorageAccountName"
if(-not $StorageContainer)
{
$StorageContainer = 'scripts'
}
if(-not $Filename)
{
$Filename = 'CustomScriptExtension.ps1'
}
if(-not $StorageContext)
{
if(-not $StorageAccountKey)
{
Try
{
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroup -Name $storageAccountName -ErrorAction Stop)[0].value
}
Catch
{
Write-Error $_
Write-Error "Failed to obtain Storage Account Key for storage account '$StorageAccountName' in Resource Group '$ResourceGroup' for VM '$VM'"
continue
}
}
Try
{
$StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
}
Catch
{
Write-Error $_
Write-Error "Failed to generate storage context for storage account '$StorageAccountName' in Resource Group '$ResourceGroup' for VM '$VM'"
continue
}
}
Try
{
$Script = $ScriptBlock.ToString()
$LocalFile = [System.IO.Path]::GetTempFileName()
Start-Sleep -Milliseconds 500 #This might not be needed
Set-Content $LocalFile -Value $Script -ErrorAction Stop
$params = @{
Container = $StorageContainer
Context = $StorageContext
}
$Existing = $Null
$Existing = @( Get-AzureStorageBlob @params -ErrorAction Stop )
if($Existing.Name -contains $Filename -and -not $ForceBlob)
{
Write-Warning "Found blob '$FileName' in container '$StorageContainer'.`n Use -ForceBlob or -Force to overwrite this"
continue
}
$Output = Set-AzureStorageBlobContent @params -File $Localfile -Blob $Filename -ErrorAction Stop -Force
if($Output.Name -notlike $Filename)
{
Throw "Set-AzureStorageBlobContent output seems off:`n$($Output | Format-List | Out-String)"
}
}
Catch
{
Write-Error $_
Write-Error "Failed to generate or upload local script for VM '$VM' in Resource Group '$ResourceGroup'"
continue
}
# We have a script in place, set up an extension!
Write-Verbose "Adding CustomScriptExtension to VM '$VM' in resource group '$ResourceGroup'"
Try
{
$Output = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroup `
-VMName $VM `
-Location $AzureRmVM.Location `
-FileName $Filename `
-ContainerName $StorageContainer `
-StorageAccountName $StorageAccountName `
-StorageAccountKey $StorageAccountKey `
-Name $ExtensionName `
-TypeHandlerVersion 1.1 `
-ErrorAction Stop
if($Output.StatusCode -notlike 'OK')
{
Throw "Set-AzureRmVMCustomScriptExtension output seems off:`n$($Output | Format-List | Out-String)"
}
}
Catch
{
Write-Error $_
Write-Error "Failed to set CustomScriptExtension for VM '$VM' in resource group $ResourceGroup"
continue
}
# collect the output!
Try
{
$AzureRmVmOutput = $null
$AzureRmVmOutput = Get-AzureRmVM @CommonParams -Status -ErrorAction Stop
$SubStatuses = ($AzureRmVmOutput.Extensions | Where {$_.name -like $ExtensionName} ).substatuses
}
Catch
{
Write-Error $_
Write-Error "Failed to retrieve script output data for $VM"
continue
}
$Output = [ordered]@{
ResourceGroupName = $ResourceGroup
VMName = $VM
Substatuses = $SubStatuses
}
foreach($Substatus in $SubStatuses)
{
$ThisCode = $Substatus.Code -replace 'ComponentStatus/', '' -replace '/', '_'
$Output.add($ThisCode, $Substatus.Message)
}
[pscustomobject]$Output
}
}
}
}
# TODO:
# Parameters could be nicer
# Allow parallelization. Default unspecified Filename should be unique
# Should we clean up script in Azure after running it?
# Should we allow running an existing script?
# Should we clean up the temp file?
# Other stuff, this was a super quick implementation