Solved

Vbscript : Last Logon/Log off Time

Posted on 2009-07-16
10
681 Views
Last Modified: 2012-05-07
I have a vbscript to retrieve mailbox information, when I retrieve the information it is in the following format 20090716080200.000312+*** , I need to convert it to MM/DD/YYYY HH:MM:SS format.

Thank You

'
Dim SWBemlocator
Dim objWMIService
Dim colItems
Dim objFSO
Dim objFile
strTitle="Mailbox Report"
strComputer = InputBox("What Exchange server do you want to check?",strTitle,"TANK")
UserName = ""
Password = ""
strLog="MailboxReport.csv"
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.CreateTextFile(strLog,True)
 
'This query will fail with the Exchange WMI Provider
'strQuery="Select ServerName,StorageGroupName,StoreName,MailboxDisplayName,Size,TotalItems from Exchange_Mailbox"
strQuery="Select * from Exchange_Mailbox"
objFile.WriteLine "Server,StorageGroup,MailStore,User,Size(KB),TotalItems,LastLogonTime,LastLogoffTime"
WScript.Echo "Examining " & strComputer
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"\root\MicrosoftExchangeV2",UserName,Password)
Set colItems = objWMIService.ExecQuery(strQuery,,48)
 
For Each objItem in colItems
	objFile.writeline objItem.ServerName & "," & objItem.StorageGroupName &_ 
	"," & objItem.StoreName & "," & Chr(34) &objItem.MailboxDisplayName & Chr(34) &_
	"," & objItem.Size & "," & objItem.TotalItems & "," & Chr(34) & objItem.LastLogonTime & Chr(34) & "," & Chr(34) & objItem.LastLogoffTime & Chr(34)
	
	'uncomment if you want to write to the screen
	'WScript.echo objItem.ServerName & "," &objItem.StorageGroupName &_
	'"," & objItem.StoreName & "," & CHR(34) & objItem.MailboxDisplayName & Chr(34) &_
	'"," & objItem.Size & "," & objItem.TotalItems
Next
 
objFile.close
 
WScript.Echo "See " & strLog & " for results."

Open in new window

0
Comment
Question by:raju1706
  • 5
  • 5
10 Comments
 
LVL 10

Accepted Solution

by:
Alan_White earned 500 total points
ID: 24868334

WScript.Echo convertDate("20090716080200.000312")
 
Function convertDate(sInputDate)
 
	sYear  	 = Left(sInputDate,4)
	sMonth 	 = Mid(sInputDate,5,2)
	sDay   	 = Mid(sInputDate,7,2)
	sHour  	 = Mid(sInputDate,9,2)
	sMin  	 = Mid(sInputDate,11,2)
	sSeconds = Mid(sInputDate,13,2)
 
	convertDate = sMonth &"/"& sDay &"/"& sYear &" "& sHour &":"& sMin &":" &sSeconds
 
End Function

Open in new window

0
 
LVL 1

Author Comment

by:raju1706
ID: 24869687
Alan Thank You for the script, but the time is offset by GMT. The above time is +6 so I have to -6 based on GMT. So how do I get the correct time of local server ?
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 24869882
The time of the "input date" or the system date is?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Expert Comment

by:Alan_White
ID: 24870102
OK, I think I've figured it out.
I originally thought it was just a matter of adding or subtracting 6 from the sHour variable but that wouldnt work if the hours were close to the next or previous day.
So in the script I posted, I was just putting the date time string out as a string.  Now we need to convert it to an actual date so that we can then add (or subtract) 6 hours from it using the dateadd function.
The problem is that the system locale might cause your input date to be incorrect (UK vs US formats).
Have a look at below and see if it makes sense.
 

WScript.Echo convertDate("20090716200200.000312")
 
Function convertDate(sInputDate)
 
	Dim sYear, sMonth, sDay, sHour, sMin, sSeconds
	Dim sDateString, sDate, sNewDate
 
	Const cGMTOffset = -6
	
	sYear  	 = Left(sInputDate,4)
	sMonth 	 = Mid(sInputDate,5,2)
	sDay   	 = Mid(sInputDate,7,2)
	sHour  	 = Mid(sInputDate,9,2)
	sMin  	 = Mid(sInputDate,11,2)
	sSeconds = Mid(sInputDate,13,2)
 
	'sDateString = sMonth &"/"& sDay &"/"& sYear &" "& sHour &":"& sMin &":" &sSeconds 'US Format
	sDateString = sDay &"/"& sMonth &"/"& sYear &" "& sHour &":"& sMin &":" &sSeconds 'UK Format
	
	sDate = CDate(sDateString)
	sNewDate = DateAdd("h",cGMTOffset,sDate)
	
	convertDate = FormatDateTime(sNewDate,0)
 
End Function

Open in new window

0
 
LVL 1

Author Comment

by:raju1706
ID: 24870405
Thank You Alan, I will award the points but I have one last query, from the server time can we dynamically get the cGMTOffset, as I will be querying servers across the globe I cannot have a constant cGmtOffset.

0
 
LVL 10

Assisted Solution

by:Alan_White
Alan_White earned 500 total points
ID: 24870782
I had to google that one.
I have found the script below and it seems to work, however my TZ is set to GMT so it's reporting 0.  I therefore dont know exactly what the output is going to look like.  Maybe you can try this script in isolation and tell me what you get?
If it's in minutes, we will need to change the parameter in the dateadd function.

wscript.Echo getTimeOffset
 
Function getTimeOffset
 
	strComputer = "."
	
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	
	Set colTime = objWMIService.ExecQuery("Select * From Win32_TimeZone")
	
	For Each objTime in colTime
	    intBias = objTime.Bias
	Next
	
	getTimeOffset = intBias
	
End Function

Open in new window

0
 
LVL 1

Author Comment

by:raju1706
ID: 24870980
Alan Thank You once again. The output is in minutes.
0
 
LVL 1

Author Closing Comment

by:raju1706
ID: 31604169
Thank You appreciate it.
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 24876534
Raju1706:
As the output is in minutes, you need to change the line in the convertDate Function to
sNewDate = DateAdd("n",cGMTOffset,sDate)
Note the "n" instead of "h", this tells it you want to add/subtract minutes instead of hours.
I assume you have figured out how to use all these component functions together?
Cheers,
Alan
0
 
LVL 1

Author Comment

by:raju1706
ID: 24881722
Alan, Thank You once again.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question