forked from nicolaszhang/DataManagementGatewayScripts
-
Notifications
You must be signed in to change notification settings - Fork 11
/
script-update-gateway.ps1
203 lines (166 loc) · 5.31 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
# This script is used to udpate/ install + register latest self-hosted integration runtime.
# And the steps are like this:
# 1. check current self-hosted IR version
# 2. Get latest version or specified version from argument
# 3. if there is newer version than current version
# 3.1 download self-hosted IR msi
# 3.2 upgrade it
## And here is the usage:
## 1. Download and install latest gateway
## PS > .\script-update-gateway.ps1
## 2. Download and install gateway of specified version
## PS > .\script-update-gateway.ps1 -version 2.11.6380.20
param([string]$version)
function Get-CurrentGatewayVersion()
{
$registryKeyValue = Get-RegistryKeyValue "Software\Microsoft\DataTransfer\DataManagementGateway\ConfigurationManager"
$baseFolderPath = [System.IO.Path]::GetDirectoryName($registryKeyValue.GetValue("DiacmdPath"))
$filePath = [System.IO.Path]::Combine($baseFolderPath, "Microsoft.DataTransfer.GatewayManagement.dll")
$version = $null
if (Test-Path $filePath)
{
$version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath).FileVersion
$msg = "Current gateway: " + $version
Write-Host $msg
}
return $version
}
function Get-LatestGatewayVersion()
{
$latestGateway = Get-RedirectedUrl "https://go.microsoft.com/fwlink/?linkid=839822"
$item = $latestGateway.split("/") | Select-Object -Last 1
if ($item -eq $null -or $item -notlike "IntegrationRuntime*")
{
throw "Can't get latest gateway info"
}
$regexp = '^IntegrationRuntime_(\d+\.\d+\.\d+\.\d+)((?:\w|%20)+)\(64-bit\)\.msi$'
$version = [regex]::Match($item, $regexp).Groups[1].Value
if ($version -eq $null)
{
throw "Can't get version from gateway download uri"
}
$msg = "Latest gateway: " + $version
Write-Host $msg
return $version
}
function Get-RegistryKeyValue
{
param($registryPath)
$is64Bits = Is-64BitSystem
if($is64Bits)
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry64)
return $baseKey.OpenSubKey($registryPath)
}
else
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry32)
return $baseKey.OpenSubKey($registryPath)
}
}
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 Download-GatewayInstaller
{
Param (
[Parameter(Mandatory=$true)]
[String]$version
)
Write-Host "Start to download MSI"
$uri = Populate-Url $version
$folder = New-TempDirectory
$output = Join-Path $folder "IntegrationRuntime.msi"
(New-Object System.Net.WebClient).DownloadFile($uri, $output)
$exist = Test-Path($output)
if ( $exist -eq $false)
{
throw "Cannot download specified MSI"
}
$msg = "New gateway MSI has been downloaded to " + $output
Write-Host $msg
return $output
}
function Populate-Url
{
Param (
[Parameter(Mandatory=$true)]
[String]$version
)
$uri = Get-RedirectedUrl
$uri = $uri.Substring(0, $uri.LastIndexOf('/') + 1)
$uri += "IntegrationRuntime_$version ("
$is64Bits = Is-64BitSystem
if ($is64Bits)
{
$uri += "64-bit"
}
else
{
$uri += "32-bit"
}
$uri += ").msi"
return $uri
}
function Install-Gateway
{
Param (
[Parameter(Mandatory=$true)]
[String]$msi
)
$exist = Test-Path($msi)
if ( $exist -eq $false)
{
throw 'there is no MSI found: $msi'
}
Write-Host "Start to install gateway ..."
$arg = "/i " + $msi + " /quiet /norestart"
Start-Process -FilePath "msiexec.exe" -ArgumentList $arg -Wait -Passthru -NoNewWindow
Write-Host "Gateway has been successfully updated!"
}
function New-TempDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
function Is-64BitSystem
{
$computerName= $env:COMPUTERNAME
$osBit = (get-wmiobject win32_processor -computername $computerName).AddressWidth
return $osBit -eq '64'
}
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
$currentVersion = Get-CurrentGatewayVersion
if ($currentVersion -eq $null)
{
Write-Host "There is no gateway found on your machine, exiting ..."
break
}
$versionToInstall = $version
if ([string]::IsNullOrEmpty($versionToInstall))
{
$versionToInstall = Get-LatestGatewayVersion
}
if ([System.Version]$currentVersion -ge [System.Version]$versionToInstall)
{
Write-Host "Your gateway is latest, no update need..."
}
else
{
$msi = Download-GatewayInstaller $versionToInstall
Install-Gateway $msi
Remove-Item -Path $msi -Force
}