This is the first in a series of posts where I want to show some more advanced scripts and things you can do with the PowerShell provider for BizTalk.
I usually do a lot of tryouts and proof of concepts on my development box. In my urge to get things working I never spend too much time on descriptive application names. I also never clean up things after I’m done.
This will in inevitably lead to the following mess after a couple of weeks:
From time to time I feel the need to clean up this mess. I used to do this with the BizTalk Administration Console. I start on top of the list right click the application to first stop and then delete it. Then I go to the second, etc.
The problems start to arise when there are references between applications. If I delete an application that is referenced from another application I get the following warning:
I can still delete the application but the warning is annoying and takes time. The last thing I want is to lose too much time cleaning up
Things get even worse when there are assembly resources inside the application which are referred to by assembly resources in other applications. This time it is not a warning but a serious error preventing me from deleting the application.
This means I need to take this reference dependency tree into account and delete the “child” applications before their “parents”.
This all makes cleaning up my development box very annoying and time consuming which is probably why I keep postponing it in the first place.
You can nicely automate this procedure using a generic application removal script that uses the PowerShell provider for BizTalk. In the script I take advantage of the exception handling possibilities of PowerShell. I sort of do a “on error resume next”.
This is how the script works:
- - Build a list of all the applications and put them in a array variable.
- - Loop through the list and try to delete the application on top of the list.
- - When the deletion fails because of dependencies we catch, in PowerShell terms “trap”, the exception and shuffle the array a little bit so that the application that failed is moved to the bottom of the list.
- - We then continue with the application that is now on top of the list.
- - The loop will end when all the applications are removed.
This is the script:
Some details about the script:
In the fragment below we use the provider to get a list of all applications. We make sure exclude the system and default application because they cannot be deleted.
The second thing to show in detail is the error handling routine. The provider (or actually the BizTalk Management Classes library) throws an exception of type BtsException since the application is referenced and cannot be deleted. We catch this exception by using a trap statement. In the exception handler we do two things. First we make sure to set a variable in the main scope so that we know what to do with the array later. Either move the top item to the bottom or remove the top item. Second we use “continue;” to have the main routine continue with the next statement.
After running the script this is what the admin console shows:
Nice!!!!
The script can be downloaded from here
Tagged: BizTalk, BizTalk Management, BizTalk tools, PowerShell
