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
}


2 thoughts on “Time saving PowerShell Snippets for BizTalk Part 1

Add yours

  1. Pingback: PHP Scripts

Leave a comment

Blog at WordPress.com.

Up ↑