BrainDump
  Home arrow BrainDump arrow Page 4 - Logging Windows 2003 Terminal Server Conne...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Logging Windows 2003 Terminal Server Connections
By: Luke Niland
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-06-19

    Table of Contents:
  • Logging Windows 2003 Terminal Server Connections
  • Creating the Batch File and Why it’s Used
  • Writing the Script
  • Running the Batch File

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Hi,Just a bit of info as to how I used this technique in my organization. We...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT