-
Notifications
You must be signed in to change notification settings - Fork 576
/
script-update-gateway.ps1
286 lines (238 loc) · 10.5 KB
/
script-update-gateway.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
param (
[Parameter(Mandatory = $false)]
[string]
$version,
[Parameter(Mandatory = $false)]
[string]
$allowDowngrade,
[Parameter(Mandatory = $false)]
[string]
$servicePassword
)
$ErrorActionPreference = "Stop"
$ProductName = "Microsoft Integration Runtime"
$supportedVersion = [System.Version]::new("5.4.7793.1")
function Get-PushedIntegrationRuntimeVersion() {
$latestIR = Get-RedirectedUrl "https://go.microsoft.com/fwlink/?linkid=839822"
$item = $latestIR.split("/") | Select-Object -Last 1
if ($null -eq $item -or $item -notlike "IntegrationRuntime*") {
throw "Can't get pushed $ProductName info"
}
$regexp = '^IntegrationRuntime_(\d+\.\d+\.\d+\.\d+)\s*\.msi$'
$version = [regex]::Match($item, $regexp).Groups[1].Value
if ($null -eq $version) {
throw "Can't get version from $ProductName download uri"
}
Write-InfoMsg "Pushed $ProductName version is $version"
return $version
}
function Get-IntegrationRuntimeInstaller([string] $folder, [string] $version) {
$uri = Get-InstallerUrl $version
$output = Join-Path $folder "IntegrationRuntime.msi"
Write-InfoMsg "Start to download $ProductName installer of version $version from $uri"
(New-Object System.Net.WebClient).DownloadFile($uri, $output)
if (-Not (Test-Path $output -PathType Leaf)) {
throw "Cannot download $ProductName installer of version $version"
}
Write-InfoMsg "$ProductName installer has been downloaded to $output."
return $output
}
function Get-InstallerUrl([string] $version) {
$uri = Get-RedirectedUrl
$uri = $uri.Substring(0, $uri.LastIndexOf('/') + 1)
$uri += "IntegrationRuntime_$version.msi"
return $uri
}
function Get-RedirectedUrl {
$URL = "https://go.microsoft.com/fwlink/?linkid=839822"
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect = $false
$response = $request.GetResponse()
If ($response.StatusCode -eq "Found") {
$response.GetResponseHeader("Location")
}
}
function New-TempDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
return (New-Item -ItemType Directory -Path (Join-Path $parent $name))
}
function Get-CurrentIntegrationRuntimeVersion() {
$baseFolderPath = [System.IO.Path]::GetDirectoryName((Get-CmdFilePath))
$filePath = [System.IO.Path]::Combine($baseFolderPath, "Microsoft.DataTransfer.GatewayManagement.dll")
$currentVersion = $null
if (Test-Path $filePath -PathType Leaf) {
$currentVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath).FileVersion
Write-InfoMsg "Current $ProductName version is $currentVersion"
}
return $currentVersion
}
function Get-RegistryKeyValue ([string] $registryPath) {
$baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry64)
return $baseKey.OpenSubKey($registryPath)
}
function Get-IntegrationRuntimeIdentityNumber {
$installedSoftwares = Get-ChildItem "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
foreach ($installedSoftware in $installedSoftwares) {
$displayName = $installedSoftware.GetValue("DisplayName")
if ($DisplayName -eq "$ProductName Preview" -or $DisplayName -eq "$ProductName") {
return Split-Path $installedSoftware.Name -Leaf
}
}
return $null
}
function Backup-IntegrationRuntimeConfig ([string] $installPath) {
Write-InfoMsg "Start to backup $ProductName configuration files."
$cmd = Get-CmdFilePath
$process = Start-Process $cmd "-ucf ""$installPath"" store" -Wait -PassThru -NoNewWindow
if ($process.ExitCode -ne 0) {
throw "Failed to backup $ProductName configuration files. Exit code: $($process.ExitCode)"
}
Write-InfoMsg "Succeed to backup $ProductName configuration files."
}
function Restore-IntegrationRuntimeConfig ([string] $installPath) {
Write-InfoMsg "Start to restore $ProductName configuration files."
$cmd = Get-CmdFilePath
$process = Start-Process $cmd "-ucf ""$installPath"" recover true" -Wait -PassThru -NoNewWindow
if ($process.ExitCode -ne 0) {
throw "Failed to restore $ProductName configuration files. Exit code: $($process.ExitCode)"
}
Write-InfoMsg "Succeed to restore $ProductName configuration files."
}
function Get-CmdFilePath {
$registryKeyValue = Get-RegistryKeyValue "Software\Microsoft\DataTransfer\DataManagementGateway\ConfigurationManager"
$filePath = $registryKeyValue.GetValue("DiacmdPath")
if ([string]::IsNullOrEmpty($filePath)) {
throw "Cannot find CLI executable file."
}
return (Split-Path -Parent $filePath) + "\dmgcmd.exe"
}
function Get-IntegrationRuntimeServiceAccount {
$irService = Get-WmiObject win32_service | Where-Object { $_.Name -eq "DIAHostService" }
return $irService.startname
}
function Set-IntegrationRuntimeServiceAccount ([string] $account, [string] $password) {
$cmd = Get-CmdFilePath
$process = Start-Process $cmd "-ssa $account $password" -Wait -PassThru -NoNewWindow
if ($process.ExitCode -ne 0) {
throw "Failed to set service account of $ProductName. Exit code: $($process.ExitCode)"
}
Write-InfoMsg "Succeed to set service account of $ProductName."
}
function Get-IntegrationRuntimeExeFolder {
$registryKeyValue = Get-RegistryKeyValue "Software\Microsoft\DataTransfer\DataManagementGateway\ConfigurationManager"
return Split-Path $registryKeyValue.GetValue("DiacmdPath") -Parent
}
function Uninstall-IntegrationRuntime ([string] $identityNumber) {
Write-InfoMsg "Start to uninstall $ProductName with identity number: $identityNumber"
$process = Start-Process "msiexec.exe" "/x $identityNumber /quiet KEEPDATA=1" -Wait -PassThru -NoNewWindow
if ($process.ExitCode -ne 0) {
throw "Failed to uninstall $ProductName. Exit code: $($process.ExitCode). Please try to uninstall $ProductName manually"
}
Write-InfoMsg "Succeed to uninstall $ProductName."
}
function Install-IntegrationRuntime ([string] $installerPath, [string] $installedPath, [boolean] $skipStartService) {
Write-InfoMsg "Start to install $ProductName"
$installArgs = "/i $installerPath /quiet"
if (-not [string]::IsNullOrEmpty($installedPath)) {
$installArgs += " INSTALLLOCATION=""$installedPath"""
}
if ($skipStartService) {
$installArgs += " SKIPSTARTSERVICE=""Yes"""
}
Write-InfoMsg "Install arguments: $installArgs"
$process = Start-Process "msiexec.exe" $installArgs -Wait -PassThru -NoNewWindow
if ($process.ExitCode -ne 0) {
throw "Failed to install $ProductName. Exit code: $($process.ExitCode). Please try to install $installPath manually"
}
Write-InfoMsg "Succeed to install $ProductName."
}
function Write-InfoMsg([string] $msg) {
Write-Host "[$(Get-UtcNowString)][Info] $msg"
}
function Write-ErrorMsg([string] $msg) {
Write-Host "[$(Get-UtcNowString)][Error] $msg" -ForegroundColor Red
}
function Get-UtcNowString {
$utcNow = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'UTC')
return $utcNow.ToString("yyyy-MM-dd HH:mm:ss UTC")
}
# Main
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-ErrorMsg "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Exit 1001
}
if ([string]::IsNullOrWhiteSpace($version)) {
$version = Get-PushedIntegrationRuntimeVersion
}
$versionObject = $null
if (-Not [System.Version]::TryParse($version, [ref]$versionObject)) {
Write-ErrorMsg "Invalid version value: $version."
Exit 1002
}
if ($versionObject -lt $supportedVersion) {
Write-ErrorMsg "Update to version less than $supportedVersion isn't supported."
Exit 1003
}
Write-InfoMsg "The version to be installed is $version"
$identityNumber = Get-IntegrationRuntimeIdentityNumber
$installed = ($null -ne $identityNumber)
if ($installed -and ($version -ieq (Get-CurrentIntegrationRuntimeVersion))) {
Write-InfoMsg "Current installed $ProductName is $version, no operation needed."
Exit
}
$isDowngrade = ($installed -and ($versionObject -lt [System.Version]::Parse((Get-CurrentIntegrationRuntimeVersion))))
if ($isDowngrade -and ($allowDowngrade -ine "true")) {
Write-ErrorMsg "You're trying to downgrade $ProductName, please set '-allowDowngrade' to true to enable downgrade."
Exit 1004
}
$serviceAccount = $null
if ($installed -and ("NT SERVICE\DIAHostService" -ine (Get-IntegrationRuntimeServiceAccount))) {
$serviceAccount = Get-IntegrationRuntimeServiceAccount
Write-InfoMsg "$ProductName service account is $serviceAccount"
}
if (($null -ne $serviceAccount) -and $isDowngrade -and ($null -eq $servicePassword)) {
Write-ErrorMsg "$ProductName isn't run as default account. You need to provide password of $serviceAccount with option '-servicePassword' to process downgrade."
Exit 1005
}
$installedPath = $null
if ($installed) {
$installedPath = (Split-Path -Parent (Split-Path -Parent (Split-Path -Parent (Get-CmdFilePath))))
}
$tmpFolder = New-TempDirectory
$installerPath = Get-IntegrationRuntimeInstaller $tmpFolder $version
if (-Not (Test-Path -Path $installerPath -PathType Leaf)) {
Write-ErrorMsg "The installer $installerPath doesn't exist."
Exit
}
if ($installed -and $isDowngrade) {
$backupPath = Join-Path -Path $installedPath -ChildPath "$([System.Version]::Parse((Get-CurrentIntegrationRuntimeVersion)).Major).0"
Backup-IntegrationRuntimeConfig $backupPath
Write-InfoMsg "Uninstall old $ProductName."
Uninstall-IntegrationRuntime $identityNumber
}
Write-InfoMsg "Install $ProductName"
Install-IntegrationRuntime $installerPath $installedPath ($null -ne $serviceAccount)
if ($isDowngrade) {
try {
$restorePath = Join-Path -Path $installedPath -ChildPath "$($versionObject.Major).0"
Restore-IntegrationRuntimeConfig $restorePath
}
catch {
Write-ErrorMsg $_
}
if ($null -ne $serviceAccount) {
try {
Set-IntegrationRuntimeServiceAccount $serviceAccount $servicePassword
}
catch {
Write-ErrorMsg $_
}
}
}
Write-InfoMsg "Clean up downloaded $ProductName installer: $installerPath."
Remove-Item $installerPath
if ($null -eq $identityNumber) {
Write-InfoMsg "Install complete. You may need to open $ProductName to manually register node."
}