Getting BizTalk information with PowerShell

As I keep venturing into Biztalk, here’s how you can start using PowerShell to access your BizTalk objects with just a few lines of code and (for now) using one of the BizTalk .NET assemblies that’s loaded during the installation.

In the following example, I will create a PowerShell .NET object containing information about my BizTalk Application Artifacts. Please, pay attention to these sample code. There’s lots of information in it.

Open a PowerShell Console prompt, and type (or copy/paste) the following code:

[sourcecode language=”powershell”]

## – Load the following Assembly:
[System.Reflection.Assembly]::loadwithPartialName("Microsoft.BizTalk.ExplorerOM");

## – Create a new empty variable to stored the .NET information:
$BTSexp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer;

## – Now, this line will connect to your Biztalk Local instance:
$BTSexp.ConnectionString = "Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";

[/sourcecode]

Now, keep in mind, you need to execute this block of code in your BizTalk server box, or use PowerShell Remoting feature. To look into the newly created object ‘$BTSexp’, use the “Get-Member” command to expose all the .NET object stored in it. You’ll notice both Properties, and Methods:

[sourcecode language=”powershell”]

$BTSexp | Get-member;

[/sourcecode]

As you can see, few line of code,  you got a lot of information about you BizTalk box. Try the following lines to list all our send box:

[sourcecode language=”powershell”]

$BTSexp.Sendports

[/sourcecode]

This line will display all properties and its content on the screen. Now, using the “Get-Member” (or “GM”) command, you can select from these list some of the properties you want to display on screen:

[sourcecode language=”powershell”]
## – List all Biztalk .NET object properties and methods:
$BTSexp.SendPorts | gm;

## – Display some of the selected properties:
$BTSexp.SendPorts | Select Application, name, Status | format-table -auto;

## – The End
[/sourcecode]

Oops!! Notice our result is not exactly correct. Our ‘Application’ values are showing with a long .NET Namespace, and our status column got truncated from the list. The ‘Application’ property is another .NET object that contains more information, and is only accessible if you use the ‘Foreach’ loop condition to get to it.

To fix this oneliner, we need to create a loop block so we can go deep into the .NET object of ‘Application’ and extract that information. The example below I added an “if” statement to search for all SendPorts containing the ‘TransportType’ for FTP’s:

[sourcecode language=”powershell”]

## – Load the following Assembly:
[System.Reflection.Assembly]::loadwithPartialName("Microsoft.BizTalk.ExplorerOM");

## – Create a new empty variable to stored the .NET information:
$BTSexp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer;

## – Now, this line will connect to your Biztalk Local instance:
$BTSexp.ConnectionString = `
"Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";
## – Looping through each item in the SendPorts object and display
## – it in the PowerShell console:
$y = 0;
foreach($item in ($BTSexp.SendPorts))
{
if($item.PrimaryTransport -ne $null)
{
if($item.PrimaryTransport.TransportType.Name -match "FTP|Nsoftware SFTP")
{
Write-host "[$($y.ToString("000"))]`t$($item.Application.name)`t$($item.PrimaryTransport.TransportType.Name)`t$($item.Name)";
$y++;
}
}
};

[/sourcecode]

Well, we are getting better now but we can make it look nicer. Adding a few more lines we change the code to store the values generated into a PowerShell PSObject. Finally, we can generate a better formatted result:

[sourcecode language=”powershell”]

## – Load the following Assembly:
[System.Reflection.Assembly]::loadwithPartialName("Microsoft.BizTalk.ExplorerOM");

## – Create a new empty variable to stored the .NET information:
$BTSexp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer;

## – Now, this line will connect to your Biztalk Local instance:
$BTSexp.ConnectionString = `
"Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";

## – Initialize variables:
$y = 0; [Array] $MyNewObject = $null;

#Add each result into our New object:
$MyNewObject = foreach($item in ($BTSexp.SendPorts))
{
if($item.PrimaryTransport -ne $null)
{
if($item.PrimaryTransport.TransportType.Name -match "FTP")
{
#Building your PowerShell PSObject item:
$newPSitem = New-Object PSObject -Property @{
seq = $y.ToString("000");
Application = $item.Application.name;
PortType = $item.PrimaryTransport.TransportType.Name;
Port = $item.Name;
};
#To display PSObject item values while processing:
$newPSitem;
$y++;
}
}
};
$MyNewObject | Select seq,Application,PortType,Port | ft -auto;

[/sourcecode]

Now, we’ve created a new PowerShell object with our selected properties, and with a new output that looks Great!. Just Try It in your box!!

The above script will list all FTP related ports. Here’s another the same code PowerShell script for listing all SendPorts on your BizTalk Server:

[sourcecode language=”powershell”]

## – Load the following Assembly:
[System.Reflection.Assembly]::loadwithPartialName("Microsoft.BizTalk.ExplorerOM");

## – Create a new empty variable to stored the .NET information:
$BTSexp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer;

## – Now, this line will connect to your Biztalk Local instance:
$BTSexp.ConnectionString = `
"Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";

## – Initialize variables:
$y = 0; [Array] $MyNewObject = $null;

## – Initialize variables:
$y = 0; [Array] $MyNewObject = $null;

#Add each result into our New object:
$MyNewObject = foreach($item in ($BTSexp.SendPorts))
{
#Building your PowerShell PSObject item:
$newPSitem = New-Object PSObject -Property @{
seq = $y.ToString("000");
Application = $item.Application.name;
PortType = $item.PrimaryTransport.TransportType.Name;
Port = $item.Name;
};
#To display PSObject item values while processing:
$newPSitem;
$y++;
};
$MyNewObject | Select seq,Application,PortType,Port | ft -auto;

[/sourcecode]

Now, you can get a list of all your sendPorts. Don’t be afraid to experiment. You are only querying BizTalk objects using PowerShell.

My next blog will show this query use to list all ReceivePorts.

Happy PowerShelling!!