AllExperts > VB Script 
Search      
VB Script
Volunteer
Answers to thousands of questions
 Home · More VB Script Questions · Answer Library  · Encyclopedia ·
More VB Script Answers
Question Library

Ask a question about VB Script
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Subbu
Expertise
I can answer VB Script questions which are related to Visual Basic / VBA / QTP Scripts.

Experience
8 Years

Education/Credentials
Master of Computer Applications

Awards and Honors
Established Member from QA Forums and Brainbench certification

 
   

You are here:  Experts > Computing/Technology > Basic > VB Script > vbs script to be modified

VB Script - vbs script to be modified


Expert: Subbu - 6/11/2009

Question
QUESTION: Hello,
I have this script:
This code prints the last logon timestamp for a user.
' ------ SCRIPT CONFIGURATION ------
strUserDN = "<UserDN>"  ' e.g. cn=rallen,ou=Sales,dc=rallencorp,dc=com
' ------ END CONFIGURATION ---------

set objUser =  GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#

This script works perfectly for one user. I am asking for your help to modify this script to work for a list of users which i have in a txt(every user is on a new line in the txt) and to make an export of the result in a tab delimited file.
Can you help me?
Many thanks in advance.


ANSWER: Raducu,

create a text file and save as "userlist.txt" at "c:\" and past your all userdn.  Make sure there will be no header row in text file.  

Try below code and let me know if any issue there.

Subbu.

dim userDN
' Get a free file number
nFileNum = FreeFile
' Open a text file for input
Open "C:\userlist.txt" For Input As nFileNum
' Read the contents of the file
Do While Not EOF(nFileNum)
  Line Input #nFileNum, userDN
  'MsgBox snextline


------ SCRIPT CONFIGURATION ------
strUserDN = userDN  ' e.g. cn=rallen,ou=Sales,dc=rallencorp,dc=com
' ------ END CONFIGURATION ---------

set objUser =  GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#

Loop
' Close the file
Close nFileNum


---------- FOLLOW-UP ----------

QUESTION: Hello, this is the error that i get :
(5, 24) Microsoft VBScript compilation error: Expected end of
statement
Can you help please?
Many thanks!

ANSWER: Raducu,

May I know where you are executing this code?

I mean in Access VBA or Excel VBA or Visual studio?

If possible can you take a screen print and attach the same?

Subbu.

---------- FOLLOW-UP ----------

QUESTION: Hi Subbu,
I run the script in comand promt on a Domain Controller.
I managed to modify the script to do what I wanted but I have another problem now. Please see the code below:
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Const FOR_APPENDING = 8
Const FOR_READING = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\userlist1.txt", FOR_READING)

Do Until objFile.AtEndOfStream
   strNextLine = objFile.Readline
   arrServiceList = Split(strNextLine , " , ")
   For i = 0 to Ubound(arrServiceList)
  
'------ SCRIPT CONFIGURATION ------
strUserDN = StrNextLine  ' e.g. cn=rallen,ou=Sales,dc=rallencorp,dc=com
' ------ END CONFIGURATION ---------



set objUser =  GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "User: " &arrServiceList(i)
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#
         
   Next

Loop
The script works fine now but I want soem help to modify the following:
1) On Error the script returns the same value as the precedent value, something like this:
User1 20
User2 30
User3 30 ( here is an error )
User4 50 and so on. I want to write " Not Found" in case of error and continue the proccessing.

2) I don`t know how to export the output to a .txt file.
Can you help, especially with point 2)
Many thanks in advance.

Answer
Raducu,

Regarding point 2:

Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO1.OpenTextFile("C:\userlist2.txt", FOR_WRITING)

'To write text into file
'Make sure these two lines with in the loop
objFile2.WriteLine "User: " & arrServiceList(i)
objFile2.WriteLine "Approx last logon timestamp: " & intLogonTime + #1/1/1601#

Point 1:

I need more information like:
where exactly you will error?

Give more information I will try.

Subbu.


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.