# Help for running powershell script through winrm in python/lambda

**URL:** <https://forum.ansible.com/t/help-for-running-powershell-script-through-winrm-in-python-lambda/35543>\
**Category:** Ansible Project\
**Created:** [July 24, 2021, 9:21pm UTC](https://forum.ansible.com/t/help-for-running-powershell-script-through-winrm-in-python-lambda/35543 "2021-07-24T21:21:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Yu](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Jason\_Yu](https://forum.ansible.com/u/Jason_Yu)\
**Post date:** [July 24, 2021, 9:21pm UTC](https://forum.ansible.com/t/help-for-running-powershell-script-through-winrm-in-python-lambda/35543/1 "2021-07-24T21:21:35Z")

</div>

Dear all:

I found this mailing list through this link [https://docs.ansible.com/ansible/latest/user\_guide/windows\_winrm.html#what-is-winrm](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](http://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)
