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

时间:2015-02-02 12:38:51

标签: python wmi filesystemobject

我使用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对象来获取文件版本。

1 个答案:

答案 0 :(得分:0)

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

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

更新:将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")