将 LastBootUpTime 从 WMI 转换为更友好的格式
Convert LastBootUpTime from WMI to a more friendly format
我有一个 IronPython 脚本可以从 WMI 收集一些信息。我要收集的其中一项是来自 Win32_OperatingSystem
的 LastBootUpTime
。我可以使用以下方式获取信息:
import clr
clr.AddReference('System.Management.Automation')
from System.Management.Automation import (
PSMethod, RunspaceInvoke
)
RUNSPACE = RunspaceInvoke()
def wmi(query):
return [dict([(prop.Name, prop.Value) for prop in psobj.Properties]) for psobj in RUNSPACE.Invoke(query)]
def to_ascii(s):
# ignore non-ascii chars
return s.encode('ascii','ignore')
operating_system = wmi('Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2"')[0]
last_boot = to_ascii(operating_system.get('LastBootUpTime'))
print last_boot
结果如下
20161117135516.486400-300
IronPython 中有没有办法将此 "timestamp" 转换为更友好的格式?
使用 ManagementDateTimeConverter
class to convert to a .net object. That field in particular is a datetime
so you'll want to use ToDateTime()
中找到的方法。您只需添加对 System.Management
程序集的引用。
clr.AddReference('System.Management')
from System.Management import ManagementDateTimeConverter
print ManagementDateTimeConverter.ToDateTime(last_boot)
我有一个 IronPython 脚本可以从 WMI 收集一些信息。我要收集的其中一项是来自 Win32_OperatingSystem
的 LastBootUpTime
。我可以使用以下方式获取信息:
import clr
clr.AddReference('System.Management.Automation')
from System.Management.Automation import (
PSMethod, RunspaceInvoke
)
RUNSPACE = RunspaceInvoke()
def wmi(query):
return [dict([(prop.Name, prop.Value) for prop in psobj.Properties]) for psobj in RUNSPACE.Invoke(query)]
def to_ascii(s):
# ignore non-ascii chars
return s.encode('ascii','ignore')
operating_system = wmi('Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2"')[0]
last_boot = to_ascii(operating_system.get('LastBootUpTime'))
print last_boot
结果如下
20161117135516.486400-300
IronPython 中有没有办法将此 "timestamp" 转换为更友好的格式?
使用 ManagementDateTimeConverter
class to convert to a .net object. That field in particular is a datetime
so you'll want to use ToDateTime()
中找到的方法。您只需添加对 System.Management
程序集的引用。
clr.AddReference('System.Management')
from System.Management import ManagementDateTimeConverter
print ManagementDateTimeConverter.ToDateTime(last_boot)