#Python code to connect with WinRM over HTTPS

1 messages · Page 1 of 1 (latest)

frigid abyss
#

Hi there, I am using pypsrp lib to connect to windows from WinRM over HTTPS. following is the code:

from pypsrp.shell import Process, SignalCode, WinRS
from pypsrp.wsman import WSMan
wsman = WSMan("x.x.x.x", ssl=True, auth="basic", encryption="auto",
              username="test", password="test")

with wsman, WinRS(wsman) as shell:
    process = Process(shell, "dir")
    process.invoke()
    process.signal(SignalCode.CTRL_C)

    # execute a process with arguments in the background
    process = Process(shell, "powershell", ["gci", "$pwd"])
    process.begin_invoke()  # start the invocation and return immediately
    process.poll_invoke()  # update the output stream
    process.end_invoke()  # finally wait until the process is finished
    process.signal(SignalCode.CTRL_C)

but i am getting following error:

requests.exceptions.SSLError: HTTPSConnectionPool(host='x.x.x.x', port=5986): Max retries exceeded with url: /wsman (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:997)')))

But I am able to connect to remote machine using powershell from WinRM using following command:

$session = New-PSSession -ComputerName x.x.x.x -Credential $credential -UseSSL -SessionOption $so -Authentication Basic

Please help me debugging.

tagging @oak edge because the is owner of pypsrp lib. 🙂

oak edge
#

You have a self signed certificate (pr the hostname doesn’t match) so you either need to ignore the cert checks or install a valid certificate

frigid abyss
#

how to ignore the check?

oak edge
#

It’s a kwarg to the WSMan object

frigid abyss
#

valida certificate mean cA cert?

oak edge
#

cert_validation=False

frigid abyss
#

now the code ran fine. is that means, connection is established?

oak edge
#

If it didn’t fail sure

frigid abyss
#

how can i verify?

oak edge
#

Run a command, print the output

frigid abyss
#

i print the process variable from above code and i got this.
<pypsrp.shell.Process object at 0x000001E8451FFFA0>

#

shall i try this:

with wsman, RunspacePool(wsman) as pool:
    # execute 'Get-Process | Select-Object Name'
    ps = PowerShell(pool)
    ps.add_cmdlet("Get-Process").add_cmdlet("Select-Object").add_argument("Name")
    output = ps.invoke()
print(output)
oak edge
#

That will give you pwsh objecys

#

These are the low level APIs you might be better off using pypsrp.client

frigid abyss
#

can you give me a sample code to verify coinnection?

oak edge
#

You can print stdout/stderr/rc as you wish

neat parcel
#

are you using powershell.exe 5 on purpose? Asking because pwsh.exe 7 is the new one

frigid abyss
#

using pwsh 7 FYI

#

@oak edge wanted to know if we can use client.copy to copy whole directory to windows?

oak edge
#

You’ll have to write your own code to create dirs and copy each file individually

#

Plus file copies of wsman is quite slow so this might not be the best tool