New-PSSession using python only (no subprocess module)

New-PSSession using python only (no subprocess module)

我想知道是否可以仅使用 python 将 New-PsSessionInvoke-Command 用于交换服务器?我正在进行测试,不想使用 subprocess 模块,而是想知道是否有任何 python 模块可以处理远程服务器的 powershell 命令?

我通常使用它连接到 powershell 中的 exchange 服务器:

    $password = ConvertTo-SecureString "SOMEPASSWORD" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("ENTEREMAILHERE", $password)
    $ses = New-PSSession -Name "ENTEREMAILHERE" -ConnectionUri https://exchange.intermedia.net/powershell -ConfigurationName Hosting.PowerShell -Credential $Cred -Authentication Basic

我试过的

我尝试用谷歌搜索一些模块,发现了两个不同的模块,但它们似乎都不适合我。

我尝试使用 pypsrp,但我认为我无法正确配置它

from httpx import BasicAuth
from pypsrp.powershell import PowerShell, RunspacePool
from pypsrp.wsman import WSMan

wsman = WSMan("https://exchange.intermedia.net/powershell", username="enteremail",
              password="enterpassword",
              auth="basic")

with RunspacePool(wsman) as pool:
    ps = PowerShell(pool)
    ps.add_cmdlet("Get-PSDrive").add_parameter("Name", "C")
    ps.invoke()
    # we will print the first object returned back to us
    print(ps.output[0])

我收到一条错误消息:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: //exchange.intermedia.net/powershell:5986/wsman (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002449336F610>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

我知道 url 可以正常工作,因为我每天都将它与 powershell 一起使用。

编辑:与@briantist 交谈后我尝试了:

wsman = WSMan("exchange.intermedia.net", username="EMAILHERE",
    password="PASSWORHERE",
    auth="basic",
    port=443,
    path="powershell")

看起来它会起作用,但后来失败了:

Code: 2150858811, Machine: exchange.intermedia.net, Reason: The WS-Management service cannot process the request. The resource URI (http://schemas.microsoft.com/powershell/Microsoft.PowerShell) was not found in  the WS-Management catalog.

我假设那是因为 https:// 不存在所以我尝试使用 https:// 并且它给出了与之前所说相同的错误:

requests.exceptions.ConnectionError:
HTTPSConnectionPool(host='https', port=443): Max retries exceeded
with url: //exchange.intermedia.net:443/powershell (Caused by
NewConnectionError('<urllib3.connection.HTTPSConnection object at
0x0000020759047AC0>: Failed to establish a new  connection: [Errno
11001] getaddrinfo failed'))

我对 pysprp 的体验主要是通过 Ansible 获得的,因为它支持 the psrp connection plugin,但我确实与该库的创建者进行了简短的交谈,他建议使用主机名并单独设置 path ,像这样:

WSMan("exchange.intermedia.net", port=443, path="powershell", ...)

更新:OP 确认它可以使用此代码:

from pypsrp.powershell import PowerShell, RunspacePool 
from pypsrp.wsman import WSMan  

wsman = WSMan("exchange.intermedia.net", username="ENTEREMAIL",               
              password="ENTERPASSWORD",
              auth="basic", port=443, path="powershell")  

with RunspacePool(wsman, configuration_name="Hosting.PowerShell") as pool:
    print("hello")

要点:

  • URI-based 端点需要使用 pypsrp
  • 拆分为主机名和路径
  • 如果使用 non-default 配置名称,请务必将其也传递给 RunspacePool 对象