如果我在pycharm中运行以下代码,我会收到此错误:
- 错误 -
C:\Python33\python.exe B:/Python/untitled3/working_test.py
'vim.VirtualMachine:vm-65063'
Traceback (most recent call last):
File "B:/Python/untitled3/working_test.py", line 47, in <module>
main()
File "B:/Python/untitled3/working_test.py", line 37, in main
filterspec = vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=source_machine))
TypeError: __init__() takes 1 positional argument but 2 were given
Process finished with exit code 1
- 错误 -
我尝试过使用自我,创建课程等,但我无法理解我做错的事情。任何帮助表示赞赏。基本上,我试图在vsphere中的实体(虚拟机)上获取任务信息。
谢谢!
import ssl
from pyVim import connect
from pyVmomi import vmodl, vim
def main():
try:
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
si = connect.SmartConnect(host='vcenter name',
user='user name',
pwd='password',
port=443,
sslContext=context)
if not si:
print("Could not connect to the specified host using specified "
"username and password")
return -1
content = si.RetrieveContent()
def getobject(vimtype, name):
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
source_machine = getobject([vim.VirtualMachine], 'virtual machine name')
print(source_machine)
taskManager = content.taskManager
filterspec = vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=source_machine))
collector = taskManager.CreateCollectorForTasks(filterspec)
except vmodl.MethodFault as e:
print("Caught vmodl fault : {}".format(e.msg))
return -1
return 0
if __name__ == "__main__":
main()
答案 0 :(得分:0)
vim.TaskFilterSpec()
不接受位置参数。可以通过以下方式对异常进行最小程度的复制:
>>> vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=vim.ManagedEntity('1')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes 1 positional argument but 2 were given
要使用vim.TaskFilterSpec()
命名参数调用类构造函数entity
。在上面的示例代码中,这意味着将第37行更改为:
filterspec = vim.TaskFilterSpec(entity=vim.TaskFilterSpec.ByEntity(entity=source_machine))
使用安慰剂ManagedEntity
调用时,会产生类似于以下内容的过滤规范:
>>> source_machine=vim.ManagedEntity('1')
>>> filterspec = vim.TaskFilterSpec(entity=vim.TaskFilterSpec.ByEntity(entity=source_machine))
>>> filterspec
(vim.TaskFilterSpec) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
entity = (vim.TaskFilterSpec.ByEntity) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
entity = 'vim.ManagedEntity:1',
recursion = <unset>
},
time = <unset>,
userName = <unset>,
activationId = (str) [],
state = (vim.TaskInfo.State) [],
alarm = <unset>,
scheduledTask = <unset>,
eventChainId = (int) [],
tag = (str) [],
parentTaskKey = (str) [],
rootTaskKey = (str) []
}
答案 1 :(得分:0)
谢谢你的帮助。
我已经调整了这部分代码并且它没有错误地返回,但现在查看,为什么它似乎只返回查询任务本身而不是与vm相关的任务。我认为它可能与处于中心级别的任务有关,但现在就开始工作了。
source_machine = getobject([vim.VirtualMachine], 'virtual machine name)
taskManager = content.taskManager
filterspec = vim.TaskFilterSpec()
filterspec.entity = vim.TaskFilterSpec.ByEntity(entity=source_machine,recursion='all')
collector = taskManager.CreateCollectorForTasks(filterspec)
print(collector)
输出返回:
C:\Python33\python.exe B:/Python/untitled3/working_test.py
'vim.TaskHistoryCollector:session[52b617f0-0f65-705c-7462-814d8b648fdd]52081175-cb98-a09f-f9f6-f6787f68d3b7'
Process finished with exit code 0