Rediger

Del via


WMI Tasks: Dates and Times

There are several WMI classes and a scripting object to parse or convert the CIM datetime format. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

To run a script

The following procedure describes how to run a script.

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...convert WMI dates to standard dates and times?
Use the SWbemDateTime object to convert these to regular dates and times.
VB
Set dtmInstallDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each strOS in objOS
    dtmInstallDate.Value = strOS.InstallDate
    Wscript.Echo dtmInstallDate.GetVarDate
Next

Or have your code do the task manually.

VB
Function WMIDateStringToDate(dtmInstallDate) 
    WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & Mid(dtmInstallDate, 7, 2) _
                        & "/" & Left(dtmInstallDate, 4) & " " & Mid (dtmInstallDate, 9, 2) & ":" _
                        & Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate,13, 2)) 
End Function 

...determine the time currently configured on a computer?

Use the Win32_LocalTime class.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem in colItems
    Wscript.Echo "Day:           " & objItem.Day & VBNewLine _
               & "Day Of Week:   " & objItem.DayOfWeek & VBNewLine _
               & "Hour:          " & objItem.Hour & VBNewLine _
               & "Minute:        " & objItem.Minute & VBNewLine _
               & "Month:         " & objItem.Month & VBNewLine _
               & "Quarter:       " & objItem.Quarter & VBNewLine _
               & "Second:        " & objItem.Second & VBNewLine _
               & "Week In Month: " & objItem.WeekInMonth & VBNewLine _
               & "Year:          " & objItem.Year 
Next

PowerShell
# Specify computer and get Local Time
$Computer = "."
$times = Get-WmiObject Win32_LocalTime -computer $computer

<# Now display the result #>

Foreach ($time in $times) { "Day : {0}" -f $Time.Day "Day Of Week : {0}" -f $Time.DayOfWeek "Hour : {0}" -f $Time.Hour "Minute : {0}" -f $Time.Minute "Month : {0}" -f $Time.Month "Quarter : {0}" -f $Time.Quarter "Second : {0}" -f $time.Second "Week In Month: {0}" -f $Time.WeekInMonth "Year : {0}" -f $Time.Year }

...determine the name of the time zone in which a computer is running?

Use the Win32_TimeZone class and check the value of the Description property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objItem in colItems
    Wscript.Echo "Description:   " & objItem.Description
    Wscript.Echo "Daylight Name: " & objItem.DaylightName
    Wscript.Echo "Standard Name: " & objItem.StandardName
    Wscript.Echo
Next
PowerShell
$Computer = "."  
$timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $computer  
  
<# Display details  #>
if ($computer -eq ".") {$computer = Hostname}  
"Time zone information on computer `"{0}`"" -f $computer  
"Time Zone Description   : {0}" -f $timezone.Description  
"Daylight Name           : {0}" -f $timezone.DaylightName  
"Standard Name           : {0}" -f $timezone.StandardName  

...ensure that "10/02/2000" is interpreted as Oct. 2, 2000, not "10 Feb, 2000"?

Manage dates in CIM DATETIME format and use SWbemDateTime methods, such as GetVarDate to convert to them to and from either FILETIME or VT_Date formats. Because DATETIME format is locale-independent, you can write a script that runs on any machine. Use the SWbemDateTime object to convert these to regular dates and times. See Date and Time Format for more information on converting dates and times.

...convert a WMI datetime to a .NET DateTime value?

Manually parse the string, then put the retrieved values into a DateTime object.

PowerShell
function WMIDateStringToDateTime( [String] $strWmiDate ) 
{ 
    $strWmiDate.Trim() > $null 
    $iYear   = [Int32]::Parse($strWmiDate.SubString( 0, 4)) 
    $iMonth  = [Int32]::Parse($strWmiDate.SubString( 4, 2)) 
    $iDay    = [Int32]::Parse($strWmiDate.SubString( 6, 2)) 
    $iHour   = [Int32]::Parse($strWmiDate.SubString( 8, 2)) 
    $iMinute = [Int32]::Parse($strWmiDate.SubString(10, 2)) 
    $iSecond = [Int32]::Parse($strWmiDate.SubString(12, 2)) 
<# decimal point is at $strWmiDate.Substring(14, 1) #> 
    $iMicroseconds = [Int32]::Parse($strWmiDate.Substring(15, 6)) 
    $iMilliseconds = $iMicroseconds / 1000 
    $iUtcOffsetMinutes = [Int32]::Parse($strWmiDate.Substring(21, 4)) 
    if ( $iUtcOffsetMinutes -ne 0 ) 
    { 
        $dtkind = [DateTimeKind]::Local 
    } 
    else 
    { 
        $dtkind = [DateTimeKind]::Utc 
    } 
    return New-Object -TypeName DateTime ` 
                      -ArgumentList $iYear, $iMonth, $iDay, ` 
                                    $iHour, $iMinute, $iSecond, ` 
                                    $iMilliseconds, $dtkind 
} 

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

Date and Time Format

TechNet ScriptCenter

`