Wednesday, March 25, 2009

FOR and NET FILE

I was presented with the task of figuring out how to kick users off of a server who did not properly log off at the end of their work day. These folks would unknowingly hang on to a file that pretty well kinked up the entire schedule. The solution to this seems easy enough, right? Problem is, most of the scripts that are out there are an all-or-nothing solution. Most of our users utilize a terminal server to get to the app that we want to disable when our nightly processes are to begin. Those users that are directly connecting through a mapped drive would not be included in a NET SESSION /DELETE command. Also, that could potentially knock off someone that may be connected to that server for another purpose that didn't include the application in question.

In my search for other possibilities in the NET suite of commands, I found NET FILE. This lists all of the open files on the machine the command is executed on with the ID, File path and name, user id and 1 or 0 for locked file indicator. Since I didn't want to knock out the main user id that connects up the middle-ware, I had to find a way to tell it to kick out all users except for one and only those that have locked files. This is where I found that the FOR command was helpful. I was able to get it to pull the three fields I needed into variable, use IF condition to test for the values I wanted and, if it found them, it passed the ID into the NET FILE ID /CLOSE command.

We tested it last night as a batch file in a scheduled task and it worked great! I am excited that I have discovered this (although it has been around forever) and the potential that it has to make a lot of tasks run more smoothly or even be completely automated!