1
+ $DmgcmdPath = " C:\Program Files\Microsoft Integration Runtime\4.0\Shared\dmgcmd.exe"
2
+
3
+ function Write-Log ($Message ) {
4
+ function TS { Get-Date - Format ' MM/dd/yyyy hh:mm:ss' }
5
+ Write-Host " [$ ( TS) ] $Message "
6
+ }
7
+
8
+ function Check-Is-Registered () {
9
+ $result = Get-ItemProperty - Path ' HKLM:\SOFTWARE\Microsoft\DataTransfer\DataManagementGateway\ConfigurationManager' - Name HaveRun - ErrorAction SilentlyContinue
10
+ if (($result -ne $null ) -and ($result.HaveRun -eq ' Mdw' )) {
11
+ return $TRUE
12
+ }
13
+ return $FALSE
14
+ }
15
+
16
+ function Check-Main-Process () {
17
+ $ProcessResult = Get-WmiObject Win32_Process - Filter " name = 'diahost.exe'"
18
+
19
+ if ($ProcessResult ) {
20
+ return $TRUE
21
+ }
22
+ else {
23
+ throw " Main Process not found"
24
+ }
25
+ }
26
+
27
+ function Check-Node-Connection () {
28
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -cgc" - RedirectStandardOutput " C:\SHIR\status-check.txt"
29
+ $ConnectionResult = Get-Content " C:\SHIR\status-check.txt"
30
+ Remove-Item - Force " C:\SHIR\status-check.txt"
31
+
32
+ if ($ConnectionResult -like " Connected" ) {
33
+ return $TRUE
34
+ }
35
+ else {
36
+ throw " Node is offline"
37
+ }
38
+ }
39
+
40
+
41
+ function RegisterNewNode {
42
+ Param (
43
+ $AUTH_KEY ,
44
+ $NODE_NAME ,
45
+ $ENABLE_HA ,
46
+ $HA_PORT
47
+ )
48
+
49
+ Write-Log " Start registering the new SHIR node"
50
+
51
+ if (! $NODE_NAME ) {
52
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -RegisterNewNode" , " $ ( $AUTH_KEY ) " - RedirectStandardOutput " C:\SHIR\register-out.txt" - RedirectStandardError " C:\SHIR\register-error.txt"
53
+ } else {
54
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -RegisterNewNode" , " $ ( $AUTH_KEY ) " , " $ ( $NODE_NAME ) " - RedirectStandardOutput " C:\SHIR\register-out.txt" - RedirectStandardError " C:\SHIR\register-error.txt"
55
+ }
56
+
57
+ if ($ENABLE_HA -eq " true" ) {
58
+ Write-Log " Enable High Availability"
59
+ $PORT = $HA_PORT -or " 8060"
60
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -EnableRemoteAccess" , " $ ( $PORT ) "
61
+ }
62
+
63
+ $StdOutResult = Get-Content " C:\SHIR\register-out.txt"
64
+ $StdErrResult = Get-Content " C:\SHIR\register-error.txt"
65
+
66
+
67
+ if ($StdOutResult )
68
+ {
69
+ Write-Log " Registration output:"
70
+ $StdOutResult | ForEach-Object { Write-Log $_ }
71
+ }
72
+
73
+ if ($StdErrResult )
74
+ {
75
+ Write-Log " Registration errors:"
76
+ $StdErrResult | ForEach-Object { Write-Log $_ }
77
+ }
78
+ }
79
+
80
+ # Register SHIR with key from Env Variable: AUTH_KEY
81
+ if (Check- Is- Registered) {
82
+ Write-Log " Restart the existing node"
83
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -Start"
84
+ } elseif (Test-Path Env:AUTH_KEY) {
85
+ Write-Log " Registering SHIR with the node key: $ ( (Get-Item Env:AUTH_KEY).Value) "
86
+ Write-Log " Registering SHIR with the node name: $ ( (Get-Item Env:NODE_NAME).Value) "
87
+ Write-Log " Registering SHIR with the enable high availability flag: $ ( (Get-Item Env:ENABLE_HA).Value) "
88
+ Write-Log " Registering SHIR with the tcp port: $ ( (Get-Item Env:HA_PORT).Value) "
89
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -Start"
90
+ RegisterNewNode (Get-Item Env:AUTH_KEY).Value (Get-Item Env:NODE_NAME).Value (Get-Item Env:ENABLE_HA).Value (Get-Item Env:HA_PORT).Value
91
+ } else {
92
+ Write-Log " Invalid AUTH_KEY Value"
93
+ exit 1
94
+ }
95
+
96
+ Write-Log " Waiting 30 seconds waiting for connecting"
97
+ Start-Sleep - Seconds 30
98
+
99
+ try {
100
+ while ($TRUE ) {
101
+ if ((Check- Main- Process) -and (Check- Node- Connection)) {
102
+ Write-Log " Node Health Check Pass"
103
+ Start-Sleep - Seconds 60
104
+ continue
105
+ }
106
+ }
107
+ }
108
+ finally {
109
+ Write-Log " Stop the node connection"
110
+ Start-Process $DmgcmdPath - Wait - ArgumentList " -Stop"
111
+ Write-Log " Stop the node connection successfully"
112
+ exit 0
113
+ }
114
+
115
+ exit 1
0 commit comments