-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdateManagement-RunCommand.ps1
137 lines (100 loc) · 3.45 KB
/
UpdateManagement-RunCommand.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
<#PSScriptInfo
.VERSION 1.0
.GUID 5494503d-d282-435f-99c4-575a66335141
.AUTHOR zachal
.COMPANYNAME Microsoft
.COPYRIGHT
.TAGS UpdateManagement, Automation
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES ThreadJob
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.DESCRIPTION
This script is intended to be run as a part of Update Management Pre/Post scripts.
It uses RunCommand to execute a PowerShell script to stop a service.
#>
<#
.SYNOPSIS
Stop a service on an AzureRM using RunCommand
.DESCRIPTION
This script is intended to be run as a part of Update Management Pre/Post scripts.
It uses RunCommand to execute a PowerShell script to stop a service.
.PARAMETER SoftwareUpdateConfigurationRunContext
This is a system variable which is automatically passed in by Update Management during a deployment.
#>
#requires -Modules ThreadJob
param(
[string]$SoftwareUpdateConfigurationRunContext
)
#region BoilerplateAuthentication
#This requires a RunAs account
$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$AzureContext = Select-AzureRmSubscription -SubscriptionId $ServicePrincipalConnection.SubscriptionID
#endregion BoilerplateAuthentication
#If you wish to use the run context, it must be converted from JSON
$context = ConvertFrom-Json $SoftwareUpdateConfigurationRunContext
$vmIds = $context.SoftwareUpdateConfigurationSettings.AzureVirtualMachines
$runId = $context.SoftwareUpdateConfigurationRunId
if (!$vmIds)
{
#Workaround: Had to change JSON formatting
$Settings = ConvertFrom-Json $context.SoftwareUpdateConfigurationSettings
#Write-Output "List of settings: $Settings"
$VmIds = $Settings.AzureVirtualMachines
#Write-Output "Azure VMs: $VmIds"
if (!$vmIds)
{
Write-Output "No Azure VMs found"
return
}
}
#The script you wish to run on each VM
$scriptBlock = @"
Stop-Service -Name "AudioSvc"
"@
$scriptPath = "$runID.ps1"
#The cmdlet only accepts a file, so temporarily write the script to disk using runID as a unique name
Out-File -FilePath $scriptPath -InputObject $scriptBlock
$scriptFile = get-item $scriptpath
$fullPath = $scriptfile.fullname
$jobIDs= New-Object System.Collections.Generic.List[System.Object]
#Start script on each machine
$vmIds | ForEach-Object {
$vmId = $_
$split = $vmId -split "/";
$subscriptionId = $split[2];
$rg = $split[4];
$name = $split[8];
Write-Output ("Subscription Id: " + $subscriptionId)
$mute = Select-AzureRmSubscription -Subscription $subscriptionId
Write-Output "Invoking command on '$($name)' ..."
$newJob = Start-ThreadJob -ScriptBlock { param($resourceGroup, $vmName, $scriptPath) Invoke-AzureRmVMRunCommand -ResourceGroupName $resourceGroup -Name $VmName -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath} -ArgumentList $rg, $name, $fullPath
$jobIDs.Add($newJob.Id)
}
$jobsList = $jobIDs.ToArray()
if ($jobsList)
{
Write-Output "Waiting for machines to finish executing..."
Wait-Job -Id $jobsList
}
foreach($id in $jobsList)
{
$job = Get-Job -Id $id
if ($job.Error)
{
Write-Output $job.Error
}
}
#Clean up our variables:
Remove-Item -Path "$runID.ps1"