无法使用 python 在 wmi 中创建文件系统对象

Unable to create file system object in wmi using python

我使用 wmi 连接到远程 windows 服务器。我想创建文件系统对象以提取远程服务器上文件的文件版本。

我的代码是这样的:

# mc_name-machine name, login_machine() to login
c = login_machine(mc_name) 
print "logged-in"
# erroneous line. 
fo = c.win32com.client.Dispatch('Scripting.filesystemobject') 
# path=path of file on remote machine
print fo.GetFileVersion(path)

将不胜感激。

对于上面代码错误抛出的错误行:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\wmi.py", line 1145, in __getattr__
    return self._cached_classes (attribute)
  File "C:\Python34\lib\site-packages\wmi.py", line 1156, in _cached_classes
    self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
  File "<COMObject <unknown>>", line 3, in Get
  File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 282, in _ApplyTypes_
    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', 'Not found ', None, 0, -2147217406), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    fo=c.win32com.client.Dispatch('Scripting.filesystemobject')
  File "C:\Python34\lib\site-packages\wmi.py", line 1147, in __getattr__
    return getattr (self._namespace, attribute)
  File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.win32com

我试图通过使用 ProvideConstants 来解决这个问题。相同的代码

fo = win32com.client.Dispatch('Scripting.filesystemobject')
foo=ProvideConstants(fo)

我不确定如何使用这个 wmi.ProvideConstants 对象来获取文件版本。

我可以使用以下代码查询远程文件的版本:

c = wmi.WMI('computer', user='domain\user', password='pass')
result = c.query('SELECT * FROM CIM_DataFile WHERE Name = "C:\path\to\file"')
for file in result:
    print file.Version

Update:获取 LastModifed 时间作为 python datetime 对象,并以 dd/mm/yyyy 格式打印出来:

from datetime import datetime
last_modified = datetime(*wmi.to_time(file.LastModified)[:7])
print last_modified.strftime("%d/%m/%Y")