forked from elangovana/publish-btdfBiztalkApplication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Publish-BiztalkBtdfApplication.psm1
1281 lines (987 loc) · 51.3 KB
/
Publish-BiztalkBtdfApplication.psm1
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#requires -version 4
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Deploys biztalk applications created using Biztalk Deployment framework (BTDF)
.DESCRIPTION
This script deploys a biztalk MSI built using BTDF, through standard BTDF steps as follows
- Takes a backup of the existing version of biztalk app, if any, MSI and bindings into a specified backup dir
- Undeploys the existing version, if any, of biztalk app
- Uninstalls existing version of biztalk app, if any
- Installs the new version of biztalk MSI created using BTDF
- Deploys the new installed version of biztalk using BTDF
This script was tested with Biztalk 2013 R2 and BTDF Release 5.6 (Release Candidate)
.INPUTS
None
.OUTPUTS
None
.LINK
https://biztalkdeployment.codeplex.com/
https://biztalkdeployment.codeplex.com/releases/view/616874
http://thoughtsofmarcus.blogspot.com.au/2010/10/find-all-possible-parameters-for-msi.html
.EXAMPLE
publish-btdfBiztalkApplication -biztalkMsi "C:\mybtdfMsi.msi" -installdir "C:\program files\mybtdfMsi" -biztalkApplicationName DeploymentFramework.Samples.BasicMasterBindings -BtdfProductName "Deployment Framework for BizTalk - BasicMasterBindings" -backupDir c:\mybackupdir -importIntoBiztalkMgmtDb 1 -deployOptions @{"/p:VDIR_USERNAME"="contoso\adam";"/p:VDIR_USERPASS"="@5t7sd";"/p:ENV_SETTINGS"="""c:\program files\mybtdfMsi\Deployment\PortBindings.xml"""}
This installs BTDF Biztalk application MSI C:\mybtdfMsi.msi, into install directory C:\program files\mybtdfMsi. in this example, the custom deploy options, VDIR_USERNAME, VDIR_USERPASS and ENV_SETTINGS are the only deployment options required to deploy the app.
Note how the value of one of the BTDF deploy options, "/p:ENV_SETTINGS", is double quoted twice """c:\program files\mybtdfMsi\Deployment\PortBindings.xml""". Please make sure values with spaces are double quoted twice in the deploy, undeploy and install options hastable
.EXAMPLE
publish-btdfBiztalkApplication -whatif
To run this script with the awesome whatif switch
.EXAMPLE
publish-btdfBiztalkApplication -verbose
To run this script with increased logging use the -verbose switch
.EXAMPLE
publish-btdfBiztalkApplication -msbuildPath "C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild.exe" -btsTaskPath "$env:systemdrive\Program Files (x86)\Microsoft BizTalk Server 2013 R2\BtsTask.exe"
Customises the paths of msbuild and btstask
#>
function Publish-BTDFBiztalkApplication() {
[CmdletBinding(SupportsShouldProcess = $True)]
Param(
# The path of biztalk MSI created using BTDF
[Parameter(Mandatory = $True)]
[string] $biztalkMsi,
# The directory into which the MSI needs to be installed
[Parameter(Mandatory = $True)]
[string]$installdir,
#The name of the BTDF product name as specified in the btdf project file property <ProductName>..</ProductName>.
[Parameter(Mandatory = $True)]
[string] $btdfProductName,
#The name of the biztalk application. This must match the name of the biztalk application the msi creates.
[Parameter(Mandatory = $True)]
[string] $biztalkApplicationName,
#The backup directory into which an existing Biztalk application, if any, will be backed up to
[Parameter(Mandatory = $True)]
$backupDir,
#This option is useful for deploying in clustered biztalk server environments. Set this to false when installing on all servers except the last one within the clustered environment.
[Parameter(Mandatory = $False)]
[boolean] $importIntoBiztalkMgmtDb = $true,
#This is a hash table of key-value pairs of deploy options that the BTDF deploy UI walks you through. This hash table of custom variables must contain all variables specified in the installwizard.xml of your BTDF project, including the port bindings file.
#At a bare minimum you will need to specify the port bindings file. Please makes sure that the values with spaces are quoted correctly, for further details see examples
#Note: There is no need to specify the default variable BT_DEPLOY_MGMT_DB in here, as it is already captured as part of $importIntoBiztalkMgmtDb
[Parameter(Mandatory = $True)]
[hashtable]$deployOptions,
#This is a hash table of key-value pairs of install options. This is the list of public properties available when installing an MSI.
[hashtable]$installOptions = $NULL,
#This is a keyvalue pairs of deploy options. This is a list of key value pairs for all custom variables specified in the uninstallwizard.xml of your BTDF project.
#Note: There is no need to specify the default variable BT_DEPLOY_MGMT_DB in here, as it is already captured as part of $importIntoBiztalkMgmtDb
[hashtable]$undeployOptions = $NULL,
#When set to true uninstalls existing version.
[boolean]$uninstallExistingVersion = $True,
#This is the BtsTaskPath.
[string]$btsTaskPath = "$env:systemdrive\Program Files (x86)\Microsoft BizTalk Server 2016\BtsTask.exe",
#This is the msbuild path.
[string]$msbuildPath = "$env:systemdrive\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe",
#This flag when set to true, undeploys all biztalk applications that are dependent on this app to be able to undeploy this app
[boolean] $undeployDependentApps = $false
)
$ErrorActionPreference = "Stop"
$script:btsTaskPath = $btsTaskPath
$script:loglevel = get-loglevel
try {
Write-verbose "Debug mode in on.. Please note that senstive information such as passwords may be logged in clear text"
if ($uninstallExistingVersion) {
unpublish-btdfbiztalkapplication -btdfProductName $btdfProductName -biztalkApplicationName $biztalkApplicationName -importIntoBiztalkMgmtDb $ImportIntoBiztalkMgmtDb -msbuildPath $msbuildPath -backupdir $backupDir -undeployDependentApps $undeployDependentApps -btsTaskPath $btsTaskPath
}
Write-Host "Step: Installing biztalk msi $BiztalkMsi"
install-btdfBiztalkApp $BiztalkMsi -installDir $installdir -installOptions $installOptions
Write-Host "Step: Deploying biztalk app $btdfProductName"
deploy-btdfBiztalkApp -btdfProductName $btdfProductName -importIntoBiztalkMgmtDb $ImportIntoBiztalkMgmtDb -msbuildExePath $msbuildPath -deployOptionsNameValuePairs $deployOptions
Write-Host "------------------------------------------------------------------"
Write-Host "Completed installing $btdfProductName using MSI $BiztalkMsi"
}
finally {
#do nothing
}
}
<#
.SYNOPSIS
Undeploys biztalk applications created using Biztalk Deployment framework (BTDF)
.DESCRIPTION
This script undeploys a biztalk MSI built using BTDF, through standard BTDF steps as follows
- Takes a backup of the existing version of biztalk app, if any, MSI and bindings into a specified backup dir
- Undeploys the existing version, if any, of biztalk app
- Uninstalls existing version of biztalk app, if any
- If there are other biztalk applications dependent on this app, then setting undeployDependentApps to true undeploys them too after taking a backup
This script was tested with Biztalk 2013 R2 and BTDF Release 5.6 (Release Candidate)
.INPUTS
None
.OUTPUTS
None
.LINK
https://biztalkdeployment.codeplex.com/
https://biztalkdeployment.codeplex.com/releases/view/616874
http://thoughtsofmarcus.blogspot.com.au/2010/10/find-all-possible-parameters-for-msi.html
.EXAMPLE
unpublish-btdfBiztalkApplication -biztalkApplicationName DeploymentFramework.Samples.BasicMasterBindings -BtdfProductName "Deployment Framework for BizTalk - BasicMasterBindings" -backupDir c:\mybackupdir -importIntoBiztalkMgmtDb 1 -undeployDependentApps 1
This uninstalls the BTDF Biztalk application product "Deployment Framework for BizTalk - BasicMasterBindings" with biztalk app name "DeploymentFramework.Samples.BasicMasterBindings". The undeployDependentApps option also undeploys all dependents apps
.EXAMPLE
unpublish-btdfBiztalkApplication -whatif
To run this script with the awesome whatif switch
.EXAMPLE
unpublish-btdfBiztalkApplication -verbose
To run this script with increased logging use the -verbose switch
.EXAMPLE
unpublish-btdfBiztalkApplication -msbuildPath "C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild.exe" -btsTaskPath "$env:systemdrive\Program Files (x86)\Microsoft BizTalk Server 2013 R2\BtsTask.exe"
Customises the paths of msbuild and btstask
#>
function unpublish-btdfbiztalkapplication() {
Param (
#The name of the BTDF product name as specified in the btdf project file property <ProductName>..</ProductName>.
[Parameter(Mandatory = $True)]
[string] $btdfProductName,
#The name of the biztalk application. This must match the name of the biztalk application the msi creates.
[Parameter(Mandatory = $True)]
[string] $biztalkApplicationName,
#The backup directory into which an existing Biztalk application, if any, will be backed up to
[Parameter(Mandatory = $True)]
$backupDir,
#This option is useful for deploying in clustered biztalk server environments. Set this to false when installing on all servers except the last one within the clustered environment.
[Parameter(Mandatory = $False)]
[boolean] $importIntoBiztalkMgmtDb = $true,
#This is a keyvalue pairs of deploy options. This is a list of key value pairs for all custom variables specified in the uninstallwizard.xml of your BTDF project.
#Note: There is no need to specify the default variable BT_DEPLOY_MGMT_DB in here, as it is already captured as part of $importIntoBiztalkMgmtDb
[hashtable]$undeployOptions = $NULL,
#This is the BtsTaskPath.
[string]$btsTaskPath = "$env:systemdrive\Program Files (x86)\Microsoft BizTalk Server 2016\BtsTask.exe",
#This is the msbuild path.
[string]$msbuildPath = "$env:systemdrive\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe",
#This flag when set to true, undeploys all biztalk applications that are dependent on this app to be able to undeploy this app
[boolean] $undeployDependentApps = $false
)
$ErrorActionPreference = "Stop"
$script:btsTaskPath = $btsTaskPath
$script:loglevel = get-loglevel
Write-Host "Step: Umdeploying existing biztalk app $BiztalkBtdfApp"
undeploy-btdfBiztalkApp -biztalkAppName $biztalkApplicationName -btdfProductName $btdfProductName -isFirstBiztalkServer $ImportIntoBiztalkMgmtDb -msbuildExePath $msbuildPath -backupdir $backupDir -undeployDependentApps $undeployDependentApps
Write-Host "Step: Uninstalling existing biztalk app $BiztalkBtdfApp"
uninstall-btdfBiztalkApp $btdfProductName
}
function get-dependentbiztalkapps() {
param(
[Parameter(Mandatory = $True)]
[string] $biztalkAppName,
[Parameter(Mandatory = $false)]
[string] $managmentDbServer = "",
[Parameter(Mandatory = $false)]
[string] $managementDb = "",
#This is the BtsTaskPath.
[string]$btsTaskPath = "$env:systemdrive\Program Files (x86)\Microsoft BizTalk Server 2016\BtsTask.exe"
)
$script:btsTaskPath = $btsTaskPath
#if no sql server details are passed in attempt to get this through btstask
if ([string]::IsNullOrEmpty($managmentDbServer) -or [string]::IsNullOrEmpty($managementDb)) {
$managmentDbServer = get-biztalkManagementServer
$managmentDbServer = $managmentDbServer[0]
$managementDb = $managmentDbServer[1]
}
[System.Collections.ArrayList]$result = [array]$(get-dependentbiztalkappsrecurse $biztalkAppName $managmentDbServer $managementDb)
#The result also contains the biztalk app name who dependents we are looking for..
if ($result.Contains($biztalkAppName) -and $result[$($result.Count - 1)] -eq $biztalkAppName) {
$null = $result.Remove($biztalkAppName)
}
return [array]$result
}
function get-loglevel() {
if ($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) {return 2}
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {return 3}
return 1
}
function get-msbuildloglevel() {
Param(
[Parameter(Mandatory = $True)]
[int]$loglevel
)
if ($loglevel -ge 3) {return "diag"}
if ($loglevel -ge 2) {return "detailed"}
return "normal"
}
function get-msiexecloglevel() {
Param(
[Parameter(Mandatory = $True)]
[int]$loglevel
)
if ($loglevel -ge 3) {return "x"}
if ($loglevel -ge 2) {return "v"}
return "*"
}
function flatten-keyValues() {
Param(
[hashtable]$hashMap = $null
)
$flattendMap = ""
if ($null -eq $hashMap) {
return $flattendMap
}
foreach ($h in $hashMap.GetEnumerator()) {
$flattendMap = $flattendMap + " " + $($h.Name) + "=" + $($h.Value)
}
return $flattendMap
}
function Get-AppUninstallCommand() {
param(
[Parameter(Mandatory = $True)]
[string]$appDisplayName
)
$app = Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\**" | Where-Object {
$_.DisplayName -like "$appDisplayName*"
}
if ($null -eq $app) {
$app = Get-ItemProperty "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\**" | Where-Object {
$_.DisplayName -like "$appDisplayName*"
}
}
#Found a match in the registry
if ($null -ne $app) {
#more than one match for the app uninstall command.. Error case.. this script doesnt support this type of uninstall
if ($app.Count -gt 1) {
Write-Error "Multiple items matched when looking for uninstall command using app name HKLM:\Software\....\Microsoft\Windows\CurrentVersion\Uninstall\$appDisplayName*." $($app | Format-Table -Property DisplayName, PSPath -Wrap | Out-String) "This script does not currently support use cases where mutiple products with the same name prefix are found."
}
return $app.uninstallstring
}
return $null
}
function get-btdfUndeployShortCut() {
param([Parameter(Mandatory = $True)]
[string]$btdfBiztalkAppName
)
#get BTDF shortcuts in the startmenu for the app, regardless of version
$undeployAppBasePath = "$Env:SystemDrive\ProgramData\Microsoft\Windows\Start Menu\Programs\$btdfBiztalkAppName*\undeploy *.lnk"
$undeployShortcut = get-btdfShortCut $undeployAppBasePath
return $undeployShortcut
}
function get-btdfDeployShortCut() {
param([Parameter(Mandatory = $True)]
[string]$btdfBiztalkAppName
)
#get BTDF shortcuts in the startmenu for the app, regardless of version
$deployAppBasePath = "$Env:SystemDrive\ProgramData\Microsoft\Windows\Start Menu\Programs\$btdfBiztalkAppName*\deploy *.lnk"
$deployShortcut = get-btdfShortCut $deployAppBasePath
return $deployShortcut
}
function get-btdfShortCut() {
param([Parameter(Mandatory = $True)]
[string]$shortcutSearchPath
)
#ensure there is exactly one match for the path, else appropriate error or warning
$items = Get-Item $shortcutSearchPath
if ($items.Count -gt 1) {
Write-Error "Multiple items matching $shortcutSearchPath found , $items. Unable to detemine which app needs to be managed!!"
}
elseif ($items.Count -eq 0) {
Write-Warning "No items found using search path $shortcutSearchPath"
}
$undeployShortcut = $items[0]
#Final check to makesure it is a file and not directory!!
if (-not (Test-Path $undeployShortcut -PathType Leaf)) {
Write-Error "Expected shortcut to be a file, found a folder instead!!"
}
return $undeployShortcut
}
function get-btdfProjectFileName() {
param(
[Parameter(Mandatory = $True)]
[string]$btdfDeployOrUndeployShortcutFileName
)
if (-not (Test-Path $btdfDeployOrUndeployShortcutFileName -PathType Leaf)) {
Write-Error "The file $btdfDeployOrUndeployShortcutFileName not found"
}
$shortcutObj = get-shortcutProperties $btdfDeployOrUndeployShortcutFileName
$projectFileRegex = "\s[^:]*\.btdfproj"
if ($shortcutObj.TargetArguments -match $projectFileRegex) {
$projectFile = ([string]$matches[0]).Trim()
}
else {
Write-Error "Could not find any project file matching regex $projectFileRegex in expression $shortcutObj.TargetArguments"
}
return $projectFile
}
function get-shortcutProperties() {
param(
[Parameter(Mandatory = $True)]
[string]$shortCut
)
$shell = $null
try {
$shell = New-Object -ComObject WScript.Shell
$properties = @{
ShortcutName = $shortCut.Name
Target = $shell.CreateShortcut($shortCut).targetpath
StartIn = $shell.CreateShortcut($shortCut).WorkingDirectory
TargetArguments = $shell.CreateShortcut($shortCut).Arguments
}
return New-Object PSObject -Property $Properties
}
finally {
if ($null -eq $shell) {
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
}
}
}
<#
.SYNOPSIS
Installs the new version of biztalk MSI created using BTDF
.DESCRIPTION
This script is used as a step during the deploy script, but can be called individually to be able to run SettingsFileGenerator and replace
settings for the current environment using Octopus variables. After using this step directly, you should call deploy-btdfBiztalkApp to continue the BTDF process.
This script was tested with Biztalk 2016 and BTDF Release 5.7
.INPUTS
None
.OUTPUTS
None
.LINK
https://github.com/eloekset/publish-btdfBiztalkApplication forked from https://github.com/elangovana/publish-btdfBiztalkApplication
.EXAMPLE
install-btdfBiztalkApp -biztalkMsi "C:\mybtdfMsi.msi" -installdir "C:\program files\mybtdfMsi" -installOptions @{<msiexec parameters>}
This installs BTDF Biztalk application MSI C:\mybtdfMsi.msi, into install directory C:\program files\mybtdfMsi.
.EXAMPLE
install-btdfBiztalkApp -whatif
To run this script with the awesome whatif switch
.EXAMPLE
install-btdfBiztalkApp -verbose
To run this script with increased logging use the -verbose switch
#>
function install-btdfBiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
# The path of biztalk MSI created using BTDF
[Parameter(Mandatory = $True)]
[string]$BiztalkAppMSI,
# The directory into which the MSI needs to be installed
[Parameter(Mandatory = $True)]
[string]$installDir,
#This is a hash table of key-value pairs of install options. This is the list of public properties available when installing an MSI.
[hashtable]$installOptions = $null
)
$script:loglevel = get-loglevel
$stdOutLog = Join-Path $([System.IO.Path]::GetTempPath()) $([System.Guid]::NewGuid().ToString())
$additionalInstallProperties = flatten-keyValues $installOptions
try {
$msiloglevel = get-msiexecloglevel $script:loglevel
$args = @("/c msiexec /i ""$BiztalkAppMSI"" /q /l$msiloglevel $stdOutLog INSTALLDIR=""$installDir"" $additionalInstallProperties")
#what if check
if ($pscmdlet.ShouldProcess("$env:computername", "cmd $args")) {
Start-Command "cmd" $args
}
}
catch {
$ErrorMessage = $_.Exception.Message
$msiOutput = Get-Content $stdOutLog | Out-String
Write-Error "$ErrorMessage $msiOutput"
}
$msiOutput = Get-Content $stdOutLog | Out-String
write-host $msiOutput
}
<#
.SYNOPSIS
Deploys an already installed BizTalk application created using BTDF
.DESCRIPTION
This script is used as a step during the deploy script, but can be called individually to be able to run SettingsFileGenerator and replace
settings for the current environment using Octopus variables. Before using this step directly, you must call install-btdfBiztalkApp to install the MSI.
This script was tested with Biztalk 2016 and BTDF Release 5.7
.INPUTS
None
.OUTPUTS
None
.LINK
https://github.com/eloekset/publish-btdfBiztalkApplication forked from https://github.com/elangovana/publish-btdfBiztalkApplication
.EXAMPLE
deploy-btdfBiztalkApp -BtdfProductName "Deployment Framework for BizTalk - BasicMasterBindings" -importIntoBiztalkMgmtDb 1 -deployOptions @{"/p:VDIR_USERNAME"="contoso\adam";"/p:VDIR_USERPASS"="@5t7sd";"/p:ENV_SETTINGS"="""c:\program files\mybtdfMsi\Deployment\PortBindings.xml"""}
This installs BTDF Biztalk application MSI C:\mybtdfMsi.msi, into install directory C:\program files\mybtdfMsi. in this example, the custom deploy options, VDIR_USERNAME, VDIR_USERPASS and ENV_SETTINGS are the only deployment options required to deploy the app.
Note how the value of one of the BTDF deploy options, "/p:ENV_SETTINGS", is double quoted twice """c:\program files\mybtdfMsi\Deployment\PortBindings.xml""". Please make sure values with spaces are double quoted twice in the deploy, undeploy and install options hastable
.EXAMPLE
deploy-btdfBiztalkApp -whatif
To run this script with the awesome whatif switch
.EXAMPLE
deploy-btdfBiztalkApp -verbose
To run this script with increased logging use the -verbose switch
#>
function deploy-btdfBiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
# The name of the BTDF product name as specified in the btdf project file property <ProductName>..</ProductName>.
[Parameter(Mandatory = $True)]
[string]$btdfProductName,
# This option is useful for deploying in clustered biztalk server environments. Set this to false when installing on all servers except the last one within the clustered environment.
[Parameter(Mandatory = $True)]
[boolean]$importIntoBiztalkMgmtDb,
# This is the msbuild path.
[Parameter(Mandatory = $False)]
[string]$msbuildExePath = "$env:systemdrive\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe",
# This is a hash table of key-value pairs of deploy options that the BTDF deploy UI walks you through. This hash table of custom variables must contain all variables specified in the installwizard.xml of your BTDF project, including the port bindings file.
# At a bare minimum you will need to specify the port bindings file. Please make sure that the values with spaces are quoted correctly, for further details see examples.
# Note: There is no need to specify the default variable BT_DEPLOY_MGMT_DB in here, as it is already captured as part of $isLastBiztalkServer.
[hashtable]$deployOptionsNameValuePairs = $null
)
Write-Host "********Deploying biztalk app $btdfProductName ......."
try {
$appUninstallCmd = Get-AppUninstallCommand $btdfProductName
#extra check for whatif, when running without any version of the biztalk app installed
if (-not($pscmdlet.ShouldProcess("$env:computername", "deploy-btdfBiztalkApp"))) {
return
}
if ($null -eq $appUninstallCmd) {
Write-Error "No version of $btdfProductName found. Please ensure this app is installed first"
}
$deployShortCut = get-btdfdeployShortcut $btdfProductName
Write-Host "Found shortcut for deploying app $deployShortCut"
$projectFile = get-btdfProjectFileName $deployShortCut
$installStartInDir = $(get-shortcutProperties $deployShortCut).StartIn
$addtionalDeployOptions = Flatten-KeyValues $deployOptionsNameValuePairs
$stdErrorLog = [System.IO.Path]::GetTempFileName()
$msbuildloglevel = get-msbuildloglevel $script:loglevel
$arg = @([System.String]::Format("/c @echo on & cd /d ""{0}"" & ""{1}"" /p:Interactive=False /t:Deploy /clp:NoSummary /nologo /tv:4.0 {2} /v:{5} /p:DeployBizTalkMgmtDB={3} /p:Configuration=Server {4}", $installStartInDir, $msbuildExePath, $projectFile, $importIntoBiztalkMgmtDb, $addtionalDeployOptions, $msbuildloglevel))
Start-Command "cmd" $arg
Write-Host "Application $btdfBiztalkAppName deployed"
}
finally {
# do nothing
}
}
function undeploy-DependentBiztalkApps() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$biztalkAppName,
[Parameter(Mandatory = $True)]
[boolean]$isFirstBiztalkServer,
[Parameter(Mandatory = $True)]
[string]$backupdir
)
Write-Host "............Undeploying dependent apps for $biztalkAppName ......."
try {
if (-not $isFirstBiztalkServer) {
Write-Host "Not first biztalk server, no dependent apps to check. Exiting..."
return
}
#check if biztalk app exists, els do nothing and return
if (-not(test-biztalkAppExists $biztalkAppName)) {
Write-Host "The application $biztalkAppName does not exist on the biztalk sever. Nothing to undeploy"
return
}
$mgmtServerDb = get-biztalkManagementServer
[array]$dependentAppsToUndeploy = [array]$(get-dependentbiztalkapps $biztalkAppName $mgmtServerDb[0] $mgmtServerDb[1])
if ($null -eq $dependentAppsToUndeploy -or $dependentAppsToUndeploy.Count -eq 0) {
Write-Host "No dependant apps to undeploy.. exiting"
return
}
if (Test-MessagBoxInstances $dependentAppsToUndeploy $mgmtServerDb[0] $mgmtServerDb[1]) {
Write-Error "There are active instances associated with one or more applications in $dependentAppsToUndeploy.."
}
Write-Host "Found dependent apps that must be undeployed.. $dependentAppsToUndeploy"
Write-Host $($dependentAppsToUndeploy | Out-String)
foreach ($app in $dependentAppsToUndeploy) {
Write-Verbose "stopping dependent app $appToUndeploy"
stop-biztalkapplication $app $isFirstBiztalkServer $mgmtServerDb[0] $mgmtServerDb[1]
}
#just do one more check before backing up and removing apps
if (Test-MessagBoxInstances $dependentAppsToUndeploy $mgmtServerDb[0] $mgmtServerDb[1]) {
Write-Error "One or more dependent applications cannot be undeployed. There are active instances associated with one or more applications in $dependentAppsToUndeploy.."
}
# Make sure all backs up are done before removing apps
foreach ($appToUndeploy in $dependentAppsToUndeploy) {
#Take a backup of biztalk app before undeploying...
Write-Verbose "Backing up $appToUndeploy to $backupdir"
backup-BiztalkApp $appToUndeploy $backupdir
}
#remove apps
foreach ($appToUndeploy in $dependentAppsToUndeploy) {
#Take a backup of biztalk app before undeploying...
Write-Verbose "Removing dependent app $appToUndeploy"
Remove-BiztalkApp $appToUndeploy
}
}
finally {
# do nothing
}
}
function undeploy-btdfBiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$biztalkAppName,
[Parameter(Mandatory = $True)]
[string]$btdfProductName,
[Parameter(Mandatory = $True)]
[boolean]$isFirstBiztalkServer,
[Parameter(Mandatory = $True)]
[string]$backupdir,
[Parameter(Mandatory = $True)]
[string]$msbuildExePath,
[hashtable]$undeployOptionsNameValuePairs = $null,
[boolean]$undeployDependentApps = $false
)
Write-Host "********Undeploying $btdfProductName ......."
try {
#check if biztalk app exists, els do nothing and return
if (-not(test-biztalkAppExists $biztalkAppName)) {
Write-Host "The application $biztalkAppName does not exist on the biztalk sever. Nothing to undeploy"
return
}
$mgmtServerDb = get-biztalkManagementServer
#getDependant applications
[array]$dependantApps = [array]$(get-dependentbiztalkapps $biztalkAppName $mgmtServerDb[0] $mgmtServerDb[1])
$tmpAppsToCheckActiveInstances = $dependantApps + @($biztalkAppName)
if (Test-MessagBoxInstances $tmpAppsToCheckActiveInstances $mgmtServerDb[0] $mgmtServerDb[1]) {
Write-Error "One or more dependent applications cannot be undeployed. There are active instances associated with one or more applications in $dependentAppsToUndeploy.."
}
# all seems ok,, stop application..
stop-biztalkapplication $biztalkAppName $isFirstBiztalkServer $mgmtServerDb[0] $mgmtServerDb[1]
#if forced undeploy, then undeploy dependents apps
if ($undeployDependentApps) {
undeploy-DependentBiztalkApps $biztalkAppName $isFirstBiztalkServer $backupdir
}
elseif ($null -ne $dependantApps) {
Write-Verbose "Dependent apps $dependantApps"
if (($dependantApps -is [array] -and $dependantApps.Count -gt 0) -or ($dependantApps -is [Int32] -and $dependantApps -gt 0)) {
Write-Error "The biztalk application $biztalkAppName cannot be undeployed as there are other applications that depend on it. To undeploy dependent applications, set the undeployDependentApps option to true. Or manually remove the apps $dependantApps"
}
}
#Take a backup of biztalk app before undeploying...
backup-BiztalkApp $biztalkAppName $backupdir
#Check if biztalk app can be undeployed using BTDF undeploy. If BTDF undeploy not found, undeploy using BTSTask.exe
$appUninstallCmd = Get-AppUninstallCommand $btdfProductName
if ($null -eq $appUninstallCmd) {
Write-Host "No older version of $btdfProductName found. Nothing to undeploy"
#BTDF undeploy not found, undeploy using BTSTask.exe
if (test-biztalkAppExists $biztalkAppName) {
#remove app only if firstbiztalk server
if ($isFirstBiztalkServer) {
Write-Warning "No Btdf command to undeploy this product $btdfProductName exists, but the biztalk application $biztalkAppName exists.. Using Btstask instead to remove app $biztalkAppName..."
Remove-BiztalkApp $biztalkAppName
}
}
return
}
#undeploy using btdf undeploy
$undeployShortCut = get-btdfUndeployShortcut $btdfProductName
Write-Host Found shortcut for undeploying app $undeployShortCut
$installDirStartIn = $(get-shortcutProperties $undeployShortCut).StartIn
$projectFile = get-btdfProjectFileName $undeployShortCut
$addtionalunDeployOptions = flatten-keyValues $undeployOptionsNameValuePairs
$msbuildloglevel = get-msbuildloglevel $script:loglevel
$stdErrorLog = Join-Path $([System.IO.Path]::GetTempPath()) $([System.Guid]::NewGuid().ToString())
$arg = @([System.String]::Format("/c @echo on & cd /d ""{0}"" & ""{1}"" /p:Interactive=False /p:ContinueOnError=FALSE /t:Undeploy /clp:NoSummary /nologo /verbosity:{5} /tv:4.0 {2} /p:DeployBizTalkMgmtDB={3} /p:Configuration=Server {4}", $installDirStartIn, $msbuildExePath, $projectFile, $isFirstBiztalkServer, $addtionalunDeployOptions, $msbuildloglevel))
#what if check
if ($pscmdlet.ShouldProcess("$env:computername", "cmd $arg")) {
Start-Command "cmd" $arg
}
Write-Host "Application $biztalkAppName undeployed"
}
finally {
# do nothing
}
}
function stop-biztalkapplication() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$biztalkAppName,
[Parameter(Mandatory = $True)]
[boolean]$IsFirstBiztalkServer,
[Parameter(Mandatory = $True)]
[string]$managmentDbServer,
[string]$managementdb = "BizTalkMgmtDb"
)
#=== Make sure the ExplorerOM assembly is loaded ===#
#Do nothing if not the first biztalk server
if (-not $IsFirstBiztalkServer) {
return
}
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$Catalog.ConnectionString = "SERVER=$managmentDbServer;DATABASE=$managementdb;Integrated Security=SSPI"
#=== Connect the BizTalk Management database ===#
foreach ($app in $Catalog.Applications) {
if ($($app.Name) -ieq $biztalkAppName) {
Write-Host Issuing stop command to $biztalkAppName..
#What if support
if ($pscmdlet.ShouldProcess("$managmentDbServer\\$managementdb\\$biztalkAppName", "StopAll")) {
$app.Stop([Microsoft.BizTalk.ExplorerOM.ApplicationStopOption] "StopAll")
$Catalog.SaveChanges()
}
} #end of application match check
}
}
function uninstall-btdfBiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$btdfProductName
)
Write-Host "******** Uninstalling biztalk app $btdfProductName ......."
try {
#Get command to uninstall
$appUninstallCmd = Get-AppUninstallCommand $btdfProductName
if ($null -eq $appUninstallCmd) {
Write-Host "No older version of $btdfProductName found. Nothing to uninstall"
return
}
$appUninstallCmd = [string]$appUninstallCmd
Write-Host uninstalling $appUninstallCmd
#use msi exec to remove using msi
$index = $appUninstallCmd.IndexOf("msiexec.exe", [System.StringComparison]::InvariantCultureIgnoreCase)
if ($index -gt -1) {
$msiUninstallCmd = $appUninstallCmd.Substring( $index)
#what if check
if ($pscmdlet.ShouldProcess("$env:computername", "$msiUninstallCmd")) {
Start-Command "cmd" "/c $msiUninstallCmd /quiet"
}
}
else {
Write-Error "Unable to find msiexec.exe uninstall command from the registry..."
}
Write-Host "Application $btdfProductName uninstalled"
}
finally {
# do nothing
}
}
function Test-MessagBoxInstances() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[array]$biztalkApplications,
[Parameter(Mandatory = $True)]
[string]$biztalkMgmtBoxServer,
[string]$biztalkMgmtBoxDb = "BizTalkMgmtDb"
)
Add-Type -AssemblyName ('Microsoft.BizTalk.Operations, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL')
$bo = New-Object Microsoft.BizTalk.Operations.BizTalkOperations $biztalkMgmtBoxServer, $biztalkMgmtBoxDb
$tmpServiceInstances = $bo.GetServiceInstances()
[System.Collections.ArrayList]$serviceInstances = @()
foreach ($instance in $tmpServiceInstances) {
$serviceInstances.Add($instance)
}
[array]$activeInstances = $serviceInstances | Where-Object {$biztalkApplications.Contains($_.Application) -and $_.Messages.Count -gt 0} | Group-Object Application, InstanceStatus, ServiceType | Select-Object Name, Count
Write-Host "Active applications $($activeInstances.Length), service instances: " ($activeInstances | Format-Table -auto | Out-String)
return $($activeInstances.Count -gt 0)
}
function backup-BiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$BiztalkAppName,
[Parameter(Mandatory = $True)]
[string]$backupdir
)
Write-Host "..........Backing up biztalk app $BiztalkAppName to $backupdir ......."
try {
if (-not( test-biztalkAppExists $BiztalkAppName)) {
Write-Host "$BiztalkAppName doesnt not exist. Nothing to backup"
return
}
$templateFileName = [system.string]::Format("{0}_{1}", $BiztalkAppName, $(Get-Date -Format yyyyMMddHHmmss))
$packageMsiPath = Join-Path $backupdir ([system.string]::Format("{0}{1}", $templateFileName, ".msi"))
$packageBindingsPath = Join-Path $backupdir ([system.string]::Format("{0}{1}", $templateFileName, ".xml"))
#use bts task to export app MSI
$exportMsiCmd = @([System.String]::Format("/c echo Exporting biztalk MSI using btsTask.. & ""{0}"" exportapp /ApplicationName:""{1}"" /Package:""{2}""", $BtsTaskPath, $BiztalkAppName, $packageMsiPath))
#whatif
if ($pscmdlet.ShouldProcess("$env:computername", "cmd $exportMsiCmd")) {
Start-Command "cmd" $exportMsiCmd
}
#use bts task to export app bindings
$exportBindingsCmd = @([System.String]::Format("/c echo Exporting biztalk bindings using btsTask..& ""{0}"" exportBindings /ApplicationName:""{1}"" /Destination:""{2}""", $BtsTaskPath, $BiztalkAppName, $packageBindingsPath))
#whatif
if ($pscmdlet.ShouldProcess("$env:computername", "cmd $exportBindingsCmd")) {
Start-Command "cmd" "$exportBindingsCmd"
}
Write-Host "Completed backing up $BiztalkAppName"
}
finally {
# do nothing
}
}
function Remove-BiztalkApp() {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $True)]
[string]$BiztalkAppName
)
Write-Host ".........Removing biztalk app $BiztalkAppName ......."
try {
#if app does not exist, nothing to do..
if (-not(test-biztalkAppExists $BiztalkAppName)) {
Write-Host "$BiztalkAppName doesnt not exist. Nothing to remove"
return
}
#use bts task to remove app
$removeAppCmd = @([System.String]::Format("/c echo Removing biztalk app using btstask... & ""{0}"" removeapp /ApplicationName:""{1}"" ", $BtsTaskPath, $BiztalkAppName))
if ($pscmdlet.ShouldProcess("$env:computername", "$removeAppCmd")) {
Start-Command "cmd" $removeAppCmd
}
Write-Host "Completed removing $BiztalkAppName using btstask"
}
finally {
# do nothing
}
}
function get-biztalkManagementServer() {
param(
[string]$BiztalkTaskPath = $btsTaskPath
)
Write-Host "Get Biztallk Management server"
$exportedSettingsFile = Join-Path $([System.IO.Path]::GetTempPath()) $([System.Guid]::NewGuid().ToString() + ".xml")
$exportBiztalkSettingsCmd = [System.String]::Format("/c echo Getting biztalk settings using BTSTask & ""{0}"" exportsettings -Destination:""{1}""", $BiztalkTaskPath, $exportedSettingsFile)
Start-Command "cmd" $exportBiztalkSettingsCmd
[xml]$XmlDocument = Get-Content -Path $exportedSettingsFile
[string]$server = $XmlDocument.Settings.ExportedGroup
Write-Host "Exported group $server"
return $server.Split(":")
}
function test-biztalkAppExists() {
param(
[Parameter(Mandatory = $True)]
[string]$BiztalkAppName
)
Write-Host "Checking if biztalk app $BiztalkAppName exists......."
try {
#use bts task to list apps
$stdOutLog = Join-Path $([System.IO.Path]::GetTempPath()) $([System.Guid]::NewGuid().ToString())
$ListBiztalkAppCmd = [System.String]::Format("/c echo & ""{0}"" ListApps > ""{1}""", $BtsTaskPath, $stdOutLog)
Start-Command "cmd" $ListBiztalkAppCmd
$biztalkAppslist = Get-Content $stdOutLog | Out-String
$appNameRegex = "-ApplicationName=""$BiztalkAppName"""
$appExists = $biztalkAppslist -match $appNameRegex
return $appExists
}
finally {
# do nothing
}
}
<#
.SYNOPSIS
Runs Start-Process with a bit of logging and error handling
.DESCRIPTION
This script is called several places internally, but is exported for ruse directly from Octopus.
.INPUTS
None
.OUTPUTS
None
.LINK
https://github.com/eloekset/publish-btdfBiztalkApplication forked from https://github.com/elangovana/publish-btdfBiztalkApplication
.EXAMPLE
Start-Command -commandToStart "msiexec.exe" -arguments "/i C:\mybtdfMsi.msi"
This installs BTDF Biztalk application MSI C:\mybtdfMsi.msi.
.EXAMPLE