powercli 中 invoke-vmscript 的 pyvmomi 等价物是什么?

What is the pyvmomi equivalent of invoke-vmscript in powercli?

我正在考虑将现有的 powercli 部署脚本移动到 python/pyvmomi,以获得多线程(它部署了很多虚拟机)。原始脚本大量使用 Invoke-VMScript 通过 VMware Tools 将 powershell 片段推送给每个来宾。

pyvmomi 中的等效功能是什么?具体来说 - 通过工具(不是访客的网络)向访客发送一个 powershell 脚本,让它 运行 使用提供的凭据,然后收集输出?

我可以看到 processManager.StartProgramInGuest,但这似乎是一个笨拙的 3 步过程(上传文件、运行 文件、下载重定向的结果)——这就是 powercli 在后台所做的吗?

因此,为了提供一些闭包,并且因为无论如何我都找不到完整的示例,所以这是我的第一个例子。它是已经使用 SmartConnect 连接到 vcenter 服务器的 class 的一部分,并设置 self.si。它并没有真正做很多错误检查。您可以选择是否要等待并获取输出,或者在启动命令后 return。 remote_cmd 最初来自 pyvmomi-community-samples,因此目前这两种方法之间存在一些重复。

def invoke_vmscript(self, vm_username, vm_password, vm_name, script_content, wait_for_output=False):

    script_content_crlf = script_content.replace('\n', '\r\n')

    content = self.si.content
    creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
    vm = self.get_vm(vm_name)
    logger.debug("Invoke-VMScript Started for %s", vm_name)
    logger.debug("CREATING TEMP OUTPUT DIR")
    file_manager = content.guestOperationsManager.fileManager

    temp_dir = file_manager.CreateTemporaryDirectoryInGuest(vm, creds, "nodebldr_",
                                                            "_scripts")
    try:
        file_manager.MakeDirectoryInGuest(vm, creds, temp_dir, False)
    except vim.fault.FileAlreadyExists:
        pass
    temp_script_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
                                                               "_script.ps1",
                                                               temp_dir)
    temp_output_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
                                                               "_output.txt",
                                                               temp_dir)
    logger.debug("SCRIPT FILE: " + temp_script_file)
    logger.debug("OUTPUT FILE: " + temp_output_file)
    file_attribute = vim.vm.guest.FileManager.FileAttributes()
    url = file_manager.InitiateFileTransferToGuest(vm, creds, temp_script_file,
                                                   file_attribute,
                                                   len(script_content_crlf), True)
    logger.debug("UPLOAD SCRIPT TO: " + url)
    r = requests.put(url, data=script_content_crlf, verify=False)
    if not r.status_code == 200:
        logger.debug("Error while uploading file")
    else:
        logger.debug("Successfully uploaded file")

    self.remote_cmd(vm_name, vm_username, vm_password, 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
                    "-Noninteractive {0} > {1}".format(temp_script_file, temp_output_file), temp_dir,
                    wait_for_end=wait_for_output)

    output = None
    if wait_for_output:
        dl_url = file_manager.InitiateFileTransferFromGuest(vm, creds,
                                                            temp_output_file)
        logger.debug("DOWNLOAD OUTPUT FROM: " + dl_url.url)
        r = requests.get(dl_url.url, verify=False)
        output = r.text
        logger.debug("Script Output was: %s", output)

    logger.debug("DELETING temp files & directory")
    file_manager.DeleteFileInGuest(vm, creds, temp_script_file)
    file_manager.DeleteFileInGuest(vm, creds, temp_output_file)
    file_manager.DeleteDirectoryInGuest(vm, creds, temp_dir, True)
    logger.debug("Invoke-VMScript COMPLETE")

    return output

def remote_cmd(self, vm_name, vm_username, vm_password, command, args, working_dir, wait_for_end=False, timeout=60):
    creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
    vm = self.get_vm(vm_name)
    try:
        cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(arguments=args, programPath=command)
        pid = self.si.content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=creds,
                                                                                        spec=cmdspec)
        logger.debug("Started process %d on %s", pid, vm_name)
    except vmodl.MethodFault as error:
        print("Caught vmodl fault : ", error.msg)
        return -1

    n = timeout
    if wait_for_end:
        while n > 0:
            info = self.si.content.guestOperationsManager.processManager.ListProcessesInGuest(vm=vm, auth=creds,
                                                                                              pids=[pid])
            if info[0].endTime is not None:
                break
            logger.debug("Process not yet completed. Will wait %d seconds", n)
            sleep(1)
            n = n - 1
        logger.debug("Process completed with state: %s", info[0])