Help for running powershell script through winrm in python/lambda

237 views
Skip to first unread message

condes...@gmail.com

unread,
Jul 24, 2021, 5:21:35 PM7/24/21
to Ansible Project
Dear all: 

I found this mailing list through this link https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html#what-is-winrm. I have suffered by using winrm module inside my python script. I would like to user it to call a call powershell script to power off MS SQL engine and client services. But when I ran the script, I got the following error. 

I have checked on the target server that winrm service was started correctly and the configuration is as expected. 

My script is very simple, just several lines, it looks like it has connection issue. Anyone here can help ?

        host = '10.251.3.22'
        domain = 'alturamso.com'
        user = 'xxxx'
        password = 'xxxxx!'
        

        session = winrm.Session(host, auth=('{}@{}'.format(user,domain), password), transport='ntlm')

        result = session.run_cmd('ipconfig', ['/all']) # To run command in cmd

[ERROR] ConnectTimeout: HTTPConnectionPool(host='10.251.3.22', port=5985): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7fad329c6c90>, 'Connection to 10.251.3.22 timed out. (connect timeout=30)'))
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 245, in lambda_handler
    result = session.run_cmd('ipconfig', ['/all']) # To run command in cmd
  File "/opt/python/winrm/__init__.py", line 39, in run_cmd
    shell_id = self.protocol.open_shell()
  File "/opt/python/winrm/protocol.py", line 166, in open_shell
    res = self.send_message(xmltodict.unparse(req))
  File "/opt/python/winrm/protocol.py", line 243, in send_message
    resp = self.transport.send_message(message)
  File "/opt/python/winrm/transport.py", line 321, in send_message
    self.build_session()
  File "/opt/python/winrm/transport.py", line 304, in build_session
    self.setup_encryption()
  File "/opt/python/winrm/transport.py", line 310, in setup_encryption
    self._send_message_request(prepared_request, '')
  File "/opt/python/winrm/transport.py", line 339, in _send_message_request
    response = self.session.send(prepared_request, timeout=self.read_timeout_sec)
  File "/opt/python/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/opt/python/requests/adapters.py", line 504, in send
    raise ConnectTimeout(e, request=request)

Reply all
Reply to author
Forward
0 new messages