How to uninstall a BizTalk application
It can be handy as part of a BizTalk application deployment to remove an old or newer version there. This assumes you use the BTDF for deploying an application.
When you use BTDF, it installs the windows menu, a selectable option to un-deploy the application.
The script below simulates stopping the BizTalk application and then un-deploying the application from the server.
Note: Need to run all these in 32 bit PowerShell. Why?? This is due to BizTalk PowerShell requiring 32 bit to access its features correctly.
Because of this requirement, we need to wrap the script in a code block. More info about how to do that here.
The following assumptions are made in the script below:
  • There is a fixed installation path.
  • BizTalk 2016 is being used.
  • BizTalk is installed on either C or D drive.
1
param (
2
[string] $ApplicationName,
3
[string] $BiztalkSQlServer = ".",
4
[string] $DeployDb,
5
[string] $msBuildPath
6
)
7
8
# All functionality in this file is in the code block (a bit unusual, but needed so we can call it in 32 bit powershell)
9
$powershell32bitCode = {
10
param (
11
[string] $ApplicationName,
12
[string] $ApplicationVersion,
13
[string] $BiztalkSQlServer = ".",
14
[string] $DeployDb,
15
[string] $msBuildPath
16
)
17
18
[System.Version]$compareVersion = $ApplicationVersion
19
20
Write-Host "Checking current application '$ApplicationName' v$ApplicationVersion."
21
# search to see if there are any versions already installed on the target machine
22
$existingApps = Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object DisplayName -like "$ApplicationName *" | select DisplayName, DisplayVersion
23
24
if ($existingApps -ne $null)
25
{
26
Write-Host "Have found $ApplicationName already installed."
27
foreach ($app in $existingApps)
28
{
29
Write-Host "Checking existing: '$($app.DisplayName)'. Version: $($app.DisplayVersion)..."
30
[System.Version]$version = $app.DisplayVersion
31
if ($version -gt $compareVersion)
32
{
33
Write-Host "Newer version of '$ApplicationName' have been detected. Attempting to undeploy."
34
35
Write-Host "Loading BizTalk PowerShell..."
36
# Load Biztalk PowerShell
37
if ((Get-Module -Name "BizTalkFactory.PowerShell.Extensions") -eq $null)
38
{
39
Write-Host "Initialising BizTalkFactory.PowerShell.Extensions..."
40
$InitializeDefaultBTSDrive = $false
41
# check the two locations that BizTalk 2016 could be installed
42
if (Test-Path "D:\Program Files (x86)\Microsoft BizTalk Server 2016\SDK\Utilities\PowerShell\BizTalkFactory.PowerShell.Extensions.dll" -PathType Leaf)
43
{
44
$BizTalkPSDll = "D:\Program Files (x86)\Microsoft BizTalk Server 2016\SDK\Utilities\PowerShell\BizTalkFactory.PowerShell.Extensions.dll"
45
}
46
else
47
{
48
$BizTalkPSDll = "C:\Program Files (x86)\Microsoft BizTalk Server 2016\SDK\Utilities\PowerShell\BizTalkFactory.PowerShell.Extensions.dll"
49
}
50
Import-Module $BizTalkPSDll -DisableNameChecking
51
}
52
53
# Ensure no drive exists already
54
If (Test-Path BizTalk:)
55
{
56
Remove-PSDrive -Name "BizTalk"
57
}
58
59
# create the PS-Drive, get the application names and remove the ps drive
60
# PsDrive Name cannot contain special chars like dot. Root must begin with Name. Using generic Name of "BizTalk"
61
$output = New-PSDrive -Name "BizTalk" -PSProvider BizTalk -Root "BizTalk:\" -Instance $BiztalkSQlServer -Database BizTalkMgmtDb -Scope Global
62
63
if ($output -eq $null)
64
{
65
Write-Error "Unable to create the PS Drive to query the Biztalk environment. Check permissions and networks to ensure you have access from this PC to $BiztalkSQlServer ." -ErrorAction Stop
66
return
67
}
68
69
Write-Host "Searching for BizTalk application $ApplicationName..."
70
$app = Get-ChildItem -Path 'BizTalk:\Applications' | Where-Object Name -eq $ApplicationName
71
# Stop the application
72
if ($app -eq $null)
73
{
74
Write-Error "Unable to find a BizTalk applications with name '$ApplicationName'. Check the configuration and try again." -ErrorAction Stop
75
}
76
if ($app.Status -ne "Stopped")
77
{
78
Write-Host "Stopping $ApplicationName app..."
79
$app | Stop-Application -StopOption StopAll
80
}
81
else
82
{
83
Write-Host "BizTalk application $ApplicationName is already stopped."
84
}
85
86
# uninstall the application
87
$appInstallPath = "C:\Program Files (x86)\$ApplicationName for BizTalk\1.0"
88
89
if ((Test-Path $appInstallPath -PathType Container) -eq $false)
90
{
91
Write-Error "Unable to find folder $appInstallPath to uninstall the application." -ErrorAction Stop
92
}
93
94
$projFile = Join-Path -Path $appInstallPath -ChildPath "\Deployment\Deployment.btdfproj"
95
$undeployArgs = @("`"$projFile`"", "/p:DeployBizTalkMgmtDB=$DeployDb;Configuration=Server;InstallDir=`"$appInstallPath`"", "/target:Undeploy")
96
97
Write-Host "For undeployment running: $msBuildPath $undeployArgs"
98
& $msBuildPath $undeployArgs
99
100
# uninstall the application from the control panel
101
$uninstallString = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object DisplayName -eq "$ApplicationName for BizTalk" | Select-Object -ExpandProperty UninstallString
102
if ($uninstallString -eq $null)
103
{
104
Write-Error "Unable to find the Application $ApplicatioName in the Control Panel Add/Remove programs..." -ErrorAction Stop
105
}
106
$uninstallCode = ($uninstallString -split ' ')[1]
107
$args = @($uninstallCode, "/quiet")
108
109
& "msiexec.exe" $args
110
Write-Host "Uninstall of $ApplicationName complete."
111
}
112
else
113
{
114
Write-Host "No later versions of '$ApplicationName' have been detected. Continuing the installation."
115
}
116
}
117
}
118
else
119
{
120
Write-Host "No previous installations of '$ApplicationName' have been detected. Continuing the installation."
121
}
122
123
}
124
# ------------------------------------------------------------------------------------------------------------------------------------------------
125
# MAIN
126
# ------------------------------------------------------------------------------------------------------------------------------------------------
127
# It will run all the code above in a PowerShell 32 bit session. This is required for the Biztalk PowerShell
128
Invoke-Command -ScriptBlock $powershell32bitCode -ArgumentList @($ApplicationName, $ApplicationVersion, $BiztalkSQlServer, $DeployDb, $msBuildPath) -ConfigurationName microsoft.powershell32 -ComputerName $env:COMPUTERNAME
Copied!
Copy link