Re: Capture logon info by jd71
jd71
Wed Oct 18 12:59:16 CDT 2006
I am not sure how to pipe the output to a text file from your call to
the vb script, but I have reworked the original. This script will now
create the text file "serial.txt" in the already created folder on the
share that is named for the computer (ex: \\yourserver\yourshare$\" &
compname & "\serial.txt").
'*******************************************************
On Error Resume Next
'*** Declarations ***
Dim Shell,FSO,Network,dbConn,locarray
'*** Objects ***
Set Shell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
'*** Environment strings ***
lserver = shell.expandenvironmentstrings ("%logonserver%")
compname = shell.expandenvironmentstrings("%computername%")
user = Shell.ExpandEnvironmentStrings("%username%")
filelocation = "\\yourserver\yourshare$\" & compname & "\serial.txt"
Set objWMIinfo = GetObject("winmgmts:\\" & compname & "\root\cimv2")
Set colItems = objWMIinfo.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem in colItems
sernum = objItem.SerialNumber
manu = objitem.Manufacturer
Next
Set colitems = objWMIinfo.ExecQuery("Select * from Win32_Processor")
For Each ObjItem in colItems
proctype = objitem.description
next
Set LogFile = FSO.CreateTextFile(filelocation)
Logfile.writeline("Logon Server: " & lserver & vbcrlf & "Computer Name:
" & compname & vbcrlf & "Processor Type: " & proctype & vbcrlf &
"Manufacturer: " & manu & vbcrlf & "Serial Number: " & sernum & vbcrlf
& "Username: " & user)
Logfile.close
'*******************************************************
You might also think about having all of your computers write to a
single csv file that you could easily import into Excel, Access, or
some other database. I have created a working example below. The
example has to Functions, you just need to choose which one suits your
needs. Choose either "Searchappend" or "Appendonly" and comment out
one and uncomment out the one you wish to use. I still highly
recommend using MySql (it is free after all!). Good Luck!!
'*******************************************************
On Error Resume Next
'*** Declarations ***
Dim Shell,FSO,Network,dbConn,locarray
Const ForAppending = 8
Const ForReading = 1
'*** Objects ***
Set Shell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
'*** Environment strings ***
lserver = shell.expandenvironmentstrings ("%logonserver%")
compname = shell.expandenvironmentstrings("%computername%")
user = Shell.ExpandEnvironmentStrings("%username%")
filelocation = "\\yourserver\yourshare$\serials.txt"
Set objWMIinfo = GetObject("winmgmts:\\" & compname & "\root\cimv2")
Set colItems = objWMIinfo.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem in colItems
sernum = objItem.SerialNumber
manu = objitem.Manufacturer
Next
Set colitems = objWMIinfo.ExecQuery("Select * from Win32_Processor")
For Each ObjItem in colItems
proctype = objitem.description
next
'searchappend
appendonly
Function searchappend()
'*** If the computer's name is found in the text file, do not append
'*** Caution: If you have a high volume logins, this function will be
'*** very slow as it has to open the text file and read each line for
each login
if FSO.FileExists(filelocation & ".txt") then
Set LogFile = FSO.OpenTextFile(filelocation, ForReading, True)
Do While LogFile.AtEndOfStream <> True
If inStr(LogFile.Readline, compname) then
LogFile.close
wscript.quit
end if
loop
LogFile.close
Set LogFile = FSO.OpenTextFile(filelocation, ForAppending, True)
Logfile.writeline(lserver & "," & compname & "," & proctype &
"," & manu & "," & sernum & "," & user)
else
Set LogFile = FSO.CreateTextFile(filelocation)
Logfile.writeline(lserver & "," & compname & "," & proctype &
"," & manu & "," & sernum & "," & user)
end if
End Function
Function appendonly()
'*** Appends the text file only. Much more efficient, but you will
have duplicates.
if FSO.FileExists(filelocation) then
Set LogFile = FSO.OpenTextFile(filelocation, ForAppending, True)
Logfile.writeline(lserver & "," & compname & "," & proctype & ","
& manu & "," & sernum & "," & user)
else
Set LogFile = FSO.CreateTextFile(filelocation)
Logfile.writeline(lserver & "," & compname & "," & proctype & ","
& manu & "," & sernum & "," & user)
end if
End Function
Logfile.close
wscript.quit
'*******************************************************
-Jason
On Oct 17, 10:55 pm, "MandG" <gsca...@gmail.com> wrote:
> That is awesome - I am extremely grateful for this and for users like
> yourself who take the time to share you knowledge and skills. I have
> one small question however -
>
> I would like to direct this output to a network share and simply name
> it "serial.txt" and place it within the folder that's named after the
> computer (in most cases this already exists). So, my command would look
> like this:
>
> call \\10.1.16.2\netlogon\thiscoolscript.vbs
> ->t:\accesslog\%computername%\serial.txt
>
> So what (if any) changes would I have to make to your script so that my
> command guides the output and not your script? I don't think I can
> safely remove your "filelocation =" line.
>
> And then of course I'd have to remove the part where your script names
> the txt file after the compname (which is actually pretty cool - sorry
> I'm easily impressed with you scripting wizards-)