Logging Windows 2003 Terminal Server Connections - Running the Batch File
(Page 4 of 4 )
Then we run the batch file we created in the last step to get our user list. We run it using the shell object we created. First we pass the name of the batch file, then a 1 is the window style (1 means activate and display the window), the true means wait until the process we are calling has finished running before carrying on with the rest of the script.
'Now open up the file and get the number of users out. All we do is open the file 'and read every line, and count the read lines. Then knock off one for the first 'header line:
Set objUserFile= objFSO.OpenTextFile("c:CurrentTSUsers.txt", ForReading)
Do until objUserFile.AtEndOfStream
strCurrLine = objUserFile.readline
intLineCnt = intLineCnt + 1
loop
'Close the file
objUserFile.Close
'Get the end amount of users no header row if 0
if intLineCnt > 0 then
intLineCnt = intLineCnt -1
end if
'Now get the last number of connections from the local file. If they are the same 'we can just quit and not bother doing an update
Set objUserFile= objFSO.OpenTextFile("c:LastTSUserCnt.txt", ForReading)
intLastCnt = trim(objUserFile.readline)
objUserFile.close
Now, in this block of code we are going to open up the text file the batch file has created for us and read its information.
First of all, use the FileSystemObject we created to open up the file for reading.
If you have changed the file/path to which the batch file sends its output, make sure you are using the same one here.
Now that the file is open, we will loop line by line through the text file, read the current line into a variable and increment the line count by one every time we go through the loop. When we have come to the end of the loop we close the file.
The file that gets created always has a header line at the top if there are users logged in. We don’t want to include this line in our count so the next section simply takes one of the count variables if it is greater than zero.
The next line of code opens up another file that we use to get its information out. Every time the script runs we write the number of users we found into this file.
Then we compare this number to the current number and if it is the same we don’t bother doing a database update as the details are the same. This is just done to decrease hits on the database. The code simply reads the number out of this file and saves to a variable, then closes the file.
If the script has never run on the machine before it could make an error at this point if it can’t find the c:LastTSUserCnt.txt file. Just create a blank file with the correct name if this happens
if Cint(intLastCnt) <> Cint(intLineCnt) then
'connect to the database
strConnString = "Driver={SQL Server};Server=TestServer;Database=TS_Connections;Uid=test;Pwd=test;"
Set connIns = CreateObject("ADODB.Connection")
connIns.Open strConnString
'now build the sql to update the new info the database and run it
strUpdateSql = "UPDATE ts_status SET current_count = " & intLineCnt & ", last_count_time = '" & strSQLDate & "' WHERE server_name = '" & strServerName & "'"
set rsServerUpd=CreateObject("ADODB.Recordset")
rsServerUpd.open strUpdateSql,connIns,3,3
connIns.close
set connIns = Nothing
'now update the count file with the new info
Set objUserFile = objFSO.OpenTextFile("c:LastTSUserCnt.txt", ForWriting)
objUserFile.Write intLineCnt
objUserFile.Close
end if
The final part of the code in the script is the bit that actually does the database update. The code is wrapped in an if statement which checks to see if the last count and current count are the same. As discussed before, if they are the same then we don’t bother doing anything.
If we are updating the database, the first part of the code creates a connection string to the database. In this case I am using a SQL Server connection string. You will have to alter this to match your specific database type and location.
Then, we create an ADOB object and open a connection to our database using the connection string.
Now that we have a connection to the database, we build a SQL query to run that contains our new user count and date values. A record set object is then created using ADOB, and the SQL query is executed on the database connection.
You might get an exception thrown here if the server does not yet exist in your table. Add one in before you run the SQL and it will work okay. Alternatively you could alter this section of the script to check for the existence of the server, and if it's not there run an INSERT command instead of the UPDATE.
The connection to the database is then closed.
The final part of the code writes out the value of the count to the text file, ready for when the script runs next time.
Going Forward
Hopefully, with these simple building blocks you can now log information about your terminal server usage. You can change the script so it writes out the user names to the database, find the amount of time users are spending on the servers, and whether they are logged into more than one server. You'll find various other uses for the code as well. I have used it in the past to write an application that load balances the users across multiple servers.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |