-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetLatestDevmachines.ps1
301 lines (239 loc) · 8.36 KB
/
GetLatestDevmachines.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
<#
- Requires -RunAsAdministrator, when ran from PowerShell.
- The Power Shell cmdlets and Windows Shell Extension (x64) needs to be installed
from the Team Foundation Server Power tools package for TFS get latest.
- To enable power shell script execution in AX 7.2 and up machines:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
#>
# Link modules
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
# Find the correct Package Local Directory (PLD)
$pldPath = "\AOSService\PackagesLocalDirectory"
$packageDirectory = "{0}:$pldPath" -f ('J','K')[$(Test-Path $("K:$pldPath"))]
$scriptPath = "{0}\Plugins\AxReportVmRoleStartupTask" -f $packageDirectory
Import-Module "$scriptPath\AosCommon.psm1" -force -DisableNameChecking
Import-Module "$scriptPath\CommonRollbackUtilities.psm1" -force -DisableNameChecking
function Log-Message
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string]$LogMessage
)
Write-Output ("{0} - {1}" -f (Get-Date), $LogMessage)
}
function Get-Latest()
{
try
{
$logFile = "{0}\Logs\GetLatestLog.txt" -f $PSScriptRoot
# Get the latest version of the code for the worspace
Update-TfsWorkSpace -Item $packageDirectory -Overwrite -Recurse -Version T >> $logFile
$logFile
}
catch
{
$message = '@channel {0} get latest failed' -f $env:COMPUTERNAME
$TeamMessage = '@general {0} get latest failed' -f $env:COMPUTERNAME
PostToMSTeam -Message $TeamMessage
throw 'Exception occured when getting latest checkins from source control'
}
}
function Build-Model($modelName)
{
try
{
#Rebuild selected model
$logMessage = "Rebuild model {0} start" -f $modelName
Log-Message $logMessage
$logFile = "{0}\Logs\{1}BuildLog.txt" -f $PSScriptRoot, $modelName
# Set executable
$modelExecutable = '{0}\bin\xppc.exe' -f $packageDirectory
$params = @(
'-metadata="{0}"' -f $packageDirectory
'-compilermetadata="{0}"' -f $packageDirectory
'-xref'
'-xrefSqlServer="localhost"'
'-xrefDbName="DYNAMICSXREFDB"'
'-output="{0}\{1}\bin"' -f $packageDirectory,$modelName
'-modelmodule="{0}"' -f $modelName
'-xmllog="{0}\Logs\{1}BuildModelResult.xml"' -f $PSScriptRoot,$modelName
'-log="{0}\Logs\{1}BuildModelResult.log"' -f $PSScriptRoot,$modelName
'-appBase="{0}\bin"' -f $packageDirectory
'-refPath="{0}\{1}\bin"' -f $packageDirectory,$modelName
'-referenceFolder="{0}"' -f $packageDirectory
)
# Execute build
& $modelExecutable $params 2>&1 >> $logFile
$logFile
$logMessage = "Rebuild model {0} end" -f $modelName
Log-Message $logMessage
}
catch
{
$TeamMessage = '@general {0} model {1} build failed' -f $env:COMPUTERNAME, $modelsToBuild[$i]
PostToMSTeam -Message $TeamMessage
throw 'Exception occured when building application suite'
}
}
function Build-Models
{
$modelsFile = "{0}\BuildModels.txt" -f $PSScriptRoot
$modelsToBuild = Get-Content $modelsFile
for ($i=0; $i -lt $modelsToBuild.count; $i++)
{
if ($modelsToBuild[$i] -inotin '')
{
Build-Model $modelsToBuild[$i]
$resultLOGTarget = "{0}\Logs\{1}BuildModelResult.log" -f $PSScriptRoot, $modelsToBuild[$i]
$hasErrors = Get-Content $resultLOGTarget | Select-String -Pattern 'Errors: '
if ($hasErrors[0] -inotin 'Errors: 0')
{
$message = '@channel {0} model {1} build failed with {2}' -f $env:COMPUTERNAME, $modelsToBuild[$i], $hasErrors
$message = '@general {0} model {1} build failed with {2}' -f $env:COMPUTERNAME, $modelsToBuild[$i], $hasErrors
PostToMSTeam -Message $TeamMessage
Exit
}
}
}
}
function Run-DBSync()
{
try
{
$logFile = "{0}\Logs\SyncDBLog.txt" -f $PSScriptRoot
# Set the executable
$SyncToolExecutable = '{0}\bin\SyncEngine.exe' -f $packageDirectory
# Set connection string
$connectionString = "Data Source=localhost; " +
"Integrated Security=True; " +
"Initial Catalog=AxDb"
# Set parameters
$params = @(
"-syncmode=`"fullall`""
"-metadatabinaries=$packageDirectory"
"-connect=`"$connectionString`""
)
# Execute syncronization
& $SyncToolExecutable $params 2>&1 >> $logFile
$logFile
}
catch
{
$TeamMessage = '@general {0} DB synchronization failed' -f $env:COMPUTERNAME
PostToMSTeam -Message $TeamMessage
throw 'Exception occurred when synchronizing DB'
}
}
function Run-RepPublish()
{
try
{
$logFile = "{0}\Logs\DeployReportsLog.txt" -f $PSScriptRoot
#Publish SSRS Reports
Set-ExecutionPolicy Unrestricted
& $scriptPath\DeployAllReportsToSSRS.ps1 -PackageInstallLocation "$packageDirectory" >> $logFile
$logFile
}
catch
{
$TeamMessage = '@general {0} report deployment failed' -f $env:COMPUTERNAME
PostToMSTeam -Message $TeamMessage
throw 'Exception occured when deploying report'
}
}
function resetiis
{
try
{
invoke-command -scriptblock {iisreset}
}
catch
{
$TeamMessage = '@general {0} iisreset failed' -f $env:COMPUTERNAME
PostToMSTeam -Message $TeamMessage
throw 'Exception when restting iis'
}
}
function PostToSlack
{
Param(
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'Slack channel')]
[ValidateNotNullorEmpty()]
[String]$Channel,
[Parameter(Mandatory = $true,Position = 1,HelpMessage = 'Chat message')]
[ValidateNotNullorEmpty()]
[String]$Message,
[Parameter(Mandatory = $false,Position = 2,HelpMessage = 'Slack API token')]
[ValidateNotNullorEmpty()]
[String]$token,
[Parameter(Mandatory = $false,Position = 3,HelpMessage = 'Optional name for the bot')]
[String]$BotName = 'PowerShell Bot'
)
Process {
# Static parameters
if (!$token)
{
$tokenFile = "{0}\slackToken.txt" -f $PSScriptRoot
$token = Get-Content $tokenFile
}
$uri = 'https://slack.com/api/chat.postMessage'
$body = @{
token = $token
channel = $Channel
text = $Message
username = $BotName
parse = 'full'
}
# Call the API
try
{
Invoke-RestMethod -Uri $uri -Body $body
}
catch
{
throw 'Unable to call the API'
}
}
}#>
function PostToMSTeam
{
Param(
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'Chat message')]
[ValidateNotNullorEmpty()]
[String]$Message
)
Process
{
$MSTeamUri = 'https://outlook.office.com/webhook//IncomingWebhook/f1e1aa9'
$messageBody = ConvertTo-JSON @{text = $Message}
# Call the API
try
{
Invoke-RestMethod -uri $MSTeamUri -Method Post -body $messageBody -ContentType 'application/json'
}
catch
{
throw 'Unable to call the MSTeam API'
}
}
}#>
# Call the TFS get latest functionality
#Log-Message "Get latest start"
Get-Latest
Log-Message "Get latest end"
# Call model build functinalty (there is no switch for rebuilding all models at once, so build needs to be called for each model requiring rebuild
Log-Message "Build models start"
Build-Models
Log-Message "Build models end"
# Call the DB syncronization functionality
Log-Message "DB Synchronization start"
Run-DBSync
Log-Message "DB Synchronization end"
# Call the SSRS reports deployment functionality
Log-Message "Reports deployment start"
Run-RepPublish
Log-Message "Reports deployment end"
#IIS reset
resetiis