WMI Tasks: Dates and Times

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.
Windows 2000:   SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
 
      
Set dtmInstallDate = CreateObject( _
    "WbemScripting.SWbemDateTime")
strComputer = "."
Set bjWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set bjOS = objWMIService.ExecQuery( _
    "Select * from Win32_OperatingSystem")
For Each strOS in objOS
    dtmInstallDate.Value = strOS.InstallDate
    Wscript.Echo dtmInstallDate.GetVarDate
Next
...determine the time currently configured on a computer? Use the Win32_LocalTime class.
Windows 2000:   Win32_LocalTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
 
      
strComputer = "."
Set bjWMIService = 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
...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.
 
      
strComputer = "."
Set bjWMIService = 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
...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.
Windows XP introduced several WMI classes and a scripting object to parse or convert the CIM datetime format. For other examples, see the TechNet ScriptCenter at http://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.

The following procedure describes how to run a script.

To run a script

1.Copy the code and save it in a file with a .vbs extension. 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. scriptfile.vbs at the command prompt.
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. scriptfile.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.
Windows 2000:   SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.

Set dtmInstallDate = CreateObject( _
    "WbemScripting.SWbemDateTime")
strComputer = "."
Set bjWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set bjOS = objWMIService.ExecQuery( _
    "Select * from Win32_OperatingSystem")
For Each strOS in objOS
    dtmInstallDate.Value = strOS.InstallDate
    Wscript.Echo dtmInstallDate.GetVarDate
Next
 
...determine the time currently configured on a computer?  Use the Win32_LocalTime class.
Windows 2000:   Win32_LocalTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.

strComputer = "."
Set bjWMIService = 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
 
...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.

strComputer = "."
Set bjWMIService = 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
 
...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. 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20200170/viewspace-762357/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/20200170/viewspace-762357/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值