BizTalk 2010+ – Powershell Script using Powershell Extensions to Create Hosts and HostInstances

I’m at a new client, and here’s the scenario.  They have good Hosts and Host Instances set up in production, and maybe the QA environment (I don’t have access yet).  But in the development environment, they have been using the default host instances.  I still can’t imagine how they did their binding files correctly.  One of my first goals is to get them to use the BizTalk Deployment Framework.  The second goal is to make the host instances the same in the development environments (and also on the VMs my co-worker and I are running).

So as soon as they give me a list of the Production Host-Instances I want to re-create them in the 4 Dev Environments.  Obviously, a script is needed. The script below does the job for the Hosts and Host Instances.  What I have left to do is to script the setup of the adapters that should be associated with the Host Instances.

I recently read this blog by Sandro Pereira: https://sandroaspbiztalkblog.wordpress.com/2013/09/05/powershell-to-configure-biztalk-server-host-and-host-instances-according-to-some-of-the-best-practices/  I’m not sure why he called WMI directly, maybe he wrote that before the Powershell Extensions became available on CodePlex: http://psbiztalk.codeplex.com  I had some errors getting it to work.  Further, his logic goes and builds the “best practices”.  I’m not yet ready or able to change the clients practices in Production.  (It’s funny how some clients give you the “keys to the kingdom” on the first day, and others keep you in more of a “pen”.  It depends on the size of the company, their security and deployment practices, whether or not you have to support and fix production issues, and what exactly you were hired to do.

The Script below, as short as it is, took me several hours to get working. The documentation link on the Powershell Extensions was broken on CodePlex, so I could not download any documentation, so it was sort of a trial and error approach. What I love, and borrowed from Sandro Pereira’s code above, is the ability of Powershell to prompt for the password, and save it in a $credentials object; then you can pass that when you set up the Host-Instances (i.e. the user/pass for the service name that runs the Host-Instances.)

NOTE 1: I have not tested this in an environment where there are more than one BizTalk server in the group.  In that case, code changes would have to be made to add one Host-Instance per server.

NOTE 2: In BizTalk 2013, Powershell Extensions are included with the install, you don’t need the CodePlex extensions.  http://www.quicklearn.com/blog/2013/07/19/automating-and-managing-biztalk-server-2013-with-powershell/

cls
Add-PSSnapIn -Name BiztalkFactory.PowerShell.Extensions  #NOTE: Must be in 32-bit version of Powershellto use this SnapIn
#get-PsSnapIn -registered   ### list registered Snap-In's

function AddHostAndHostInstance($HostName, $Is32BitOnly, $IsTrackingOn, $HostType)
{
 if ($HostType -eq "Isolated") 
   {
     $HostTypeCode = 2
   }
   else 
   {
     $HostTypeCode = 1 
   }
 #$HostType = 1 # 1 = InProcess assuming 2 = ISO 
 $myNTHostGroupName = "mscwvbizd01\BizTalk Application Users"
 $AuthTrusted = $false
 $serverName = "mscwvbizd01"
 #$HostItemType = 1 #NOTE: I got an error when I tried using the -ItemType on the New-Item of the Host-Instance 
 #(fortunately it is defaulting to In-Process) 

 cd "Biztalk:\Platform Settings\Hosts"
 Write-Host "Try to add New Host=$hostName"
 #New-Item $HostName -HostType:$HostType -NtGroupName:$NTGroupName -AuthTrusted:$AuthTrusted
 $temp = New-Item -path $hostName -HostType:$HostTypeCode -NtGroupName:$myNTHostGroupName -AuthTrusted:$AuthTrusted 
 Set-ItemProperty -Path $hostName -Name 'Is32BitOnly' -Value $Is32BitOnly
 Set-ItemProperty -Path $hostName -Name 'HostTracking' -Value $IsTrackingOn
 dir

 cd "BizTalk:\Platform Settings\Host Instances"
 Write-Host "Try to add New HostInstance=$hostName"
 dir
 New-Item $hostName -HostName $hostName -Credentials $hostCredentials -RunningServer $serverName
 #dir
}
### MAIN CODE HERE - Calls Function Above once per Host/Host-Instance ### 

$domainName = "biztalkdev"
$serviceUserid = "biztalkservice"

$hostCredentials = $Host.ui.PromptForCredential("Logon Credentials","This account must have SQL Server permissions.", $domainName + "\" + $serviceUserid, "");
#[String]$hostCredentialsPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($hostCredentials.Password));

# pass three parms: first host name, is32Bit, IsTracking 
AddHostAndHostInstance "TrackingHost"  "True" "True" "In-Process"
AddHostAndHostInstance "AS2Receive"    "True" "True" "Isolated"
AddHostAndHostInstance "TestNewHost32" "True" "False" "In-Process" 
AddHostAndHostInstance "TestNewHost64" "False" "False" "Isolated" 


Write-Host “Script Completed”

Here’s one more library of functions that a co-worker of mine used at a previous client. I didn’t learn about it until after I wrote the above:
https://github.com/lantrix/BTS/blob/master/build/2013/ConfigureBizTalkServer2013EnvHostAndHostInstances.ps1

Update: 05/30/2017 – above script was added to include the line to set the Is32BitFlag so you can make the Host 64-bit or 32-bit using the Powershell extensions.

Update: 05/31/2017 – added the ability to create Isolated hosts by passing a fourth parameter

Update 05/31/2017 – now I also have a script to Update Adapters with Host Names. This was modeled from code here.

Uncategorized  

Leave a Reply