contrib/automation/hgautomation/winrm.py
changeset 43076 2372284d9457
parent 42661 e91930d712e8
child 43216 6a350194de7f
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
     9 
     9 
    10 import logging
    10 import logging
    11 import pprint
    11 import pprint
    12 import time
    12 import time
    13 
    13 
    14 from pypsrp.client import (
    14 from pypsrp.client import Client
    15     Client,
       
    16 )
       
    17 from pypsrp.powershell import (
    15 from pypsrp.powershell import (
    18     PowerShell,
    16     PowerShell,
    19     PSInvocationState,
    17     PSInvocationState,
    20     RunspacePool,
    18     RunspacePool,
    21 )
    19 )
    33 
    31 
    34     end_time = time.time() + timeout
    32     end_time = time.time() + timeout
    35 
    33 
    36     while True:
    34     while True:
    37         try:
    35         try:
    38             client = Client(host, username=username, password=password,
    36             client = Client(
    39                             ssl=ssl, connection_timeout=5)
    37                 host,
       
    38                 username=username,
       
    39                 password=password,
       
    40                 ssl=ssl,
       
    41                 connection_timeout=5,
       
    42             )
    40             client.execute_ps("Write-Host 'Hello, World!'")
    43             client.execute_ps("Write-Host 'Hello, World!'")
    41             return client
    44             return client
    42         except requests.exceptions.ConnectionError:
    45         except requests.exceptions.ConnectionError:
    43             if time.time() >= end_time:
    46             if time.time() >= end_time:
    44                 raise
    47                 raise
    76 
    79 
    77         for o in ps.output:
    80         for o in ps.output:
    78             print(format_object(o))
    81             print(format_object(o))
    79 
    82 
    80         if ps.state == PSInvocationState.FAILED:
    83         if ps.state == PSInvocationState.FAILED:
    81             raise Exception('PowerShell execution failed: %s' %
    84             raise Exception(
    82                             ' '.join(map(format_object, ps.streams.error)))
    85                 'PowerShell execution failed: %s'
       
    86                 % ' '.join(map(format_object, ps.streams.error))
       
    87             )