Time saving PowerShell Snippets for BizTalk Part 1

Some of the beauty of using PowerShell is the ability to rapidly do things that would take a long time to do in .NET code. But you can do this without opening Visual Studio. I have found many uses for PowerShell in working with BizTalk. One area it comes in handy is working with BizTalk binding files. I know you can just use the ExplorerOM but this is painful due to its 32-bit limitations. A frequent pain-point in deploying across environments is needing to create external artifacts for supporting your BizTalk ports.

So for example you use MSMQ and need to create all the queues that your ports refer to. Or directories for all the FILE ports you use. It is nice to just include some more PowerShell to handle this and just rest assured the external artifact will get created when you deploy your BizTalk updates. In my PowerShell code below it handles creation of folders. Enjoy!

param([string]$pathToBindingsFile,[string]$rootCreatePath)

$newroot = ""

if($rootCreatePath -ne "") {
    $rootCreatePathExists = [io.file]::Exists($rootCreatePath)

    if($rootCreatePathExists -eq $true) {  $newroot = $rootCreatePath }
    else {

        Write-Host "Creating directory" $rootCreatePath
        [io.directory]::CreateDirectory($rootCreatePath)

        $newroot = $rootCreatePath
    }

}

$bindingFileExists = [io.file]::Exists($pathToBindingsFile)

if($bindingFileExists -eq $true) {
    
    # Read in the binding file content
    $bindingContent = [xml](Get-Content $pathToBindingsFile)
    $bindingContent.SelectNodes("//PrimaryTransport") | ForEach-Object {
    
        # Only process if the port type is FILE
        if ($_.TransportType.Name -eq "FILE") {
        
            $directory = [io.path]::GetDirectoryName($_.Address)
        
            # replace with rootCreatePath if this value exists
            if ($newroot.Trim() -ne "") {
                $directory = $directory.Replace($defaultRoot,$newRoot)
            }
           
            
            # Check if the directory exists
            $directoryExists = [io.directory]::Exists($directory)
            if ($directoryExists -ne $true) {
                Write-Host "New directory:" $directory
                [io.directory]::CreateDirectory($directory)
            }
        }
    }
    
}
else {
    Write-Host "Binding file does not exist:" $pathToBindingsFile
}


Advertisements

  1. #1 by Ben Cline on February 15, 2011 - 5:33 pm

    I saw this post: http://geekswithblogs.net/EltonStoneman/archive/2009/06/08/using-powershell-in-biztalk-post-processing-scripts.aspx and Elton mentions a similar idea. I expanded the concept and based the logic on my binding file.

    The nice thing about basing it off of a binding file is that when the binding file changes you do not need to update your script.

  1. PHP Scripts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: