Wednesday, December 17, 2008

Killing a Process running on Remote Computer(The batch file way..)

This morning when I came to office, I had a set of tasks which needed to be completed ASAP. My current engagement requires me to work on customer server via RDP. Now RDP has an interesting behavior, in certain cases if the server has a limited resources, it will simply refuse to allow RDP access. And unfortunately, it happened to me today. One of my colleague ran a time-consuming, resource-hungry custom code over the weekend so we can work smoothly during the weekdays, however the application could not complete its work during the weekend and was still running and consuming whatever resources were available and in turn caused a DoS (Denial Of Service). I thought to use VBScript/WMI to kill the remote process as I had the name of the process to be killed and have had used it in past effectively. I started working with the script but, I felt it will take too much time to figure out the script that will help me so I decided to search on my favorite search engine for easier and faster options. Within a few minutes I got the pointers to use tasklist.exe and taskkill.exe. I killed the process manually and easily got my access restored. However at end of the day, spending some time one this and making a batch file will come handy as we run into this situation more often. So I spent few minutes in notepad and here is the batch file that I got. This batch file is not polished but should get the job done in time of needs. And as usual I don't take any responsibility of any kind if you use this script and get into some trouble. So use it at your own risk...

@ECHO OFF
@ECHO WARNING : This script will forcefully kill the process and will not ask for confirmation before killing the process.

SET MESSAGE=
SET SYSTEMNAME=
SET USERNAME=
SET PASSWORD=
SET PID=

SET /P MESSAGE=Press any key to continue...

SET /P SYSTEMNAME=Enter target computer name or IP :
SET /P USERNAME=Enter User Name (domain\username) :
SET /P PASSWORD=Enter Password :

@ECHO Please wait while we get the list of processes...

TASKLIST.EXE /S %SYSTEMNAME% /U %USERNAME% /P %PASSWORD%

SET /P PID=Enter PID(Process ID) of the process you want to kill :

TASKKILL.EXE /F /S %SYSTEMNAME% /U %USERNAME% /P %PASSWORD% /PID %PID%

SET /P MESSAGE=Press any key to continue...

Generic Serializer-Deserializer

Around one and half year back, I was handling an interesting situation. There were few different systems sending in XML messages, and my component had to accept the message, interpret it and take appropriate actions.

It was easy till I came to know about XML message and its elements and alternate messages. The possibilities were unlimited, and it sounded stupid to hand code/hard code XML elements. Rather than that the solution was designed so that we converted XML -> XSD -> CS. After few changes made here and there to fine tune the CS files with XML, we faced another challenge, we had to serialize/deserialize incoming/outgoing messages. After few minutes of R&D I could pull out this. I've listed the method signature and minor code implementation. It is so simple that you can easily get it working in few minutes.

public static string Serialize<T>(
T data
)
{

//Create the Serializer

XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

//Use the serializer we have just created and serialize the object.

}

the same goes with the Deserializer

public static T Deserialize<T>(
string serializedData
)
{

//check whether the incoming data is not a null string
if (string.IsNullOrEmpty(serializedData))
return default(T); //This can be controversial, so handle with care and as per your requirements, in certain cases you can return null as well, kindly let me know if you have anything that can improvise this code

//create a serializer
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

//Use the deserializer we have just created and deserialize the object.

}

I must thank my friend Sidhartha http://IamJunk.spaces.live.com, as he helped me a lot to enhance the code. I've not published the whole code, in case you need it, drop me an email/comment. I will be more than happy to help you out.

All In One Entity for CRM 4.0

Sometimes back I was doing a research on a script(It is coming soon.. just wait and watch...) and that script required me to try all the CRM datatypes one by one on the form. And trust me, it was a pretty boring job to create the element on a CRM form and then check compatibility of that script with it. Anyway, I thought I can help out my friends out there just in case they face the similar situation. So here is a SkyDrive link :

https://cid-b6a90e5b5092759c.skydrive.live.com/self.aspx/BlogStore/AllInOneEntity.zip
If you find that I've missed out something, kindly drop me a note. Thanks in advance.