WMI 工作:日期和時間

有數個 WMI 類別和腳本物件可剖析或轉換 CIM 日期時間 格式。 如需其他範例,請參閱 位於 的 TechNet ScriptCenter https://www.microsoft.com/technet

本主題所示的腳本範例只會從本機電腦取得資料。 如需如何使用腳本從遠端電腦取得資料的詳細資訊,請參閱 連線到遠端電腦上的 WMI

執行指令碼

下列程式描述如何執行腳本。

  1. 複製程式碼,並將它儲存在副檔名為 .vbs 的檔案中,例如 filename.vbs。 請確定文字編輯器不會將.txt副檔名新增至檔案。
  2. 開啟命令提示字元視窗,並流覽至您儲存檔案的目錄。
  3. 在命令提示字元中輸入 cscript filename.vbs
  4. 如果您無法存取事件記錄檔,請檢查您是否從提升許可權的命令提示字元執行。 某些事件記錄檔,例如安全性事件記錄檔,可能會受到使用者存取控制 (UAC) 保護。

注意

根據預設,cscript 會在命令提示字元視窗中顯示腳本的輸出。 因為 WMI 腳本可以產生大量的輸出,所以您可能會想要將輸出重新導向至檔案。 在命令提示字元中輸入 cscript filename.vbs > outfile.txt ,將 filename.vbs 腳本的輸出重新導向至 outfile.txt

下表列出可用來從本機電腦取得各種資料類型的腳本範例。

如何… WMI 類別或方法
...將 WMI 日期轉換為標準日期和時間?
使用 SWbemDateTime 物件將這些物件轉換成一般日期和時間。
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

或者,您的程式碼會手動執行工作。

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 

...判斷電腦上目前設定的時間?

使用 Win32_LocalTime 類別。

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
# 指定電腦並取得本機時間$Computer = 「.」 $times = Get-WmiObject Win32_LocalTime -computer $computer

<# 現在顯示結果#>

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 }

...判斷電腦執行所在的時區名稱?

使用 Win32_TimeZone 類別,並檢查 Description 屬性的值。

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  

...確定 「10/02/2000」 解譯為 2000 年 10 月 2 日,而不是 「2000 年 2 月 10 日」?

CIMDATETIME 格式管理日期,並使用 SWbemDateTime 方法,例如 GetVarDate 來將它們轉換成 FILETIMEVT_Date 格式。 因為 DATETIME 格式與地區設定無關,所以您可以撰寫在任何電腦上執行的腳本。 使用 SWbemDateTime 物件將這些物件轉換成一般日期和時間。 如需轉換日期和時間的詳細資訊,請參閱 日期和時間格式

...將 WMI 日期時間轉換成 .NET DateTime 值?

手動剖析字串,然後將擷取的值放入 DateTime 物件中。

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 工作

WMI C++ 應用程式範例

日期和時間格式

TechNet ScriptCenter

`