Using WMI dates in custom inventory 7.x
WMI Date format
Dates in WMI are rather unique. They are not what you would typically expect. For example, the following wmic query returns the date in the format shown here:
>wmic path win32_operatingsystem get installdate
This format isn't very useful in SQL queries, such as date comparisons, or in end-user reports. However, it actually does contain the real date and time stamp for the queried attribute. In this case, the date is 2010-08-12 and the time is16:03:19 UTC. Note that this is the UTC value, not the local time. Not sure what the value after the decimal point is all about.
Since custom inventory in 7.0 and above use scripting languages to gather date, it would be fairly simple to create a function to parse out the separate pieces, such as:
WMIDateStringToDate = _
CStr(Mid(dtmInstallDate, 5, 2) &_ 'dd
Mid(dtmInstallDate, 7, 2) &_ 'mm
Left(dtmInstallDate, 4) &_ 'yyyy
" " &_
Mid (dtmInstallDate, 9, 2) &_ 'hh
Mid(dtmInstallDate, 11, 2) &_ 'mm
Mid(dtmInstallDate, 13, 2)) 'ss
WMI Date Object Script
However, VBScript provides an object-oriented method for converting dates to a standard format. This class is: ‘objSWbemDateTime’. Since the 'Value' property of the objSWbemDateTime is initially blank, we need to assign it a value based on the date we received from WMI. Once objSWbemDateTime has a value, we can get the properly-formatted date using the 'GetVarDate(False) method. In this case, the 'strDate' is the date object returned by the WMI query.
Function FormatFileDate (strDate)
objSWbemDateTime.Value = strDate
FormatFileDate = CStr(objSWbemDateTime.GetVarDate(False))
This returns the corresponding date in this format in local time: 8/12/2010 10:03:19 PM, which what we would expect to see in a readable format as well as a format usable in a sql query comparision operation, etc.
Output of both date scripts
Following is the output showing tthe results of both of the above methods:
Parsed InstallDate: 20100812160319.000000-360 ---> 08/12/2010 16:03:19
Object InstallDate: 20100812160319.000000-360 ---> 8/12/2010 10:03:19 PM
A Few of Notes
1. The instance of the wbem datetime object must be created:
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
2. The result must be converted to a string for use in custom inventory. Notice that the result of the objSWbemDateTime.GetVarDate method was converted to a string in the above example. This is because the custom inventory objDataRow class only accepts strings. The kb article, http://www.symantec.com/docs/HOWTO31200 , describes the NSE object model. It specifically states that the AeXNSEventDataRow data type is a string and provides the following description for the AddField and SetField methods:
“The value of the field. This may be a string, a number, a GUID, a resource object to reference another resource defined in the event, NULL to represent a database NULL value, or omitted to represent the default value (the default value is specified for the column when the dataclass is configured on the NS.)”
3. The data class definition, in the ‘Manage custom data classes’ screen, allows for 'date' data types when creating table attributes. The returned values will be loaded properly as date/time data types even though they are treated as strings in the custom inventory script.
4. The dataloader will drop the 'PM' string from the value. The value in the database will be similar to: '2010-08-12 10:03:19.000'.
TIP: Manually running custom inventory vbscript from a DOS shell
It is possible to manually run the script from a dos prompt of a client machine with the 7.x agent and inventory plug-in installed. The plug-in will register the dll with the operating system and make the class and its methods available. Hence, running the script from a dos/cmd window is the same as if it ran from a task running under the NS/SMP agent. This provides a great way to view output, errors, etc on screen for troubleshooting or data validation purposes. The output listed above was captured using this method with a couple of WScript.Echo statements embedded in the vbscript. Simply open a cmd window and run 'cscript <my>.vbs', or whatever your script name is. ('cscript' should be in the default path, already.)
(Note that the nse.SendQueued command is currently commented out in the attached sample for on-screen only testing purposes.)
Further information can be found at: http://technet.microsoft.com/en-us/magazine/2006.07.scriptingguy.aspx .