2016-11-22 240 views
0

我有一個IronPython腳本從WMI收集一些信息。我試圖收集的項目之一是從Win32_OperatingSystem。我能夠用得到的信息:將WMI的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的方式來轉換這個「時間戳」,以更加友好的格式?

回答

0

使用ManagementDateTimeConverter類中的方法將其轉換爲.net對象。該字段尤其是datetime,因此您需要使用ToDateTime()。您只需要添加對System.Management組件的引用即可。

clr.AddReference('System.Management') 
from System.Management import ManagementDateTimeConverter 
print ManagementDateTimeConverter.ToDateTime(last_boot) 
+0

工作完美。謝謝。 – user3783772

相關問題