Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
One of the more confusing aspects of WMI is the way WMI handles dates and times. For example, consider this simple script, which returns the date that the operating system was installed on a computer:
strComputer =
".
"Set objSWbemServices = GetObject(
"winmgmts:\\
"& strComputer &
"\root\cimv2
") Set colOS = objSWbemServices.ExecQuery(
"SELECT * FROM Win32_OperatingSystem
") For Each objOS in colOS Wscript.Echo objOS.InstallDate Next
When this script is run under CScript, output similar to the following appears in the command window:
20011224113047.000000-480
This is not a misprint; 20011224113047.000000 480 really is the date that the operating system was installed on the computer. In fact, this value indicates that the operating system was installed on December 24, 2001. The date is correct; the problem lies not with the date but with the fact that the date is displayed in the Universal Time Coordinate (UTC) format.
In the UTC format, dates are displayed as yyyymmddHHMMSS.xxxxxx UUU, where:
Consequently, the value 20011224113047.000000 480 is translated like this:
Needless to say, the UTC format creates a number of problems for both script writers and script users. For one thing, it is difficult to determine dates and times at a glance. For another, it is not exactly a straightforward process to query for items based on date-time values. Suppose you want to retrieve a list of all the folders on a computer that were created after September 3, 2002. This seems like a simple enough task; after all, the Win32_Directory class includes a CreationDate property that specifies the date the folder was created.
Unfortunately, you cannot simply ask WMI to return a list of folders created after 9/3/2002. This script, which attempts to do just that, will run, but will not return any data.
dtmTargetDate = #9/3/2002# strComputer =
".
"Set objSWbemServices = GetObject(
"winmgmts:\\
"& strComputer &
"\root\cimv2
") Set colDirectories = objSWbemServices.ExecQuery _ (
"SELECT * FROM Win32_Directory WHERE CreationDate > '
"& dtmTargetDate &
"'
") For Each objDirectory In colDirectories Wscript.Echo objDirectory.Name Next
Instead, you have to pass WMI the date using UTC format:
dtmTargetDate =
"20020903000000.000000-480
"strComputer =
".
"Set objSWbemServices = GetObject(
"winmgmts:\\
"& strComputer &
"\root\cimv2
") Set colDirectories = objSWBemServices.ExecQuery _ (
"SELECT * FROM Win32_Directory WHERE CreationDate > '
"& dtmTargetDate &
"'
") For Each objDirectory In colDirectories Wscript.Echo objDirectory.Name Next
Because dates are very important in system administration, and because WMI is the technology of choice for most system administration tasks, it is important for script writers to be able to carry out two tasks:
Send us your feedback | « Previous | Next » |