Coat of Arms

Possibly the best vbscript logon script in the world.

Ok, so I might be slightly biased here, seeing as I wrote the script, so naturally i’m gonna boast a little.

I wrote the script as a replacement for our current user logon script at work. Currently, a script is executed when a user hits our domain, which creates some text files on the users computer. These text files store bits of inventory related info such as hostname, user logged on and the Dell service tag of the machine.

Great. Now what can we do with that. Don’t get me wrong, it does the job, but i’ve created numerous types of apps that scour the network, pulling this info from each terminal, but it takes too long.

So, having come across a few problems here and there, I decided to have a go myself. I decided due to the extensibility of Microsofts vbscript, there must be a way to get the information stashed into a SQL database, and man was I right.

First off, we have to declare a few objects.

On Error Resume Next
Set oNetwork = CreateObject("WScript.NetWork")
Set oShell = CreateObject("WScript.Shell")

It is fairly important that you have the ‘error’ statement at the top, because if our script hits an error, we don’t want it to pause, as the user login will stop! When you deploy to 2000 machines like I have done, the last thing you want is 2000 angry users hitting up your helpdesk.

Next, we use the objects declared above to store some simple bits of information into some strings.

sUserName = oNetwork.UserName
sHostName = oNetwork.ComputerName
sDomainName = oNetwork.UserDomain
sDate = Date
sTime = Time
sLastLogon = sDate & " " & sTime

As you can see, we now have some of the basic information stored. We have the username of the logged in user, as well as the domain name and computer name. Also, we store the logon time. The next part I have used to get the service tag of the machine. This proabably only works on Dell machines, as I havn’t tried it on anything else. The service tag is stored in the BIOS, so we need to setup some interaction. Here goes…

Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHostName & "\root\cimv2")Set sSerials = oWMIService.ExecQuery("Select * from Win32_BIOS")
for each sSerial in sSerials
     sServiceTag = sSerial.SerialNumber
Next

The sServiceTag string should now hold the service tag of the machine.

Now, for the fun part. We have some strings and we want to record them to the database. Obviously, i’m going to assume you have a SQL database sitting on the network somewhere with the table configured to accept this data. Make sure you give adequate permissions for your users, as they will need to INSERT, SELECT and UPDATE.

Let’s setup the connection and objects first.

set oConn = CreateObject("ADODB.Connection")
set oRecordSet = CreateObject("ADODB.Recordset")
sSelectQuery = "SELECT * FROM Computers WHERE cHostName = '" & sHostName & "'"
sInsertQuery = "INSERT INTO Computers ( [fieldnames] ) VALUES ( '" & namesofstrings & ")"

sConnString="Provider=SQLOLEDB.1;Data Source=SERVERNAME; Initial Catalog=DATABASENAME;Integrated Security=SSPI;"

oConn.open = sConnString
set oRecordSet = oConn.execute(sSelectQuery)

if (oRecordSet.BOF and oRecordSet.EOF) then
     oConn.execute(sInsertQuery)
else
     oRecordSet.movefirst
     do until oRecordSet.EOF
          sUpdateQuery = "UPDATE Computers SET " & _
          "cLastUser = '" & sUserName & "', " & _
          "cServiceTag = '" & sServiceTag & "', " & _
          "cOS = '" & sOperatingSystem & "', " & _
          "cTemplateVer = '" & sTemplateVer & "', " & _
          "cLastLogon = '" & sLastLogon & "', " & _
          "cAddress = '" & sAddress & "' " & _
          "WHERE cHostName = '" & sHostName & "'"
          oConn.execute(sUpdateQuery)
          oRecordSet.movenext
     loop
end if
And that's pretty much all there is to it, when the users log in, the information will get pushed to the SQL database and you should have a self-completing inventory system.

If you had problems with this, or couldn't figure out my table schema, or you're just too damn lazy to copy and paste, you can download the script (complete with commenting) right here. It also shows the database schema. Oh, and it also has the housekeeping section to tidy away all the nasty little leftovers. Come to think of it, just download it and see.

InfoLogging.txt (Remember to rename me to .VBS)

Kala's Birthday - April 2008Oxford - April 2008Geraints 21st - Sep 2007Spain - July 2006Random Phone SnapsPaintball 2006Oxford 2006New Year 2007Georgina and Ians WeddingFlorida 2007