def winrm(self, script, winrm_kwargs=dict(), quiet=False, **kwargs):
"""
Executes a remote windows powershell script
:param script: A string with the powershell script
:param winrm_kwargs: The pywinrm Protocol class kwargs
:param quiet: Whether to hide the stdout/stderr output or not
:return: A tuple with the status code, the stdout and the stderr
:raise: WinRmError: If the command fails
"""
if self._vm_object:
self._wait_for_winrm_service(
kwargs['vcdriver_vm_winrm_username'],
kwargs['vcdriver_vm_winrm_password'],
**winrm_kwargs
)
winrm_session = self._open_winrm_session(
kwargs['vcdriver_vm_winrm_username'],
kwargs['vcdriver_vm_winrm_password'],
winrm_kwargs
)
if not quiet:
print('Executing remotely on {} ...'.format(self.ip()))
styled_print(Style.DIM)(script)
status, stdout, stderr = self._run_winrm_ps(winrm_session, script)
if not quiet:
styled_print(Style.BRIGHT)('CODE: {}'.format(status))
styled_print(Fore.GREEN)(stdout)
if status != 0:
if not quiet:
styled_print(Fore.RED)(stderr)
raise WinRmError(script, status, stdout, stderr)
else:
return status, stdout, stderr
评论列表
文章目录