Logon scripts

Started by benthehutt, March 24, 2006, 05:49:37 PM

Previous topic - Next topic
So I've been asked to figure out if we need to rewrite our current logon scripts.  They're all written in kixStart and they want me to figure out if they'd be better written in VBScript.  Since I have no knowledge in either scripting language, what do you guys think?
(Probably either way I'd have to rewrite them)
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

I just wanted to say I feel sorry for you ... having to possibly learn VBScript ...

Or I suppose you could find benchmarks or something to determine which would be better.

But keep in mind that maintainability is very important; If you can't maintain it, what's the point of the program/script ? So a program that can't be modified efficiently, etc., is pointless so you should learn what's best suited for you (or the system) ...

About all I can offer I'm afraid, since I know crap all about either language.
"My Terminal is my Soul"

It seems to me like I should just learn VBScript.  I mean, Kix is only for logon scipts.  VBScript is for whatever you want.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

Hey Metty don't knock VBscript..

I have never used Kix .. but it even sounds dodgy .. haha .. Any logon scripts or pretty much any system/admin scripts i need to write are all done with VBscript ... unless i am on my Linux systems of course. But i really like VBscript ..

If ya need any help with it .. Let me know.
I am not suffering with insanity... I am loving every minute of it.

Oh, I need some help alright.  Basically, I need to write a logoff script that gets some info from the user.  I've gotten it to get the username and computer name, but the current date and time I'm a bit sketchy on.  I don't want to get it from that computer, because all of the computers have different clocks.  So I decided to try using the "net time" command.  It's very messy though, and assumes that the computer can contact the server (it also doesn't write anything yet):

Option Explicit

dim wshNetwork, wshShell, scriptExec, command, results

command = "net time \\myComputer"
set wshNetwork = WScript.CreateObject("WScript.Network")
set wshShell = Wscript.CreateObject("WScript.Shell")
WScript.Echo "Your Computer Name is " & wshNetwork.ComputerName & " and your username is " & wshNetwork.UserName

set scriptExec = wshShell.exec(command)

do until scriptExec.Status = 1
   wScript.sleep 100
loop

while Not scriptExec.StdOut.AtEndOfStream
    results = results & scriptExec.StdOut.Read(1)
wend

dim startDate, endDate, myDate, myTime

startDate = InStr(results, "is ")
endDate = InStr(startDate + 3, results, " ")
myDate = mid(results,startDate+3,endDate-startDate-3)

startDate = InStr(endDate, results, " ")
endDate = InStr(startDate+1,results,"M")

myTime = mid(results,startDate+1,endDate-startDate)

wscript.echo "The current time is " & myDate & " at " & myTime


I also need to fiddle with converting the time to 24-hour time...anyway, I know there's gotta be a better way to do this, any ideas?

Also, I'd like the time to be accurate down to the second, any ideas on that as well?
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

Also, I need some way of figuring out the Windows version and SP info.  Is there anyway I can do that?
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

Ha!  Found it:

dim regKeyOS, regKeySP, regResponseOS, regResponseSP
regKeyOS = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName"
regKeySP = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CSDVersion"

'Windows version
regResponseOS = wshShell.RegRead (regKeyOS)
'Service pack installed
regResponseSP = wshShell.RegRead (regKeySP)


Now I need some code to find the MAC address and full name of the person logged on--I suppose that'll be an AD search?  (I don't know why I bother asking, I'm gonna figure it out before Cobra ever replies)  ;D
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

That's only because he has some issues he has to take care of lately in real life ... otherwise he'd beat you to it!

"My Terminal is my Soul"

For all of you people who have been following along, I've now gotten my VBScript to retrieve the long name of the user:

dim objCommand, objConnection, strBase, strQuery, strFilter
dim strAttributes, objRecordSet, strDisplayName, strDisplay

'Start seaching from here
strBase = "<LDAP://dc=myDomain,dc=com>"

'Create command and connection objects
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

'Search for all records with this username
strFilter = "(cn=" & wshNetwork.UserName & ")"

'Only return their full names
strAttributes = "displayName"

'Construct query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

'Execute query
Set objRecordSet = objCommand.Execute

'Run through each value returned--hopefully there will be only one record
Do Until objRecordSet.EOF
  strDisplayName = objRecordSet.Fields("displayName").value
  wscript.echo "Name: " & strDisplayName
  objRecordSet.MoveNext
Loop

'Close the connection
objConnection.Close


Now I've just got to write some code to find the MAC address and I'm home free!  Woot, woot!

(*Raise the roof--and let the walls fall down!*)  8)
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

The cherry on top: getting all the MAC addresses.

dim IPConfigSet, IPConfig, custom

Set IPConfigSet = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
wshNetwork.ComputerName & "\root\cimv2" ).ExecQuery _
("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration " & "WHERE IPEnabled=true" )
For Each IPConfig In IPConfigSet
   wscript.echo "Your MAC Address is: " & IPConfig.MACAddress
next


I'll post the whole thing as soon as I finish sorting it all out.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

March 29, 2006, 05:50:03 PM #10 Last Edit: March 29, 2006, 05:51:18 PM by benthehutt
Alrighty then, my very first VBScript is done, how 'bout that, eh?  It's to run when a user logs onto the network, and it's compatible with Windows 2000/XP/2003, or anything with WSH installed on it (which is also some NT's and some 98 machines).  Anyway, it determines where it needs to write.  Say it's march, then it would write in 2006Mar.txt.  If the file isn't there, it creates it, if it is, it append is.  Each logoff is of the form:

RecordType ; Date ; Time ; MAC ; IP ; COMPUTERNAME ; DOMAIN ; USERID ; Full Name ; Windows Version ; Service Pack Type

Also, the time is taken from one of our servers, so it's constistent.

'The Logoff Script
'Written Poorly By: benthehutt
'Last Updated: March 2006

Option Explicit

'---------------------------------------------------------------------------------------------------------------------------------------'
'Bunch of objects needed in multiple places

'Record type for logoff entries
dim recordType : recordType = 200

'Directory in which the log is kept
dim strDirectory : strDirectory = "\\tadc2\syslog$"

'New network object
dim wshNetwork
set wshNetwork = WScript.CreateObject("WScript.Network")

'New shell object
dim wshShell
set wshShell = Wscript.CreateObject("WScript.Shell")

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the date and time

dim scriptExec, command, results, startDate, endDate, myDate, myTime
dim militaryTime, meridian, finalTime

'Give shell object a net time command and execute it
command = "net time \\myServer"
set scriptExec = wshShell.exec(command)

do until scriptExec.Status = 1
   wScript.sleep 100
loop

while Not scriptExec.StdOut.AtEndOfStream
    results = results & scriptExec.StdOut.Read(1)
wend

'Parse all the stuff to get the date and time
startDate = InStr(results, "is ")
endDate = InStr(startDate + 3, results, " ")
myDate = mid(results,startDate+3,endDate-startDate-3)

startDate = InStr(endDate, results, " ")
endDate = InStr(startDate+1,results,"M")

myTime = mid(results,startDate+1,endDate-startDate)

'Change it to military time if need be
militaryTime = Split(myTime,":")
meridian = Split(militaryTime(1)," ")
if meridian(1)="PM" then 
   militaryTime(0) = militaryTime(0) + 12 
end if

finalTime = militaryTime(0) & ":" & meridian(0)

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the MAC address

dim IPConfigSet, IPConfig, custom, MAC

'New object, search for MAC addresses
Set IPConfigSet = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
wshNetwork.ComputerName & "\root\cimv2" ).ExecQuery _
("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration " & "WHERE IPEnabled=true" )

'For each one found...
For Each IPConfig In IPConfigSet
   MAC = MAC & IPConfig.MACAddress
next

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the IP address

dim ipRawData, ipData, ipCommand
dim ip, ipStart, ipEnd

'Command for ipconfig
ipCommand = "%comspec% /c ipconfig"
set ipRawData = wshShell.exec(ipCommand)

do until ipRawData.Status = 1
   wScript.sleep 100
loop

while Not ipRawData.StdOut.AtEndOfStream
    ipData = ipData & ipRawData.StdOut.Read(1)
wend

'Parse and find out all the ip addresses
ipStart = InStr(ipData, "IP Address")
ipStart = InStr(ipStart, ipData, ":") + 2
ipEnd = InStr(ipStart, ipData, " ")
ip = mid(ipData,ipStart,ipEnd-ipStart)

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the long name of the user

dim objCommand, objConnection, strBase, strQuery, strFilter
dim strAttributes, objRecordSet, strDisplayName, strDisplay

'Start seaching from here
strBase = "<LDAP://dc=myDomain,dc=comu>"

'Create command and connection objects
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

'Search for all records with this username
strFilter = "(cn=" & wshNetwork.UserName & ")"
'Only return their full names
strAttributes = "displayName"
'Construct query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
'Execute query
Set objRecordSet = objCommand.Execute

'Run through each value returned--hopefully there will be only one record
Do Until objRecordSet.EOF
  strDisplayName = objRecordSet.Fields("displayName").value
  objRecordSet.MoveNext
Loop

'Close the connection
objConnection.Close

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the windows version ans SP info

dim regKeyOS, regKeySP, regResponseOS, regResponseSP

'Registry keys with Windows version and SP info
regKeyOS = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName"
regKeySP = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CSDVersion"

'Search egistry for keys
regResponseOS = wshShell.RegRead (regKeyOS)
regResponseSP = wshShell.RegRead (regKeySP)

'---------------------------------------------------------------------------------------------------------------------------------------'
'Figure out what file you need to write to and write it

Dim objFSO, objFSOText, objFile, objTextFile, strFile, dateArray, month
const ForAppending = 8

dateArray = split(myDate,"/")

select case dateArray(0)
   case 1
      month = "Jan"
   case 2
      month = "Feb"
   case 3
      month = "Mar"
   case 4
      month = "Apr"
   case 5
      month = "May"
   case 6
      month = "Jun"
   case 7
      month = "Jul"
   case 8
      month = "Aug"
   case 9
      month = "Sep"
   case 10
      month = "Oct"
   case 11
      month = "Nov"
   case 12
      month = "Dec"
end select   

strFile = "\" & dateArray(2) & month & ".txt"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strDirectory & strFile) Then
'Append, don't create
   Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForAppending, True)
Else
'Create file
   Set objTextFile = objFSO.CreateTextFile(strDirectory & strFile)
End If

'Write it!
objTextFile.WriteLine(recordType & ";" & myDate & ";" & finaltime & ";" & MAC & ";" & ip & ";" & wshNetwork.ComputerName _
& ";" & wshNetwork.UserDomain & ";" & wshNetwork.UserName & ";" & strDisplayName & ";" & regResponseOS _
 & ";" & regResponseSP)

'Close log file
objTextFile.Close


If anyone has any suggestions, I'd be very willing to hear them, this is my first VBScript script (that sure sounds funny).  You can download the source if you'd like a better look at it.

Woo-hoo!  How's that Cobra?
;D
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

Sorry for the long delay. Not on the board that often. so i am just plagued with Red Dots telling me whats new.

Looks good mate.. Will download it and go through it.

But looks good.. fair play.
I am not suffering with insanity... I am loving every minute of it.

To tell the truth, I've already worked some bugs out and have a different version running here.  In this new one it grabs ALL the MAC addresses and all the IP's associated with those MACs, not just the one it grabs in this script.

Just replace the part wher it finds the MAC with this:

'---------------------------------------------------------------------------------------------------------------------------------------'
'Get the MAC addresses

dim IPConfigSet, IPConfig, custom, MAC, MACAddresses
dim IPCommand, commandResults, commandResultsRaw, counter : counter = 0
dim startMAC, startIPText, startIPColon, startIP, endIP, IP, convertedMAC
IPCommand = "%comspec% /c ipconfig /all"

'New object, search for MAC addresses
Set IPConfigSet = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
wshNetwork.ComputerName & "\root\cimv2" ).ExecQuery _
("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration " & "WHERE IPEnabled=true" )

'For each one found...
For Each IPConfig In IPConfigSet
   counter = counter + 1
   dim MACArray
'Get the first MAC address
   MAC = IPConfig.MACAddress
'Convert it to a MAC with dashes
   MACArray = split(MAC,":")
   convertedMAC = MACArray(0) & "-" & MACArray(1) & "-" & MACArray(2) & "-" & _
   MACArray(3) & "-" & MACArray(4) & "-" & MACArray(5)
'ipconfig /all
   set commandResultsRaw = wshShell.exec(IPCommand)
   do until commandResultsRaw.status = 1
      wscript.sleep 100
   loop
   while not commandResultsRaw.StdOut.AtEndOfStream
      commandResults = commandResults & commandResultsRaw.StdOut.Read(1)
   wend

   startMAC = instr(commandResults,convertedMAC)
   startIPText = instr(startMAC,commandResults,"IP")
   if IP < startMAC then
      startIP = instr(startIPText,commandResults,":") + 2
      endIP = instr(startIP,commandResults," ")
      IP = mid(commandResults,startIP,endIP-startIP)
   else
      IP = "0.0.0.0"
   end if
   if counter = 1 then
      MACAddresses = MAC & "," & IP
   else
      MACAddresses = MACAddresses & "," & MAC & "," & IP
   end if
   
next
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

SMF spam blocked by CleanTalk