Batch file to start a stopped windows service or windows service watcher batch

A colleague of mine was having issue with his “Microsoft Exchange Information Store” service it got stopped at certain time after some schedule job of defragmentation and it actually cause the Mail store to un-mount. So he had to get up early in the morning to just restart the service.

I suggested him that before he find more permanent solution to the problem I could write him a small batch script to start the service if it is stopped and then he could configure it as scheduled task to run after certain period of time to check if service is not running then it will start it. Ideally we should use PowerShell script instead of a batch script power shell is really a powerful shell. I used batch since he was using windows 2003 server so it’s easier to run batch then PowerShell.

To cut the long story short below is a simple script which checks if service is running or not using a “net start” piped with “find” command. Name given as parameter to find is “Display Name” of service and if that name is not found which means service is not running which causes errorlevel=1 which is checked through “IF” expression and when it’s true inside of “IF” is executed. In IF “Net Start” command is used to start the service with “Service Name”, below figure 1 shows where to look for these names.

Also you might have noticed “>>” redirection operator. This is used to redirect output of console to a file so that we could log the result in a text file to keep track of what happened when.

Just save the below content in a “.bat” and schedule it as job with windows scheduler with as many “Reoccurrence” as you want since it will start the service if it’s stopped otherwise it will do nothing and there you have it a quick and dirty windows service watcher.

echo on

net start | find /i “Microsoft Exchange Information Store”

If errorlevel== 1 (

net start “MSExchangeIS” >> c:\ServiceCheck.log

Date /T >> c:\ServiceCheck.log

Time /T >> c:\ServiceCheck.log

)

Figure 1 to find out “Display Name” and “Service Name”

 

 

Advertisements

Windows PowerShell to Auto submit web forms

I had a requirement from a client to auto submit some business data to a website. Currently there is a fulltime person who just does that with just 300 forms submissions per day. I was looking for different options to submit data to the website. One was to use httpWebRequest and httpWebReponse classes which are available with dotnet framework to submit data over the https. But it seemed too complex a solution for this problem. Anyways I heard that we could do that with scripts as well and started searching on this subject and Walla I found out that Power-Shell could be used for this purpose too. Writing a script would take lot less effort as compared to writing C# code. In matter of minutes I was able to kick off my automated data submission. Which I unsuccessfully wasted on using http web request using C# with all my wiretapping network traffic from the browser. I almost feel embarrassed for going C#’s way because of the natural bias toward itJ. One must choose the right tool for the Job.

Below is the link to a very good MSDN magazine article on this subject

http://msdn.microsoft.com/en-us/magazine/cc337896.aspx

If you want to learn Power-Shell Script more you can download book from following link. I think it is a pirate book site so please don’t blame me if get caught downloading this J

http://www.wowebook.be/book/windows-powershell-for-developers/

BAM definitions Views, Activities, Fields ID reset or creating new BAM Version

In one of my recent Integration project there was a requirement to create a new version of BAM Definition with few new columns but keeping existing BAM intact without update instead requirement was to create another version of BAM. So I just renamed the BAM views and activities with ‘_V2’ postfix and added new fields and all of this was done through import/export XML of BAM excel add-in. but to my surprise that was not enough and when I tried to deploy it got deployment error that BAM activity with same ID already exists.

So there were two options for me either to do donkey work of changing ID’s manually or write a small tool to do it. Later one seemed a more sensible choice because BAM Definitions were very big with lot of activities.

I am posting this tool here so if anyone has similar issue might find it useful.

It’s a command line tool which accepts following parameters

  1. Give complete path of XML definition file e.g. BAMUpdateGUIDS.exe -d:d:\BAM-A.xml
  2. Give Folder location where all BAM definitions are present e.g. BAMUpdateGUIDS.exe -d:d:\bamfiles

GUID Comparison in Notepad ++

Download File from here. Right click and save as BAMUpdateGUIDS.exe is currently upload as JPEG J

Few Tips around XDocument, XmlDocument, XLANGMessages

XmlDocument class is a very useful class when you want to manipulate XML Document quickly in prototyping mode. But for actually production environment its usage is not recommended. There are lot of reasons for not using it. Memory consumption is one of them because it loads entire DOM in memory so you production memory could fill up fairly quickly if your XML size is relatively large. XmlDocument class has been termed as memory HOG. Here is a very good blog by “Joe Frezner” to give you some idea of comparison of performance of different Xml API’s in .NET.

As per above reference blog it could safely be concluded that XMLWriter and XMLReader are fastest of all XML API’s in .Net not necessarily simplest to implement.  after them we have XDocument(Linq to XML) then we have XmlDocument which 7 times slower than the first one. so if we are looking to use Xml API we should try to find middle ground from DEV, Maintainance and performance perspective and XDocument rightly fits in the middle in most of the cases

So for using XDocument instead of XmlDocument in Biztalk Environment, Following are some points for inter-conversion of types

If you want to manipulate Biztalk Message using XDocument instead of using XmlDocument you could use

following is code to Convert to and from Biztalk message to XDocument Object

XLANGPart to XDocument

public static XDocument GetXDocumentFromXlangPart(XLANGPart messagPart)
{
return XDocument.Load((Stream)messagPart.RetrieveAs(typeof(Stream)));
}

XDocument to XLANGMessage

public void SetResponse(XLANGMessage message, XDocument xDocument)
{
var biztalkMsgStream = new VirtualStream();
xDocument.Save(biztalkMsgStream);
message[0].LoadFrom(biztalkMsgStream);
}

Not:- above method takes in XLang message as input parameter we can create our our XLANGMessage object using inherited class. I will give detail some other blog.

  1. I have seen people using XmlDocument class with Biztalk Message in following way just to get XML content from message whereas there is more efficient way of getting XML Content

Instead you could use following code in Utility class to get XML text from XLANGMessage

public static string GetStringFromXlangPart(XLANGPart messagPart)
{
var reader = new StreamReader((Stream)messagPart.RetrieveAs(typeof(Stream)));
return reader.ReadToEnd();
}

There are some other items in the list I will update them later

Note:- Above are simple snippet to give idea they don’t contain error checks and exception handling but you should include those as well

Also I have recently implemented impilicit conversion of types by overloading assignment operator in a class I will share that in coming days as well

XPath to Select Distinct Nodes

While selecting data from XML files using XPath there would be some scenarios where you want to select only list of unique nodes

Let’s take a simple example from the following XML

<?xml
version=1.0
encoding=utf-8?>

<Categories>

<CategoryID>1</CategoryID>

<CategoryID>1</CategoryID>

<CategoryID>2</CategoryID>

<CategoryID>3</CategoryID>

<CategoryID>3</CategoryID>

<CategoryID>4</CategoryID>

<CategoryID>4</CategoryID>

</Categories>

 

From above XML you want to select only unique category ids and return the following result

Below expressions of XPath 1.0 will allow you to achieve the above result, Later one is fully qualified XPath slightly bigger but work universally.

//CategoryID[not(preceding::CategoryID= .)]

or

//*[local-name()=’CategoryID’ and not(preceding::*[local-name()=’CategoryID’ ] = .)]

Custom Throttling or Multi-Ton Pattern in BizTalk

Sometimes you need to control number of Instances of an Orchestration get created. although BizTalk has its own mechanism for throttling but because of some external resource limitations like database connections, Cloud Connections limitation it is recommended to handle throttling from within the BizTalk Orchestration using Singleton Pattern, Fixed number of Controlled instances (Multi-Ton). There is one very fine blog/article written by Richard Seroter on this. Following is the link to that

BizTalk Orchestration Throttling Pattern