How to connect to remote machine via WinRM in Python (pywinrm) using domain account?

后端 未结 3 2065
渐次进展
渐次进展 2020-12-17 01:02

I would like to write a script in Python using pywinrm library to be able to connect to remote machine via WinRM.

import winrm

s = winrm.Session(\'MACHINEH         


        
3条回答
  •  星月不相逢
    2020-12-17 01:57

    Pywinrm to connect using domain user account:

    In Remote Windows Machine

    1. Make sure in the target windows machine the network connection type is "private", if it is "public" winrm would not get configured.
    2. Open command prompt and type:

      winrm qc
      winrm set winrm/config/service @{AllowUnencrypted="true"}
      
    3. Open Powershell and type:

      enable-psremoting
      set-item WSMan:\localhost\Client\TrustedHosts * # ('*' is for all hosts, you may specify the host you want)
      

    In your Python Code

    1. In your python script:

      import winrm
      
      host = 'YourWindowsHost'
      domain = 'YourDomain'
      user = 'YourDomainUser'
      password = 'YourPassword'
      
      session = winrm.Session(host, auth=('{}@{}'.format(user,domain), password), transport='ntlm')
      
      result = session.run_cmd('ipconfig', ['/all']) # To run command in cmd
      
      result = session.run_ps('Get-Acl') # To run Powershell block
      

提交回复
热议问题