pyvim UpdateVirtualSwitch返回“指定的参数不正确”

时间:2019-12-16 18:23:29

标签: python esxi pyvmomi

我正在尝试通过python更新虚拟交换机安全性设置,但失败并显示消息:

Traceback (most recent call last):
  File "VPC_CRS-Compliance-Remediation-ESXi.py", line 787, in <module>
    main()
  File "VPC_CRS-Compliance-Remediation-ESXi.py", line 771, in main
    current_setting(hosts, "check")
  File "VPC_CRS-Compliance-Remediation-ESXi.py", line 653, in current_setting
    host.configManager.networkSystem.UpdateVirtualSwitch(vswitchName="vSwitch0", spec=switch.spec)
  File "/root/pydev/py36-venv/lib64/python3.6/site-packages/pyVmomi/VmomiSupport.py", line 706, in <lambda>
    self.f(*(self.args + (obj,) + args), **kwargs)
  File "/root/pydev/py36-venv/lib64/python3.6/site-packages/pyVmomi/VmomiSupport.py", line 512, in _InvokeMethod
    return self._stub.InvokeMethod(self, info, args)
  File "/root/pydev/py36-venv/lib64/python3.6/site-packages/pyVmomi/SoapAdapter.py", line 1374, in InvokeMethod
    raise obj # pylint: disable-msg=E0702
pyVmomi.VmomiSupport.InvalidArgument: (vmodl.fault.InvalidArgument) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = 'A specified parameter was not correct: ',
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) [],
   invalidProperty = <unset>

我正在尝试这样设置:

for host in hosts:
    if host.runtime.connectionState == 'connected':
        #set_port_grp_security(host)

    security_policy = vim.host.NetworkPolicy.SecurityPolicy()
    security_policy.allowPromiscuous = False
    security_policy.macChanges = False
    security_policy.forgedTransmits = True
    network_policy = vim.host.NetworkPolicy(security=security_policy)
    switch = vim.host.VirtualSwitch.Config()
    switch.spec = vim.host.VirtualSwitch.Specification()
    switch.spec.policy = network_policy
    a = host.config.network.vswitch
    for i in a:
        print(i.spec.policy.security.allowPromiscuous)
        print(i.name)
        host.configManager.networkSystem.UpdateVirtualSwitch(vswitchName=i.name, spec=switch.spec)

我找不到任何示例,该示例如何在每个人都在创建或删除内容的任何地方正确地更新它,没有人在更新:D任何想法该怎么做?

1 个答案:

答案 0 :(得分:0)

这对我有用(它调用了函数,函数的参数是vim.HostSystem的宿主对象):

   switches = host.config.network.vswitch
   # attept to set security policy
   for vswitch in switches:
       host_id = host.configManager.networkSystem
       specs = vswitch.spec
       specs.policy.security.allowPromiscuous = var_allowPromiscuous
       specs.policy.security.macChanges = var_macChanges
       specs.policy.security.forgedTransmits = var_forgedTransmits
       host_id.UpdateVirtualSwitch(vswitchName=vswitch.name, spec=specs)

永远带我去弄清楚,希望它能对某人有所帮助。...