2016-11-22 1 views
0

WMI에서 정보를 수집하는 IronPython 스크립트가 있습니다. 내가 수집하려고하는 아이템 중 하나는 LastBootUpTime에서 Win32_OperatingSystem입니다.WMI의 LastBootUpTime을보다 친숙한 형식으로 변환

20161117135516.486400-300 

더 친숙한 형식이 "타임 스탬프를"변환하는 IronPython의에 방법이 있나요을 다음과 같이

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 

결과는 : 내가 사용하여 정보를 얻을 수 있어요?

답변

0

ManagementDateTimeConverter 클래스의 메서드를 사용하여 .net 개체로 변환합니다. 이 필드는 특히 datetime이므로 ToDateTime()을 사용하는 것이 좋습니다. System.Management 어셈블리에 대한 참조를 추가하기 만하면됩니다.

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

완벽하게 작동했습니다. 고맙습니다. – user3783772

관련 문제