This repository was archived by the owner on Jul 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploy-AXModel.ps1
More file actions
793 lines (648 loc) · 28 KB
/
Deploy-AXModel.ps1
File metadata and controls
793 lines (648 loc) · 28 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# Deploy a given model database to PRODUCTION AX
Param(
$aosServerNames = ('AU11STHAOSPRD01', 'AU11STHAOSPRD02', 'AU11STHAOSPRD03', 'AU11STHAOSPRD04', 'AU11STHAOSPRD05', 'AU11STHAOSPRD06', 'AU11STHAOSPRD07', 'AU11STHAOSPRD08'),
$primaryAOSName = 'AU11STHAOSPRD07',
$aosBatchServer = 'AU11STHAOSPRD01',
$AXServiceName = 'AOS60$01',
$XppILFolderStr = 'C$\Program Files\Microsoft Dynamics AX\60\Server\DynamicsAX_PROD\bin\XppIL\',
$SSRSServer = 'AU11STHSSRPRD01',
$SSRSServiceName = 'ReportServer',
$LasernetServer = 'AU11STHLSNPRD01',
$LasernetServiceName = 'Lasernet 7 (PROD:3279)',
$LasernetPrintCaptureServiceName = 'Lasernet Print Capture 7',
$SQLServer = 'MHSTHSQLPRD01',
$AXDatabaseName = 'DynamicsAX_PROD',
$AXModelName = 'DynamicsAX_PROD_model',
$dbBackupPath = 'E:\MSSQL\Backup\',
$ModelToRestore = 'E:\AXDeployments\Restore\DynamicsAX_Staging_model_20180406-1100.bak',
$NotifyEmail = ('laurencel@sable37.com', 'marcelf@sable37.com', 'alan.mills@steelandtube.co.nz', 'judy.vanschaik@steelandtube.co.nz', 'colin.williams@steelandtube.co.nz', 'matthew.rangiwahia@steelandtube.co.nz')
)
$ErrorActionPreference = "Stop"
function Send-StepNotification {
param(
$emailTo = $(throw "Please specify the email destination"),
$emailFrom = "AXdeployment@steelandtube.co.nz",
$smtpServer = "scantomailmx1.steel.steelandtube.co.nz",
$subject = "AX deployment",
$atachFile,
$notification
)
if ($atachFile)
{
Send-MailMessage -to $emailTo -Subject $subject -Body $notification -SmtpServer $smtpServer -from $emailFrom -BodyAsHtml -Attachments $atachFile
}
else
{
Send-MailMessage -to $emailTo -Subject $subject -Body $notification -SmtpServer $smtpServer -from $emailFrom -BodyAsHtml
}
#Sleep to send emails in the right order
Start-Sleep -Seconds 5
Write-Output $notification
}
Send-StepNotification -subject "Starting AX deployment" -notification ("Server " + $env:COMPUTERNAME) -emailTo $NotifyEmail
#region Create log folder
$logFolder = 'C:\Scripts\log\' + (Get-Date -Format 'yyyyMMdd-HHmm')
try
{
New-Item -ItemType Directory -Force -Path $logFolder
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Could not create log file ' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
#endregion
#region Disable batch server
Send-StepNotification -subject "Starting step 'Disable batch server & drain AOS to stop'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm ') -emailTo $NotifyEmail
#create xpo
$automationAXClass = 'STH_DeploymentAutomation'
$automationXPO = ($logFolder+'\STH_DeploymentAutomation.xpo')
Add-Content -Path $automationXPO -Value @"
Exportfile for AOT version 1.0 or later
Formatversion: 1
***Element: CLS
; Microsoft Dynamics AX Class: STH_DeploymentAutomation unloaded
; --------------------------------------------------------------------------------
CLSVERSION 1
CLASS #STH_DeploymentAutomation
PROPERTIES
Name #STH_DeploymentAutomation
Origin #{791F3C97-A023-4C37-91B7-634F5760372A}
ENDPROPERTIES
METHODS
SOURCE #classDeclaration
#/// <summary>
#/// This class is used to automate deployment
#/// </summary>
#/// <remarks>
#/// This class is imported by the deployment powershell script
#/// </remarks>
#
#class STH_DeploymentAutomation
#{
#}
ENDSOURCE
SOURCE #disableAllBatchServer
#public static void disableAllBatchServer()
#{
# SysServerConfig sysServerConfig;
#
# update_recordSet SysServerConfig
# setting enablebatch = NoYes::No;
#
#}
ENDSOURCE
SOURCE #drainAllServers
#public static void drainAllServers()
#{
# SysServersDrain sysServersDrain;
# SysServerSessions serverSessions;
# container serversList;
# ;
# while select serverSessions
# where serverSessions.Status == SysServerStatus::Alive
# {
# serversList += [[serverSessions.Instance_Name, serverSessions.ServerId]];
# }
# sysServersDrain = SysServersDrain::newServersList(SysServerStatus::Drain, serversList);
# sysServersDrain.run();
#}
ENDSOURCE
SOURCE #enableBatchServer
#public static void enableBatchServer(ServerId _serverId)
#{
# SysServerConfig sysServerConfig;
#
# ttsBegin;
#
# select forupdate sysServerConfig where sysServerConfig.ServerId like '*'+ _serverId;
# if(sysServerConfig.RecId)
# {
# sysServerConfig.EnableBatch = NoYes::Yes;
# sysServerConfig.doUpdate();
# ttsCommit;
# }
# else
# {
# ttsAbort;
# error(strFmt('Could not find server %1',_serverId));
# }
#}
ENDSOURCE
SOURCE #killAllSessions
#public static void killAllSessions()
#{
# SysClientSessions sysClientSessions;
# xSession xSession;
# SessionType tmpSessionType;
#
# while select sysClientSessions
# where sysClientSessions.Status == SessionStatus::Running
# {
# xSession = new xSession(sysClientSessions.SessionId, true);
# tmpSessionType = sysClientSessions.sessionType;
# info(strFmt('The session "%1" type "%2" of the user "%3" will be terminated',sysClientSessions.SessionId, enum2str(tmpSessionType), sysClientSessions.userId));
# xSession.terminate();
# }
#}
ENDSOURCE
ENDMETHODS
ENDCLASS
***Element: END
"@
#createXML command to importXPO
$xmlImportXPO = $logFolder+'\XpoImport.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlImportXPO,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($logFolder + '\XpoImport.log'))
$xmlWriter.WriteStartElement("XpoImport")
$XmlWriter.WriteAttributeString("file",$automationXPO)
$xmlWriter.WriteEndElement # <-- XpoImport
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
#importXPO
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlImportXPO
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
#Stop batch server
$xmlAXRun = $logFolder+'\RunStopBatch.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlAXRun,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($xmlAXRun + '.log'))
$xmlWriter.WriteStartElement("Run")
$XmlWriter.WriteAttributeString("type","Class")
$XmlWriter.WriteAttributeString("name",$automationAXClass)
$XmlWriter.WriteAttributeString("method","disableAllBatchServer")
$xmlWriter.WriteEndElement # <-- Run
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
#Stop Batch server
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlAXRun
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
$drainDate = Get-Date -Format 'dd/MM/yyyy'
$drainTime = Get-Date -Format 'HH:mm'
#Send-StepNotification -subject "Step complete 'Disable batch server & drain AOS to stop'" -notification ('date:'+$drainDate+' time:'+$drainTime) -emailTo $NotifyEmail
#endregion
#region Stop all AOS in parallel
Send-StepNotification -subject "Starting step 'Stop ALL AOS for PROD'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
$servicesList = New-Object System.Collections.ArrayList
foreach ($AOSServer in $aosServerNames)
{
$service = Get-Service -ComputerName $AOSServer $AXServiceName
$servicesList.Add($service) | Out-Null
if ($service.Status -eq "Stopped")
{
continue
}
if ($service.Status -eq "Running" -and $service.CanStop -eq $True)
{
Write-Output "Stopping AOS in $AOSServer"
$service.Stop()
}
else
{
throw "Could not stop AOS in $AOSServer"
}
}
foreach ($service in $servicesList)
{
Write-Output "Waiting for server $($service.MachineName) to stop"
$service.WaitForStatus("stopped")
Write-Output "AOS in' $($service.MachineName) is stopped"
}
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Could not stop AOS' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$stopAllAOSDate = Get-Date -Format 'dd/MM/yyyy'
$stopAllAOSTime = Get-Date -Format 'HH:mm'
#Send-StepNotification -subject "Step complete 'Stop ALL AOS for PROD'" -notification ('date:'+$stopAllAOSDate+' time:'+$stopAllAOSTime) -emailTo $NotifyEmail
#endregion
#region SQL AX DB backup
Send-StepNotification -subject "Starting step 'Take backup of target model and database'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
#Create SQL session
$SQLsession = New-PSSession -ComputerName $SQLServer
$BackupFile = $dbBackupPath + $AXDatabaseName + "_" + (Get-Date -Format yyyyMMdd-HHmm) + ".bak"
Invoke-Command -Session $SQLsession -ScriptBlock{Backup-SqlDatabase -ServerInstance $args[0] -Database $args[1] -BackupFile $args[2] -Verbose} -ArgumentList $SQLServer, $AXDatabaseName, $BackupFile
}
catch
{
Send-StepNotification -notification ('Backup AX database failed ' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$AXDBBkpDate = Get-Date -Format 'dd/MM/yyyy'
$AXDBBkpTime = Get-Date -Format 'HH:mm'
$AXDBBkpDetail = 'Backup file:' + $BackupFile + ' server' + $SQLServer
#Send-StepNotification -subject "Step complete 'Take backup of target database'" -notification ('date:'+$AXDBBkpDate+' time:'+$AXDBBkpTime + ' ' + $AXDBBkpDetail) -emailTo $NotifyEmail
#endregion
#region SQL AX MODEL backup
Send-StepNotification -subject "Starting step 'Take backup of target model'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
$BackupFile = $dbBackupPath + $AXModelName + "_" + (Get-Date -Format yyyyMMdd-HHmm) + ".bak"
try
{
Invoke-Command -Session $SQLsession -ScriptBlock{Backup-SqlDatabase -ServerInstance $args[0] -Database $args[1] -BackupFile $args[2] -Verbose} -ArgumentList $SQLServer, $AXModelName, $BackupFile
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('SQL backup error ' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$AXModelBkpDate = Get-Date -Format 'dd/MM/yyyy'
$AXModelBkpTime = Get-Date -Format 'HH:mm'
$AXModelBkpDetail = 'Backup file:' + $BackupFile + ' server' + $SQLServer
Send-StepNotification -subject "Step complete 'Take backup of target model'" -notification ('date:'+$AXModelBkpDate+' time:'+$AXModelBkpTime + ' ' + $AXModelBkpDetail) -emailTo $NotifyEmail
#endregion
#region SQL AX MODEL restore
Send-StepNotification -subject "Starting step 'Restore back up of Staging model database over target model database'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
Invoke-Command -Session $SQLsession -ScriptBlock{Restore-SqlDatabase -ServerInstance $args[0] -Database $args[1] -BackupFile $args[2] -Replace -Verbose} -ArgumentList $SQLServer, $AXModelName, $ModelToRestore
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('SQL restore error' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$AXRestoreBkpDate = Get-Date -Format 'dd/MM/yyyy'
$AXRestoreBkpTime = Get-Date -Format 'HH:mm'
$AXRestoreBkpDetail = 'Restored from file:' + $ModelToRestore + ' server' + $SQLServer
#Send-StepNotification -subject "Step complete 'Restore back up of Staging model database over target model database'" -notification ('date:'+$AXRestoreBkpDate+' time:'+$AXRestoreBkpTime + ' ' + $AXRestoreBkpDetail) -emailTo $NotifyEmail
#endregion
#region Clean XppIL folders on all AOSes
Send-StepNotification -subject "Starting step 'Clean XppIL folders on all AOSes'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
foreach ($AOSServer in $aosServerNames)
{
$currentAOSXppILFolderStr = "\\" + $AOSServer + "\" + $XppILFolderStr
$currentAOSXppILFolderStrDeleteFilter = $currentAOSXppILFolderStr + "*"
Remove-Item $currentAOSXppILFolderStrDeleteFilter -Force -Recurse -ErrorAction SilentlyContinue -Verbose
}
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Clean XppIL folders on all AOSes ' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$CleanXppIlDate = Get-Date -Format 'dd/MM/yyyy'
$CleanXppIlTime = Get-Date -Format 'HH:mm'
#Send-StepNotification -subject "Step complete 'Clean XppIL folders on all AOSes'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#endregion
#region Truncate SYSXPPASSEMBLY table within the model database
Send-StepNotification -subject "Starting step 'Truncate SYSXPPASSEMBLY CIL system table'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
$SQLCmdTruncateCILTable = "DELETE FROM [dbo].[SYSXPPASSEMBLY]"
icm -Session $SQLsession -ScriptBlock{Invoke-Sqlcmd -ServerInstance $args[0] -Database $args[1] -Query $args[2] -Verbose} -ArgumentList $SQLServer, $AXModelName, $SQLCmdTruncateCILTable
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Truncate SYSXPPASSEMBLY CIL system table' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
#Send-StepNotification -subject "Step complete 'Truncate SYSXPPASSEMBLY CIL system table'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#endregion
#region Truncate SYSCLIENTSESSIONS table within AX database
Send-StepNotification -subject "Starting step 'Truncate SYSCLIENTSESSIONS table'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
$SQLCmdTruncateSYSCLIENTSESSIONSTable = "TRUNCATE TABLE [dbo].[SYSCLIENTSESSIONS]"
Invoke-Command -Session $SQLsession -ScriptBlock{Invoke-Sqlcmd -ServerInstance $args[0] -Database $args[1] -Query $args[2] -Verbose} `
-ArgumentList $SQLServer, $AXDatabaseName, $SQLCmdTruncateSYSCLIENTSESSIONSTable
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Truncate SYSCLIENTSESSIONS table' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
#endregion
#region Clear SQL plan cache
Send-StepNotification -subject "Starting step 'Clear SQL plan cache'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
$SQLCmdDBCCFREEPROCCACHE = "DBCC FREEPROCCACHE"
Invoke-Command -Session $SQLsession -ScriptBlock{Invoke-Sqlcmd -ServerInstance $args[0] -Database $args[1] -Query $args[2] -Verbose} `
-ArgumentList $SQLServer, $AXDatabaseName, $SQLCmdDBCCFREEPROCCACHE
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Clear SQL plan cache' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
#endregion
#region Model optimization
Import-module "C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1"
$targetserver = $SQLServer
$targetdb = $AXModelName
$targetAOSAccount = 'steel\svc.dax.live.aos'
Initialize-AXModelStore -Server $targetserver -Database $targetdb
Grant-AXModelStore -Server $targetserver -Database $targetdb -AOSAccount $targetAOSAccount
Optimize-AXModelStore -Server $targetserver -Database $targetdb
& 'C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\axutil.exe' 'refreshrolecache'
#endregion
#region Start main AOS
Send-StepNotification -subject "Starting step 'Start primary AOS'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
try
{
Get-Service -ComputerName $primaryAOSName $AXServiceName | Start-Service
}
catch
{
Send-StepNotification -subject 'Deployment failed' -notification ('Could not start AOS ' + (Get-Date -Format 'dd/MM/yyyy - HH:mm ') + $_.Exception.Message) -emailTo $NotifyEmail
break
}
$startPrimaryAOSDate = Get-Date -Format 'dd/MM/yyyy'
$startPrimaryAOSTime = Get-Date -Format 'HH:mm'
$startPrimaryAOSDetail = 'AOS name:' + $primaryAOSName + 'service name:'+$AXServiceName
#Send-StepNotification -subject "Step complete 'Start primary AOS'" -notification ('date:'+$startPrimaryAOSDate+' time:'+$startPrimaryAOSTime + ' ' + $startPrimaryAOSDetail) -emailTo $NotifyEmail
#endregion
#region AX client - Generate CIL
Send-StepNotification -subject "Starting step 'Run full CIL'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
$xmlAXRun = $logFolder+'\CompileIl.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlAXRun,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($xmlAXRun + '.txt'))
$xmlWriter.WriteStartElement("CompileIL")
$XmlWriter.WriteAttributeString("incremental","false")
$xmlWriter.WriteEndElement # <-- Run
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
#AX client - Generate full CIL
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlAXRun
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
$generateFullCilDate = Get-Date -Format 'dd/MM/yyyy'
$generateFullCilTime = Get-Date -Format 'HH:mm'
$generateFullCilDetail = 'Log file:' + $xmlAXRun + '.txt'
Send-StepNotification -subject "Step complete 'Run full CIL'" -notification ('date:'+$generateFullCilDate+' time:'+$generateFullCilTime + ' ' + $generateFullCilDetail) -emailTo $NotifyEmail -atachFile ($xmlAXRun + '.txt')
#endregion
#region AX client - Synchronize
Send-StepNotification -subject "Starting step 'Run full Synchronisation'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
$xmlAXRun = $logFolder+'\Sync.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlAXRun,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($xmlAXRun + '.txt'))
$xmlWriter.WriteStartElement("Synchronize")
$xmlWriter.WriteEndElement # <-- Run
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlAXRun
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
$runFullSyncDate = Get-Date -Format 'dd/MM/yyyy'
$runFullSyncTime = Get-Date -Format 'HH:mm'
$runFullSyncDetail = 'Log file:' + $xmlAXRun + '.txt'
Send-StepNotification -subject "Step complete 'Run full Synchronisation'" -notification ('date:'+$runFullSyncDate+' time:'+$runFullSyncTime + ' ' + $runFullSyncDetail) -emailTo $NotifyEmail -atachFile ($xmlAXRun + '.txt')
#endregion
#region Start all AOS
Send-StepNotification -subject "Starting step 'Restart all AOS for the target environment'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#Stop main AOS
Get-Service -ComputerName $primaryAOSName $AXServiceName | Stop-Service
#Start all AOS
foreach ($AOSServer in $aosServerNames)
{
Get-Service -ComputerName $AOSServer $AXServiceName | Start-Service -Verbose
}
$restartAllAOSDate = Get-Date -Format 'dd/MM/yyyy'
$restartAllAOSTime = Get-Date -Format 'HH:mm'
$restartAllAOSDetail = 'Application object servers' + $aosServerNames
Send-StepNotification -subject "Step complete 'Restart all AOS for the target environment'" -notification ('date:'+$restartAllAOSDate+' time:'+$restartAllAOSTime + ' ' + $restartAllAOSDetail) -emailTo $NotifyEmail
#endregion
#region Restart SSRS
Send-StepNotification -subject "Starting step 'Restart SSRS reporting server services'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
Get-Service -ComputerName $SSRSServer $SSRSServiceName | Restart-Service
Send-StepNotification -subject "Step complete 'Restart SSRS reporting server services'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#endregion
#region Restart Lasernet service
Send-StepNotification -subject "Starting step 'Restart Lasernet reporting server services'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
Get-Service -ComputerName $LasernetServer $LasernetServiceName | Restart-Service
Get-Service -ComputerName $LasernetServer $LasernetPrintCaptureServiceName | Restart-Service
Send-StepNotification -subject "Step complete 'Restart Lasernet reporting server services'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#endregion
#region Enable batch server
Send-StepNotification -subject "Starting step 'Enable batch server'" -notification (Get-Date -Format 'dd/MM/yyyy - HH:mm') -emailTo $NotifyEmail
#createXML command to importXPO
$xmlImportXPO = $logFolder+'\XpoImport2.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlImportXPO,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($logFolder + '\XpoImport2.log'))
$xmlWriter.WriteStartElement("XpoImport")
$XmlWriter.WriteAttributeString("file",$automationXPO)
$xmlWriter.WriteEndElement # <-- XpoImport
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
#importXPO
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlImportXPO
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
#Start batch server
$xmlAXRun = $logFolder+'\RunStartBatch.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlAXRun,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement("AxaptaAutoRun")
# Write the Document
$xmlWriter.WriteAttributeString("exitWhenDone","True")
$xmlWriter.WriteAttributeString("version","6.0")
$xmlWriter.WriteAttributeString("logFile", ($xmlAXRun + '.log'))
$xmlWriter.WriteStartElement("Run")
$XmlWriter.WriteAttributeString("type","Class")
$XmlWriter.WriteAttributeString("name",$automationAXClass)
$XmlWriter.WriteAttributeString("method","enableBatchServer")
$XmlWriter.WriteAttributeString("parameters","'"+$aosBatchServer+"'")
$xmlWriter.WriteEndElement # <-- Run
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
#Enable Batch server
$startUpCommand = '-StartUpCmd=Autorun_'+$xmlAXRun
& 'C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe' $startUpCommand | Out-Null
$enableBatchAOSDate = Get-Date -Format 'dd/MM/yyyy'
$enableBatchAOSTime = Get-Date -Format 'HH:mm'
$enableBatchAOSDetail = 'Batch server enabled:' + $aosBatchServer
Send-StepNotification -subject "Step complete 'Enable batch server'" -notification ('date:'+$enableBatchAOSDate+' time:'+$enableBatchAOSTime + ' ' + $enableBatchDetail) -emailTo $NotifyEmail
#endregion
#region Final report HTML
$finalReport = @"
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
th {
text-align: left;
}
</style>
</head>
<body>
<table style="width:100%">
<caption><b>AX deployment checklist</b></caption>
<tr>
<td><b>Item / task</b></td>
<td><b>Date</b></td>
<td><b>Time</b></td>
<td><b>Details</b></td>
</tr>
<tr>
<td>Disable batch server & drain AOS to stop</td>
<td>$drainDate</td>
<td>$drainTime</td>
<td>$drainDetails</td>
</tr>
<tr>
<td>Stop ALL AOS for PROD</td>
<td>$stopAllAOSDate</td>
<td>$stopAllAOSTime</td>
<td>$stopAllAOSDetails</td>
</tr>
<tr>
<td>Take backup of target database</td>
<td>$AXDBBkpDate</td>
<td>$AXDBBkpTime</td>
<td>$AXDBBkpDetail</td>
</tr>
<tr>
<td>Take backup of target model</td>
<td>$AXModelBkpDate</td>
<td>$AXModelBkpTime</td>
<td>$AXModelBkpDetail</td>
</tr>
<tr>
<td>Restore back up of Staging model database over target model database</td>
<td>$AXRestoreBkpDate</td>
<td>$AXRestoreBkpTime</td>
<td>$AXRestoreBkpDetail</td>
</tr>
<tr>
<td>Clean XppIL folders on all AOSes</td>
<td>$CleanXppIlDate</td>
<td>$CleanXppIlTime</td>
<td></td>
</tr>
<tr>
<td>Start primary AOS</td>
<td>$startPrimaryAOSDate</td>
<td>$startPrimaryAOSTime</td>
<td>$startPrimaryAOSDetail</td>
</tr>
<tr>
<td>Run full CIL</td>
<td>$generateFullCilDate</td>
<td>$generateFullCilTime</td>
<td>$generateFullCilDetail</td>
</tr>
<tr>
<td>Run full Synchronisation</td>
<td>$runFullSyncDate</td>
<td>$runFullSyncTime</td>
<td>$runFullSyncDetail</td>
</tr>
<tr>
<td>Restart all AOS for the target environment</td>
<td>$restartAllAOSDate</td>
<td>$restartAllAOSTime</td>
<td>$restartAllAOSDetail</td>
</tr>
<tr>
<td>Enable batch server</td>
<td>$enableBatchAOSDate</td>
<td>$enableBatchAOSTime</td>
<td>$enableBatchAOSDetail</td>
</tr>
</table>
</body>
</html>
"@
#endregion
Send-StepNotification -subject "Deployment complete" -notification $finalReport -emailTo $NotifyEmail